Adding and updating DB version to 2
This commit is contained in:
parent
ea6c9c4ee0
commit
5f373a19d7
@ -15,6 +15,7 @@
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
tools:replace="android:allowBackup"
|
||||
android:theme="@style/AppTheme.Light">
|
||||
<activity android:name=".QrCodeActivity" />
|
||||
<activity android:name=".PdfViewerActivity" />
|
||||
|
@ -4,6 +4,7 @@ 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
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
|
||||
class MyDatabase private constructor(private val savePath: String, private val filesPath: String) {
|
||||
|
||||
@ -11,10 +12,15 @@ class MyDatabase private constructor(private val savePath: String, private val f
|
||||
private var _attestationDao: AttestationDao? = null
|
||||
private var _configDao: ConfigDao? = null
|
||||
|
||||
fun profileDao(): ProfileDao = _profileDao ?: ProfileDao(savePath).also { _profileDao = it }
|
||||
fun profileDao(configRepository: ConfigRepository): ProfileDao =
|
||||
_profileDao ?: ProfileDao(savePath, configRepository).also { _profileDao = it }
|
||||
|
||||
fun attestationDao(): AttestationDao =
|
||||
_attestationDao ?: AttestationDao(savePath, filesPath).also { _attestationDao = it }
|
||||
fun attestationDao(configRepository: ConfigRepository): AttestationDao =
|
||||
_attestationDao ?: AttestationDao(
|
||||
savePath,
|
||||
filesPath,
|
||||
configRepository
|
||||
).also { _attestationDao = it }
|
||||
|
||||
fun configDao(): ConfigDao = _configDao ?: ConfigDao(savePath).also { _configDao = it }
|
||||
|
||||
@ -28,40 +34,3 @@ class MyDatabase private constructor(private val savePath: String, private val f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
@Database(
|
||||
entities = [Profile::class],
|
||||
version = 1
|
||||
)
|
||||
abstract class MyDatabase private constructor() : RoomDatabase() {
|
||||
|
||||
abstract fun profileDao(): ProfileDao
|
||||
abstract fun attestationDao(): AttestationDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: MyDatabase? = null
|
||||
private val LOCK = Any()
|
||||
|
||||
operator fun invoke(context: Context) = instance ?: synchronized(LOCK) {
|
||||
instance ?: buildDatabase(context).also { instance = it }
|
||||
}
|
||||
|
||||
private fun buildDatabase(context: Context) =
|
||||
Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
MyDatabase::class.java, "data.db"
|
||||
)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -6,10 +6,15 @@ import androidx.lifecycle.MutableLiveData
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.utilities.PdfUtils
|
||||
import java.io.File
|
||||
|
||||
class AttestationDao(private val savePath: String, private val filesPath: String) {
|
||||
class AttestationDao(
|
||||
private val savePath: String,
|
||||
private val filesPath: String,
|
||||
private val configRepository: ConfigRepository
|
||||
) {
|
||||
|
||||
private val _attestations = MutableLiveData<ArrayList<Attestation>>()
|
||||
private val fileName = "attestation.db"
|
||||
@ -23,9 +28,8 @@ class AttestationDao(private val savePath: String, private val filesPath: String
|
||||
_attestations.value?.elementAt(id)
|
||||
|
||||
fun addAttestation(attestation: Attestation) {
|
||||
_attestations.value = _attestations.value ?: ArrayList()
|
||||
|
||||
_attestations.value?.add(attestation)
|
||||
_attestations.value = _attestations.value
|
||||
Log.d(TAG, "Add Attestation : $attestation")
|
||||
save()
|
||||
}
|
||||
@ -66,6 +70,8 @@ class AttestationDao(private val savePath: String, private val filesPath: String
|
||||
|
||||
if (file.exists()) {
|
||||
_attestations.value = Gson().fromJson(file.readText(Charsets.UTF_8), type)
|
||||
} else {
|
||||
_attestations.value = ArrayList()
|
||||
}
|
||||
_attestations.value?.forEach {
|
||||
val filePath = PdfUtils.getPath(filesPath, it.createAt)
|
||||
|
@ -1,48 +1,69 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.dao
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import fr.sanchezm.attestationsCovid19.BuildConfig
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||
import fr.sanchezm.attestationsCovid19.utilities.DbMigration
|
||||
import java.io.File
|
||||
|
||||
class ProfileDao(private val path: String) {
|
||||
class ProfileDao(private val path: String, private val configRepository: ConfigRepository) {
|
||||
|
||||
private val _profile = MutableLiveData<Profile>()
|
||||
private val _profile = MutableLiveData<ArrayList<Profile>>()
|
||||
private val fileName = "profile.db"
|
||||
private val type = object : TypeToken<ArrayList<Profile>>() {}.type
|
||||
|
||||
fun getProfile(): LiveData<Profile> =
|
||||
_profile
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun getProfiles(): LiveData<List<Profile>> =
|
||||
_profile as LiveData<List<Profile>>
|
||||
|
||||
fun updateProfile(profile: Profile) {
|
||||
_profile.value = profile
|
||||
fun addProfile(profile: Profile) {
|
||||
_profile.value?.add(profile)
|
||||
_profile.value = _profile.value
|
||||
save()
|
||||
}
|
||||
|
||||
init {
|
||||
load()
|
||||
val currentDbVersion = configRepository.getConfig().value!!.dbVersion
|
||||
val appDbVersion = BuildConfig.DB_VERSION
|
||||
|
||||
if (currentDbVersion != appDbVersion) {
|
||||
if (currentDbVersion == 1 && appDbVersion == 2) {
|
||||
_profile.value = DbMigration.migrateProfileFrom1To2("$path/$fileName")
|
||||
save()
|
||||
configRepository.getConfig().value!!.let {
|
||||
it.dbVersion = appDbVersion
|
||||
configRepository.updateConfig(it)
|
||||
}
|
||||
Log.d(TAG, "Db version update from $currentDbVersion to $appDbVersion")
|
||||
} else {
|
||||
File("$path/$fileName").delete()
|
||||
load()
|
||||
}
|
||||
} else {
|
||||
load()
|
||||
}
|
||||
}
|
||||
|
||||
private fun load() {
|
||||
val file = File("$path/$fileName")
|
||||
|
||||
if (file.exists()) {
|
||||
_profile.value = Gson().fromJson(file.readText(Charsets.UTF_8), Profile::class.java)
|
||||
_profile.value = Gson().fromJson(file.readText(Charsets.UTF_8), type)
|
||||
} else {
|
||||
_profile.value = Profile("", "", "", "", "", "", "")
|
||||
_profile.value = ArrayList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun save() {
|
||||
File("$path/$fileName").writeText(Gson().toJson(_profile.value))
|
||||
}
|
||||
}
|
||||
|
||||
//@Dao
|
||||
//interface ProfileDao {
|
||||
// @Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
// fun insert(profile: Profile)
|
||||
//
|
||||
// @Query(value = "SELECT * FROM profile WHERE id = $CURRENT_PROFILE_ID")
|
||||
// fun getProfile(): LiveData<Profile>
|
||||
//}
|
||||
companion object {
|
||||
private val TAG = ProfileDao::class.simpleName
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import java.util.*
|
||||
|
||||
const val PATTERN = "dd/MM/yyyy 'a' HH'h'mm"
|
||||
|
||||
//@Entity(tableName = "attestation")
|
||||
data class Attestation(
|
||||
val profile: Profile,
|
||||
val exitDate: String,
|
||||
|
@ -1,7 +1,7 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||
|
||||
data class Config(
|
||||
val dbVersion: Int,
|
||||
var dbVersion: Int,
|
||||
var versionName: String,
|
||||
var versionCode: Int
|
||||
)
|
@ -1,6 +1,5 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||
|
||||
//@Entity(tableName = "profile")
|
||||
data class Profile(
|
||||
val firstName: String,
|
||||
val lastName: String,
|
||||
@ -10,8 +9,6 @@ data class Profile(
|
||||
val city: String,
|
||||
val postalCode: String
|
||||
) {
|
||||
// @PrimaryKey
|
||||
// var id: Int = CURRENT_PROFILE_ID
|
||||
|
||||
override fun toString(): String {
|
||||
return "Nom: $lastName; Prenom: $firstName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city"
|
||||
|
@ -6,11 +6,11 @@ import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
class ProfileRepository private constructor(private val profileDao: ProfileDao) {
|
||||
|
||||
fun getProfile(): LiveData<Profile> =
|
||||
profileDao.getProfile()
|
||||
fun getProfiles(): LiveData<List<Profile>> =
|
||||
profileDao.getProfiles()
|
||||
|
||||
fun insertProfile(profile: Profile) =
|
||||
profileDao.updateProfile(profile)
|
||||
profileDao.addProfile(profile)
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
|
@ -81,15 +81,15 @@ class AddViewModel(
|
||||
}
|
||||
|
||||
private fun setProfileValue() {
|
||||
val profile = profileRepository.getProfile()
|
||||
val profile = profileRepository.getProfiles().value?.first()
|
||||
|
||||
firstName.value = profile.value?.firstName
|
||||
lastName.value = profile.value?.lastName
|
||||
birthday.value = profile.value?.birthday
|
||||
birthPlace.value = profile.value?.birthPlace
|
||||
address.value = profile.value?.address
|
||||
city.value = profile.value?.city
|
||||
postalCode.value = profile.value?.postalCode
|
||||
firstName.value = profile?.firstName
|
||||
lastName.value = profile?.lastName
|
||||
birthday.value = profile?.birthday
|
||||
birthPlace.value = profile?.birthPlace
|
||||
address.value = profile?.address
|
||||
city.value = profile?.city
|
||||
postalCode.value = profile?.postalCode
|
||||
}
|
||||
|
||||
private fun setDateHourToday() {
|
||||
|
@ -0,0 +1,22 @@
|
||||
package fr.sanchezm.attestationsCovid19.utilities
|
||||
|
||||
import com.google.gson.Gson
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
import java.io.File
|
||||
|
||||
object DbMigration {
|
||||
|
||||
fun migrateProfileFrom1To2(filename: String): ArrayList<Profile> {
|
||||
val file = File(filename)
|
||||
val list = ArrayList<Profile>()
|
||||
|
||||
val profile: Profile = if (file.exists()) {
|
||||
Gson().fromJson(file.readText(Charsets.UTF_8), Profile::class.java)
|
||||
} else {
|
||||
Profile("", "", "", "", "", "", "")
|
||||
}
|
||||
list.add(profile)
|
||||
|
||||
return list
|
||||
}
|
||||
}
|
@ -39,11 +39,12 @@ object InjectorUtils {
|
||||
|
||||
private fun getAttestationRepo(context: Context): AttestationRepository =
|
||||
AttestationRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
|
||||
MyDatabase.invoke(context, getMyFilesDir(context))
|
||||
.attestationDao(getConfigRepo(context))
|
||||
)
|
||||
|
||||
private fun getProfileRepo(context: Context): ProfileRepository = ProfileRepository.getInstance(
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).profileDao()
|
||||
MyDatabase.invoke(context, getMyFilesDir(context)).profileDao(getConfigRepo(context))
|
||||
)
|
||||
|
||||
private fun getConfigRepo(context: Context): ConfigRepository = ConfigRepository.getInstance(
|
||||
|
@ -3,8 +3,7 @@
|
||||
buildscript {
|
||||
ext {
|
||||
kotlin_version = '1.3.72'
|
||||
db_version = '1'
|
||||
|
||||
db_version = '2'
|
||||
}
|
||||
|
||||
repositories {
|
||||
|
Loading…
Reference in New Issue
Block a user