Issue 2: Add config #7
@ -2,18 +2,22 @@ package fr.sanchezm.attestationsCovid19.data.db
|
||||
|
||||
import android.content.Context
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ConfigDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
|
||||
class MyDatabase private constructor(private val savePath: String, private val filesPath: String) {
|
||||
|
||||
private var _profileDao: ProfileDao? = null
|
||||
private var _attestationDao: AttestationDao? = null
|
||||
private var _configDao: ConfigDao? = null
|
||||
|
||||
fun profileDao(): ProfileDao = _profileDao ?: ProfileDao(savePath).also { _profileDao = it }
|
||||
|
||||
fun attestationDao(): AttestationDao =
|
||||
_attestationDao ?: AttestationDao(savePath, filesPath).also { _attestationDao = it }
|
||||
|
||||
fun configDao(): ConfigDao = _configDao ?: ConfigDao(savePath).also { _configDao = it }
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: MyDatabase? = null
|
||||
|
@ -29,8 +29,12 @@ class ConfigDao(private val savePath: String) {
|
||||
|
||||
if (file.exists()) {
|
||||
_config.value = Gson().fromJson(file.readText(Charsets.UTF_8), Config::class.java)
|
||||
_config.value?.versionCode = BuildConfig.VERSION_CODE
|
||||
_config.value?.versionName = BuildConfig.VERSION_NAME
|
||||
save()
|
||||
} else {
|
||||
_config.value = Config(BuildConfig.DB_VERSION)
|
||||
_config.value =
|
||||
Config(BuildConfig.DB_VERSION, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||
|
||||
data class Config(
|
||||
val dbVersion: Int
|
||||
val dbVersion: Int,
|
||||
var versionName: String,
|
||||
var versionCode: Int
|
||||
)
|
@ -10,6 +10,7 @@ import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
import fr.sanchezm.attestationsCovid19.utilities.Event
|
||||
import java.text.SimpleDateFormat
|
||||
@ -18,6 +19,7 @@ import java.time.format.DateTimeFormatter
|
||||
import java.util.*
|
||||
|
||||
class AddViewModel(
|
||||
private val configRepository: ConfigRepository,
|
||||
private val profileRepository: ProfileRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) : ViewModel() {
|
||||
|
@ -3,9 +3,11 @@ package fr.sanchezm.attestationsCovid19.ui.add
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AddViewModelFactory(
|
||||
private val configRepository: ConfigRepository,
|
||||
private val profileRepository: ProfileRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) :
|
||||
@ -13,6 +15,6 @@ class AddViewModelFactory(
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AddViewModel(profileRepository, attestationRepository) as T
|
||||
return AddViewModel(configRepository, profileRepository, attestationRepository) as T
|
||||
}
|
||||
}
|
@ -5,9 +5,14 @@ import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.utilities.Event
|
||||
|
||||
class AttestationsViewModel(attestationRepository: AttestationRepository) : ViewModel() {
|
||||
class AttestationsViewModel(
|
||||
private val configRepository: ConfigRepository,
|
||||
attestationRepository: AttestationRepository
|
||||
) :
|
||||
ViewModel() {
|
||||
|
||||
private val _startActivity = MutableLiveData<Event<Long>>()
|
||||
private var _displayNoAttestation = MutableLiveData<Int>()
|
||||
|
@ -3,13 +3,16 @@ package fr.sanchezm.attestationsCovid19.ui.attestations
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
|
||||
class AttestationsViewModelFactory(private val attestationRepository: AttestationRepository) :
|
||||
class AttestationsViewModelFactory(
|
||||
private val configRepository: ConfigRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AttestationsViewModel(attestationRepository) as T
|
||||
return AttestationsViewModel(configRepository, attestationRepository) as T
|
||||
}
|
||||
}
|
@ -7,10 +7,15 @@ import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.utilities.Event
|
||||
import fr.sanchezm.attestationsCovid19.utilities.QRCodeUtils
|
||||
|
||||
class QrCodeViewModel(private val attestationRepository: AttestationRepository) : ViewModel() {
|
||||
class QrCodeViewModel(
|
||||
private val configRepository: ConfigRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) :
|
||||
ViewModel() {
|
||||
|
||||
private val _attestation = MutableLiveData<Attestation>()
|
||||
|
||||
|
@ -3,12 +3,16 @@ package fr.sanchezm.attestationsCovid19.ui.qrcode
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
|
||||
class QrCodeViewModelFactory(private val attestationRepository: AttestationRepository) :
|
||||
class QrCodeViewModelFactory(
|
||||
private val configRepository: ConfigRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return QrCodeViewModel(attestationRepository) as T
|
||||
return QrCodeViewModel(configRepository, attestationRepository) as T
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.os.Environment
|
||||
import fr.sanchezm.attestationsCovid19.data.db.MyDatabase
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory
|
||||
import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFactory
|
||||
@ -11,41 +12,42 @@ import fr.sanchezm.attestationsCovid19.ui.qrcode.QrCodeViewModelFactory
|
||||
|
||||
object InjectorUtils {
|
||||
|
||||
fun provideAddViewModelFactory(context: Context): AddViewModelFactory {
|
||||
val profileRepository =
|
||||
ProfileRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).profileDao()
|
||||
)
|
||||
val attestationRepository =
|
||||
AttestationRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
|
||||
)
|
||||
fun provideAddViewModelFactory(context: Context): AddViewModelFactory =
|
||||
AddViewModelFactory(
|
||||
getConfigRepo(context),
|
||||
getProfileRepo(context),
|
||||
getAttestationRepo(context)
|
||||
)
|
||||
|
||||
return AddViewModelFactory(profileRepository, attestationRepository)
|
||||
}
|
||||
fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory =
|
||||
AttestationsViewModelFactory(
|
||||
getConfigRepo(context),
|
||||
getAttestationRepo(context)
|
||||
)
|
||||
|
||||
fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory {
|
||||
val attestationRepository =
|
||||
AttestationRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
|
||||
)
|
||||
fun provideQrCodeViewModel(context: Context): QrCodeViewModelFactory =
|
||||
QrCodeViewModelFactory(
|
||||
getConfigRepo(context),
|
||||
getAttestationRepo(context)
|
||||
)
|
||||
|
||||
return AttestationsViewModelFactory(attestationRepository)
|
||||
}
|
||||
fun providePdfUtils(context: Context): PdfUtils =
|
||||
PdfUtils.getInstance(context.assets, getMyFilesDir(context))
|
||||
|
||||
fun provideQrCodeViewModel(context: Context): QrCodeViewModelFactory {
|
||||
val attestationRepository =
|
||||
AttestationRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
|
||||
)
|
||||
private fun getMyFilesDir(context: Context): String =
|
||||
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.path!!
|
||||
|
||||
return QrCodeViewModelFactory(attestationRepository)
|
||||
}
|
||||
private fun getAttestationRepo(context: Context): AttestationRepository =
|
||||
AttestationRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
|
||||
)
|
||||
|
||||
fun providePdfUtils(context: Context): PdfUtils {
|
||||
return PdfUtils.getInstance(context.assets, getMyFilesDir(context))
|
||||
}
|
||||
private fun getProfileRepo(context: Context): ProfileRepository = ProfileRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).profileDao()
|
||||
)
|
||||
|
||||
private fun getMyFilesDir(context: Context): String = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.path!!
|
||||
private fun getConfigRepo(context: Context): ConfigRepository = ConfigRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).configDao()
|
||||
)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user