diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 306b486..dbe28f1 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -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">
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt
index 28ff7ed..87f2523 100644
--- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt
@@ -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()
- }
-}
-*/
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 417f7ef..86fec2e 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
@@ -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>()
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)
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 c152df5..7f1e4ac 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
@@ -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()
+ private val _profile = MutableLiveData>()
private val fileName = "profile.db"
+ private val type = object : TypeToken>() {}.type
- fun getProfile(): LiveData =
- _profile
+ @Suppress("UNCHECKED_CAST")
+ fun getProfiles(): LiveData> =
+ _profile as LiveData>
- 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
-//}
+ companion object {
+ private val TAG = ProfileDao::class.simpleName
+ }
+}
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt
index 5971e30..bccc7d1 100644
--- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt
@@ -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,
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
index 3f479d2..aa0344d 100644
--- 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
@@ -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
)
\ No newline at end of file
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt
index 6d3ae1d..b224b19 100644
--- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt
@@ -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"
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ProfileRepository.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ProfileRepository.kt
index ab40224..8b45668 100644
--- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ProfileRepository.kt
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/ProfileRepository.kt
@@ -6,11 +6,11 @@ import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
class ProfileRepository private constructor(private val profileDao: ProfileDao) {
- fun getProfile(): LiveData =
- profileDao.getProfile()
+ fun getProfiles(): LiveData> =
+ profileDao.getProfiles()
fun insertProfile(profile: Profile) =
- profileDao.updateProfile(profile)
+ profileDao.addProfile(profile)
companion object {
@Volatile
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt
index 2ad4498..b195b5e 100644
--- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt
@@ -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() {
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/DbMigration.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/DbMigration.kt
new file mode 100644
index 0000000..6d5b41b
--- /dev/null
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/DbMigration.kt
@@ -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 {
+ val file = File(filename)
+ val list = ArrayList()
+
+ val profile: Profile = if (file.exists()) {
+ Gson().fromJson(file.readText(Charsets.UTF_8), Profile::class.java)
+ } else {
+ Profile("", "", "", "", "", "", "")
+ }
+ list.add(profile)
+
+ return list
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt
index 9e0f85d..e8d5df9 100644
--- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt
+++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt
@@ -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(
diff --git a/build.gradle b/build.gradle
index 6c8070b..1d6f210 100644
--- a/build.gradle
+++ b/build.gradle
@@ -3,8 +3,7 @@
buildscript {
ext {
kotlin_version = '1.3.72'
- db_version = '1'
-
+ db_version = '2'
}
repositories {