Add Config, ConfigDao et ConfigRepository
Change var to val in ProfileDao and AttestationDao
This commit is contained in:
		@@ -11,8 +11,8 @@ import java.io.File
 | 
			
		||||
 | 
			
		||||
class AttestationDao(private val savePath: String, private val filesPath: String) {
 | 
			
		||||
 | 
			
		||||
    private var _attestations = MutableLiveData<ArrayList<Attestation>>()
 | 
			
		||||
    private var fileName = "attestation.db"
 | 
			
		||||
    private val _attestations = MutableLiveData<ArrayList<Attestation>>()
 | 
			
		||||
    private val fileName = "attestation.db"
 | 
			
		||||
    private val type = object : TypeToken<ArrayList<Attestation>>() {}.type
 | 
			
		||||
 | 
			
		||||
    @Suppress("UNCHECKED_CAST")
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Config>()
 | 
			
		||||
    private val fileName = "config.db"
 | 
			
		||||
 | 
			
		||||
    fun getConfig(): LiveData<Config> =
 | 
			
		||||
        _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))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -8,8 +8,8 @@ import java.io.File
 | 
			
		||||
 | 
			
		||||
class ProfileDao(private val path: String) {
 | 
			
		||||
 | 
			
		||||
    private var _profile = MutableLiveData<Profile>()
 | 
			
		||||
    private var fileName = "profile.db"
 | 
			
		||||
    private val _profile = MutableLiveData<Profile>()
 | 
			
		||||
    private val fileName = "profile.db"
 | 
			
		||||
 | 
			
		||||
    fun getProfile(): LiveData<Profile> =
 | 
			
		||||
        _profile
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,5 @@
 | 
			
		||||
package fr.sanchezm.attestationsCovid19.data.db.entity
 | 
			
		||||
 | 
			
		||||
data class Config(
 | 
			
		||||
    val dbVersion: Int
 | 
			
		||||
)
 | 
			
		||||
@@ -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 }
 | 
			
		||||
            }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user