Issues-2: Adding Config Repository in the injectorUtils

This commit is contained in:
Mathieu Sanchez 2020-04-25 22:13:45 +02:00
parent 148fd55e92
commit 5b5a9c28f2
10 changed files with 72 additions and 39 deletions

View File

@ -2,18 +2,22 @@ package fr.sanchezm.attestationsCovid19.data.db
import android.content.Context import android.content.Context
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
import fr.sanchezm.attestationsCovid19.data.db.dao.ConfigDao
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
class MyDatabase private constructor(private val savePath: String, private val filesPath: String) { class MyDatabase private constructor(private val savePath: String, private val filesPath: String) {
private var _profileDao: ProfileDao? = null private var _profileDao: ProfileDao? = null
private var _attestationDao: AttestationDao? = null private var _attestationDao: AttestationDao? = null
private var _configDao: ConfigDao? = null
fun profileDao(): ProfileDao = _profileDao ?: ProfileDao(savePath).also { _profileDao = it } fun profileDao(): ProfileDao = _profileDao ?: ProfileDao(savePath).also { _profileDao = it }
fun attestationDao(): AttestationDao = fun attestationDao(): AttestationDao =
_attestationDao ?: AttestationDao(savePath, filesPath).also { _attestationDao = it } _attestationDao ?: AttestationDao(savePath, filesPath).also { _attestationDao = it }
fun configDao(): ConfigDao = _configDao ?: ConfigDao(savePath).also { _configDao = it }
companion object { companion object {
@Volatile @Volatile
private var instance: MyDatabase? = null private var instance: MyDatabase? = null

View File

@ -29,8 +29,12 @@ class ConfigDao(private val savePath: String) {
if (file.exists()) { if (file.exists()) {
_config.value = Gson().fromJson(file.readText(Charsets.UTF_8), Config::class.java) _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 { } else {
_config.value = Config(BuildConfig.DB_VERSION) _config.value =
Config(BuildConfig.DB_VERSION, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)
} }
} }

View File

@ -1,5 +1,7 @@
package fr.sanchezm.attestationsCovid19.data.db.entity package fr.sanchezm.attestationsCovid19.data.db.entity
data class Config( data class Config(
val dbVersion: Int val dbVersion: Int,
var versionName: String,
var versionCode: Int
) )

View File

@ -10,6 +10,7 @@ import fr.sanchezm.attestationsCovid19.R
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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.data.repository.ProfileRepository
import fr.sanchezm.attestationsCovid19.utilities.Event import fr.sanchezm.attestationsCovid19.utilities.Event
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -18,6 +19,7 @@ import java.time.format.DateTimeFormatter
import java.util.* import java.util.*
class AddViewModel( class AddViewModel(
private val configRepository: ConfigRepository,
private val profileRepository: ProfileRepository, private val profileRepository: ProfileRepository,
private val attestationRepository: AttestationRepository private val attestationRepository: AttestationRepository
) : ViewModel() { ) : ViewModel() {

View File

@ -3,9 +3,11 @@ package fr.sanchezm.attestationsCovid19.ui.add
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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.data.repository.ProfileRepository
class AddViewModelFactory( class AddViewModelFactory(
private val configRepository: ConfigRepository,
private val profileRepository: ProfileRepository, private val profileRepository: ProfileRepository,
private val attestationRepository: AttestationRepository private val attestationRepository: AttestationRepository
) : ) :
@ -13,6 +15,6 @@ class AddViewModelFactory(
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return AddViewModel(profileRepository, attestationRepository) as T return AddViewModel(configRepository, profileRepository, attestationRepository) as T
} }
} }

View File

@ -5,9 +5,14 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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.Event
class AttestationsViewModel(attestationRepository: AttestationRepository) : ViewModel() { class AttestationsViewModel(
private val configRepository: ConfigRepository,
attestationRepository: AttestationRepository
) :
ViewModel() {
private val _startActivity = MutableLiveData<Event<Long>>() private val _startActivity = MutableLiveData<Event<Long>>()
private var _displayNoAttestation = MutableLiveData<Int>() private var _displayNoAttestation = MutableLiveData<Int>()

View File

@ -3,13 +3,16 @@ package fr.sanchezm.attestationsCovid19.ui.attestations
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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() { ViewModelProvider.NewInstanceFactory() {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return AttestationsViewModel(attestationRepository) as T return AttestationsViewModel(configRepository, attestationRepository) as T
} }
} }

View File

@ -7,10 +7,15 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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.Event
import fr.sanchezm.attestationsCovid19.utilities.QRCodeUtils 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>() private val _attestation = MutableLiveData<Attestation>()

View File

@ -3,12 +3,16 @@ package fr.sanchezm.attestationsCovid19.ui.qrcode
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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() { ViewModelProvider.NewInstanceFactory() {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return QrCodeViewModel(attestationRepository) as T return QrCodeViewModel(configRepository, attestationRepository) as T
} }
} }

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.os.Environment import android.os.Environment
import fr.sanchezm.attestationsCovid19.data.db.MyDatabase import fr.sanchezm.attestationsCovid19.data.db.MyDatabase
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository 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.data.repository.ProfileRepository
import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory
import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFactory import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFactory
@ -11,41 +12,42 @@ import fr.sanchezm.attestationsCovid19.ui.qrcode.QrCodeViewModelFactory
object InjectorUtils { object InjectorUtils {
fun provideAddViewModelFactory(context: Context): AddViewModelFactory { fun provideAddViewModelFactory(context: Context): AddViewModelFactory =
val profileRepository = AddViewModelFactory(
ProfileRepository.getInstance( getConfigRepo(context),
MyDatabase.invoke(context, getMyFilesDir(context)).profileDao() getProfileRepo(context),
) getAttestationRepo(context)
val attestationRepository = )
AttestationRepository.getInstance(
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
)
return AddViewModelFactory(profileRepository, attestationRepository) fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory =
} AttestationsViewModelFactory(
getConfigRepo(context),
getAttestationRepo(context)
)
fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory { fun provideQrCodeViewModel(context: Context): QrCodeViewModelFactory =
val attestationRepository = QrCodeViewModelFactory(
AttestationRepository.getInstance( getConfigRepo(context),
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao() getAttestationRepo(context)
) )
return AttestationsViewModelFactory(attestationRepository) fun providePdfUtils(context: Context): PdfUtils =
} PdfUtils.getInstance(context.assets, getMyFilesDir(context))
fun provideQrCodeViewModel(context: Context): QrCodeViewModelFactory { private fun getMyFilesDir(context: Context): String =
val attestationRepository = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.path!!
AttestationRepository.getInstance(
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
)
return QrCodeViewModelFactory(attestationRepository) private fun getAttestationRepo(context: Context): AttestationRepository =
} AttestationRepository.getInstance(
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
)
fun providePdfUtils(context: Context): PdfUtils { private fun getProfileRepo(context: Context): ProfileRepository = ProfileRepository.getInstance(
return PdfUtils.getInstance(context.assets, getMyFilesDir(context)) 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()
)
} }