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:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
|
tools:replace="android:allowBackup"
|
||||||
android:theme="@style/AppTheme.Light">
|
android:theme="@style/AppTheme.Light">
|
||||||
<activity android:name=".QrCodeActivity" />
|
<activity android:name=".QrCodeActivity" />
|
||||||
<activity android:name=".PdfViewerActivity" />
|
<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.AttestationDao
|
||||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ConfigDao
|
import fr.sanchezm.attestationsCovid19.data.db.dao.ConfigDao
|
||||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
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) {
|
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 _attestationDao: AttestationDao? = null
|
||||||
private var _configDao: ConfigDao? = 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 =
|
fun attestationDao(configRepository: ConfigRepository): AttestationDao =
|
||||||
_attestationDao ?: AttestationDao(savePath, filesPath).also { _attestationDao = it }
|
_attestationDao ?: AttestationDao(
|
||||||
|
savePath,
|
||||||
|
filesPath,
|
||||||
|
configRepository
|
||||||
|
).also { _attestationDao = it }
|
||||||
|
|
||||||
fun configDao(): ConfigDao = _configDao ?: ConfigDao(savePath).also { _configDao = 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.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||||
|
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||||
import fr.sanchezm.attestationsCovid19.utilities.PdfUtils
|
import fr.sanchezm.attestationsCovid19.utilities.PdfUtils
|
||||||
import java.io.File
|
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 _attestations = MutableLiveData<ArrayList<Attestation>>()
|
||||||
private val fileName = "attestation.db"
|
private val fileName = "attestation.db"
|
||||||
@ -23,9 +28,8 @@ class AttestationDao(private val savePath: String, private val filesPath: String
|
|||||||
_attestations.value?.elementAt(id)
|
_attestations.value?.elementAt(id)
|
||||||
|
|
||||||
fun addAttestation(attestation: Attestation) {
|
fun addAttestation(attestation: Attestation) {
|
||||||
_attestations.value = _attestations.value ?: ArrayList()
|
|
||||||
|
|
||||||
_attestations.value?.add(attestation)
|
_attestations.value?.add(attestation)
|
||||||
|
_attestations.value = _attestations.value
|
||||||
Log.d(TAG, "Add Attestation : $attestation")
|
Log.d(TAG, "Add Attestation : $attestation")
|
||||||
save()
|
save()
|
||||||
}
|
}
|
||||||
@ -66,6 +70,8 @@ class AttestationDao(private val savePath: String, private val filesPath: String
|
|||||||
|
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
_attestations.value = Gson().fromJson(file.readText(Charsets.UTF_8), type)
|
_attestations.value = Gson().fromJson(file.readText(Charsets.UTF_8), type)
|
||||||
|
} else {
|
||||||
|
_attestations.value = ArrayList()
|
||||||
}
|
}
|
||||||
_attestations.value?.forEach {
|
_attestations.value?.forEach {
|
||||||
val filePath = PdfUtils.getPath(filesPath, it.createAt)
|
val filePath = PdfUtils.getPath(filesPath, it.createAt)
|
||||||
|
@ -1,48 +1,69 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.data.db.dao
|
package fr.sanchezm.attestationsCovid19.data.db.dao
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import com.google.gson.Gson
|
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.db.entity.Profile
|
||||||
|
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
|
||||||
|
import fr.sanchezm.attestationsCovid19.utilities.DbMigration
|
||||||
import java.io.File
|
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 fileName = "profile.db"
|
||||||
|
private val type = object : TypeToken<ArrayList<Profile>>() {}.type
|
||||||
|
|
||||||
fun getProfile(): LiveData<Profile> =
|
@Suppress("UNCHECKED_CAST")
|
||||||
_profile
|
fun getProfiles(): LiveData<List<Profile>> =
|
||||||
|
_profile as LiveData<List<Profile>>
|
||||||
|
|
||||||
fun updateProfile(profile: Profile) {
|
fun addProfile(profile: Profile) {
|
||||||
_profile.value = profile
|
_profile.value?.add(profile)
|
||||||
|
_profile.value = _profile.value
|
||||||
save()
|
save()
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
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()
|
load()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
load()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun load() {
|
private fun load() {
|
||||||
val file = File("$path/$fileName")
|
val file = File("$path/$fileName")
|
||||||
|
|
||||||
if (file.exists()) {
|
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 {
|
} else {
|
||||||
_profile.value = Profile("", "", "", "", "", "", "")
|
_profile.value = ArrayList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun save() {
|
private fun save() {
|
||||||
File("$path/$fileName").writeText(Gson().toJson(_profile.value))
|
File("$path/$fileName").writeText(Gson().toJson(_profile.value))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//@Dao
|
companion object {
|
||||||
//interface ProfileDao {
|
private val TAG = ProfileDao::class.simpleName
|
||||||
// @Insert(onConflict = OnConflictStrategy.REPLACE)
|
}
|
||||||
// fun insert(profile: Profile)
|
}
|
||||||
//
|
|
||||||
// @Query(value = "SELECT * FROM profile WHERE id = $CURRENT_PROFILE_ID")
|
|
||||||
// fun getProfile(): LiveData<Profile>
|
|
||||||
//}
|
|
||||||
|
@ -5,7 +5,6 @@ import java.util.*
|
|||||||
|
|
||||||
const val PATTERN = "dd/MM/yyyy 'a' HH'h'mm"
|
const val PATTERN = "dd/MM/yyyy 'a' HH'h'mm"
|
||||||
|
|
||||||
//@Entity(tableName = "attestation")
|
|
||||||
data class Attestation(
|
data class Attestation(
|
||||||
val profile: Profile,
|
val profile: Profile,
|
||||||
val exitDate: String,
|
val exitDate: String,
|
||||||
|
@ -1,7 +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,
|
var dbVersion: Int,
|
||||||
var versionName: String,
|
var versionName: String,
|
||||||
var versionCode: Int
|
var versionCode: Int
|
||||||
)
|
)
|
@ -1,6 +1,5 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||||
|
|
||||||
//@Entity(tableName = "profile")
|
|
||||||
data class Profile(
|
data class Profile(
|
||||||
val firstName: String,
|
val firstName: String,
|
||||||
val lastName: String,
|
val lastName: String,
|
||||||
@ -10,8 +9,6 @@ data class Profile(
|
|||||||
val city: String,
|
val city: String,
|
||||||
val postalCode: String
|
val postalCode: String
|
||||||
) {
|
) {
|
||||||
// @PrimaryKey
|
|
||||||
// var id: Int = CURRENT_PROFILE_ID
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Nom: $lastName; Prenom: $firstName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city"
|
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) {
|
class ProfileRepository private constructor(private val profileDao: ProfileDao) {
|
||||||
|
|
||||||
fun getProfile(): LiveData<Profile> =
|
fun getProfiles(): LiveData<List<Profile>> =
|
||||||
profileDao.getProfile()
|
profileDao.getProfiles()
|
||||||
|
|
||||||
fun insertProfile(profile: Profile) =
|
fun insertProfile(profile: Profile) =
|
||||||
profileDao.updateProfile(profile)
|
profileDao.addProfile(profile)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Volatile
|
@Volatile
|
||||||
|
@ -81,15 +81,15 @@ class AddViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setProfileValue() {
|
private fun setProfileValue() {
|
||||||
val profile = profileRepository.getProfile()
|
val profile = profileRepository.getProfiles().value?.first()
|
||||||
|
|
||||||
firstName.value = profile.value?.firstName
|
firstName.value = profile?.firstName
|
||||||
lastName.value = profile.value?.lastName
|
lastName.value = profile?.lastName
|
||||||
birthday.value = profile.value?.birthday
|
birthday.value = profile?.birthday
|
||||||
birthPlace.value = profile.value?.birthPlace
|
birthPlace.value = profile?.birthPlace
|
||||||
address.value = profile.value?.address
|
address.value = profile?.address
|
||||||
city.value = profile.value?.city
|
city.value = profile?.city
|
||||||
postalCode.value = profile.value?.postalCode
|
postalCode.value = profile?.postalCode
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setDateHourToday() {
|
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 =
|
private fun getAttestationRepo(context: Context): AttestationRepository =
|
||||||
AttestationRepository.getInstance(
|
AttestationRepository.getInstance(
|
||||||
MyDatabase.invoke(context, getMyFilesDir(context)).attestationDao()
|
MyDatabase.invoke(context, getMyFilesDir(context))
|
||||||
|
.attestationDao(getConfigRepo(context))
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun getProfileRepo(context: Context): ProfileRepository = ProfileRepository.getInstance(
|
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(
|
private fun getConfigRepo(context: Context): ConfigRepository = ConfigRepository.getInstance(
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
ext {
|
ext {
|
||||||
kotlin_version = '1.3.72'
|
kotlin_version = '1.3.72'
|
||||||
db_version = '1'
|
db_version = '2'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
Loading…
Reference in New Issue
Block a user