From 5b5a9c28f289b633b8c4bf21d6f8c6b1d12ff094 Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Sat, 25 Apr 2020 22:13:45 +0200 Subject: [PATCH] Issues-2: Adding Config Repository in the injectorUtils --- .../attestationsCovid19/data/db/MyDatabase.kt | 4 ++ .../data/db/dao/ConfigDao.kt | 6 +- .../data/db/entity/Config.kt | 4 +- .../ui/add/AddViewModel.kt | 2 + .../ui/add/AddViewModelFactory.kt | 4 +- .../ui/attestations/AttestationsViewModel.kt | 7 ++- .../AttestationsViewModelFactory.kt | 9 ++- .../ui/qrcode/QrCodeViewModel.kt | 7 ++- .../ui/qrcode/QrCodeViewModelFactory.kt | 8 ++- .../utilities/InjectorUtils.kt | 60 ++++++++++--------- 10 files changed, 72 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt index f113f15..28ff7ed 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt @@ -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 diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ConfigDao.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ConfigDao.kt index 98cdc1b..cd86a5e 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ConfigDao.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ConfigDao.kt @@ -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) } } diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Config.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Config.kt index a584749..3f479d2 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Config.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Config.kt @@ -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 ) \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt index b8249d8..2ad4498 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt @@ -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() { diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt index 5570fb7..34545e8 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt @@ -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 create(modelClass: Class): T { - return AddViewModel(profileRepository, attestationRepository) as T + return AddViewModel(configRepository, profileRepository, attestationRepository) as T } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt index 4a23ca4..1a240e1 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt @@ -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>() private var _displayNoAttestation = MutableLiveData() diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt index 913cb1b..ed05e45 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt @@ -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 create(modelClass: Class): T { - return AttestationsViewModel(attestationRepository) as T + return AttestationsViewModel(configRepository, attestationRepository) as T } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt index 7a3e6b9..600c58d 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt @@ -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() diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt index 18f4395..98bc5bd 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt @@ -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 create(modelClass: Class): T { - return QrCodeViewModel(attestationRepository) as T + return QrCodeViewModel(configRepository, attestationRepository) as T } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt index 0da4f76..9e0f85d 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt @@ -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() + ) } \ No newline at end of file