From 148fd55e92abb08a0e6ca446200046bd1cccb97f Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Sat, 25 Apr 2020 09:28:00 +0200 Subject: [PATCH] Add Config, ConfigDao et ConfigRepository Change var to val in ProfileDao and AttestationDao --- .../data/db/dao/AttestationDao.kt | 4 +- .../data/db/dao/ConfigDao.kt | 41 +++++++++++++++++++ .../data/db/dao/ProfileDao.kt | 4 +- .../data/db/entity/Config.kt | 5 +++ .../data/repository/ConfigRepository.kt | 21 ++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ConfigDao.kt create mode 100644 app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Config.kt create mode 100644 app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ConfigRepository.kt diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt index 8fc78b6..417f7ef 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt @@ -11,8 +11,8 @@ import java.io.File class AttestationDao(private val savePath: String, private val filesPath: String) { - private var _attestations = MutableLiveData>() - private var fileName = "attestation.db" + private val _attestations = MutableLiveData>() + private val fileName = "attestation.db" private val type = object : TypeToken>() {}.type @Suppress("UNCHECKED_CAST") 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 new file mode 100644 index 0000000..98cdc1b --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ConfigDao.kt @@ -0,0 +1,41 @@ +package fr.sanchezm.attestationsCovid19.data.db.dao + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import com.google.gson.Gson +import fr.sanchezm.attestationsCovid19.BuildConfig +import fr.sanchezm.attestationsCovid19.data.db.entity.Config +import java.io.File + +class ConfigDao(private val savePath: String) { + + private val _config = MutableLiveData() + private val fileName = "config.db" + + fun getConfig(): LiveData = + _config + + fun updateConfig(config: Config) { + _config.value = config + save() + } + + init { + load() + } + + private fun load() { + val file = File("$savePath/$fileName") + + if (file.exists()) { + _config.value = Gson().fromJson(file.readText(Charsets.UTF_8), Config::class.java) + } else { + _config.value = Config(BuildConfig.DB_VERSION) + } + } + + private fun save() { + File("$savePath/$fileName").writeText(Gson().toJson(_config.value)) + } + +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ProfileDao.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ProfileDao.kt index d285a5d..c152df5 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ProfileDao.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/ProfileDao.kt @@ -8,8 +8,8 @@ import java.io.File class ProfileDao(private val path: String) { - private var _profile = MutableLiveData() - private var fileName = "profile.db" + private val _profile = MutableLiveData() + private val fileName = "profile.db" fun getProfile(): LiveData = _profile 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 new file mode 100644 index 0000000..a584749 --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Config.kt @@ -0,0 +1,5 @@ +package fr.sanchezm.attestationsCovid19.data.db.entity + +data class Config( + val dbVersion: Int +) \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ConfigRepository.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ConfigRepository.kt new file mode 100644 index 0000000..0c0ccfd --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ConfigRepository.kt @@ -0,0 +1,21 @@ +package fr.sanchezm.attestationsCovid19.data.repository + +import fr.sanchezm.attestationsCovid19.data.db.dao.ConfigDao +import fr.sanchezm.attestationsCovid19.data.db.entity.Config + +class ConfigRepository private constructor(private val configDao: ConfigDao) { + + fun getConfig() = configDao.getConfig() + + fun updateConfig(config: Config) = configDao.updateConfig(config) + + companion object { + @Volatile + private var instance: ConfigRepository? = null + + fun getInstance(configDao: ConfigDao) = + instance ?: synchronized(this) { + instance ?: ConfigRepository(configDao).also { instance = it } + } + } +} \ No newline at end of file