From c88adaef0bc93c501051626a231ec34f3493c485 Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Tue, 14 Apr 2020 18:40:09 +0200 Subject: [PATCH] Getting database from room to Json File Add date and time automatically --- .../data/db/dao/AttestationDao.kt | 37 +++++++++++++++++++ .../data/db/dao/ProfileDao.kt | 4 +- .../data/repository/AttestationRepository.kt | 17 ++------- .../data/repository/ProfileRepository.kt | 27 ++++---------- .../attestationsCovid19/ui/add/AddFragment.kt | 14 +++---- .../ui/add/AddViewModel.kt | 23 +++++++++++- .../ui/attestations/AttestationsFragment.kt | 13 +++---- .../utilities/InjectorUtils.kt | 10 +---- 8 files changed, 85 insertions(+), 60 deletions(-) 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 b5bafe5..8ea0a27 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 @@ -1,7 +1,44 @@ package fr.sanchezm.attestationsCovid19.data.db.dao +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation +import java.io.File + class AttestationDao(private val path: String) { + private var _attestations = MutableLiveData>() + private var fileName = "attestation.db" + private val type = object : TypeToken>() {}.type + + fun getAttestations() : LiveData> = + _attestations + + fun getAttestation(id: Int) : Attestation? = + _attestations.value?.elementAt(id) + + fun addAttestation(attestation: Attestation) { + _attestations.value?.add(attestation) + } + + init { + load() + } + + private fun load() { + val file = File("$path/$fileName") + + if (file.exists()) { + _attestations.value = Gson().fromJson(file.readText(Charsets.UTF_8), type) + } + } + + private fun save() { + File("$path/$fileName").writeText(Gson().toJson(_attestations.value)) + } + } //@Dao 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 e4617db..d285a5d 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 @@ -28,11 +28,13 @@ class ProfileDao(private val path: String) { if (file.exists()) { _profile.value = Gson().fromJson(file.readText(Charsets.UTF_8), Profile::class.java) + } else { + _profile.value = Profile("", "", "", "", "", "", "") } } private fun save() { - File("$path/$fileName").writeText(Gson().toJson(this)) + File("$path/$fileName").writeText(Gson().toJson(_profile.value)) } } diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt index 724bdb4..caaf8d6 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt @@ -1,27 +1,16 @@ package fr.sanchezm.attestationsCovid19.data.repository import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao -import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao -import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation -import fr.sanchezm.attestationsCovid19.data.db.entity.Profile -class AttestationRepository private constructor(/*private val attestationDao: AttestationDao*/) { - -// fun getAttestation() = attestationDao.getAttestation() - - fun createAttestation() { - - } -// fun insertAttestation(attestation: Attestation) = attestationDao.insert(attestation) +class AttestationRepository private constructor(private val attestationDao: AttestationDao) { companion object { @Volatile private var instance: AttestationRepository? = null - fun getInstance(/*attestationDao: AttestationDao*/) = + fun getInstance(attestationDao: AttestationDao) = instance ?: synchronized(this) { -// instance ?: AttestationRepository(attestationDao).also { instance = it } - instance ?: AttestationRepository().also { instance = it } + instance ?: AttestationRepository(attestationDao).also { instance = it } } } } \ No newline at end of file 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 7e5cb14..ab40224 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 @@ -1,35 +1,24 @@ package fr.sanchezm.attestationsCovid19.data.repository import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData +import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao import fr.sanchezm.attestationsCovid19.data.db.entity.Profile -class ProfileRepository private constructor(/*private val profileDao: ProfileDao*/) { +class ProfileRepository private constructor(private val profileDao: ProfileDao) { - private var _profile = MutableLiveData(Profile( - "Mathieu", - "Sanchez", - "19/11/1997", - "Perpignan", - "33 Rue Salvador Dali", - "Canohes", - "66680" - )) + fun getProfile(): LiveData = + profileDao.getProfile() - fun getProfile(): LiveData = _profile - - // fun insertProfile(profile: Profile) = profileDao.insert(profile) - fun insertProfile(profile: Profile) { - _profile.value = profile - } + fun insertProfile(profile: Profile) = + profileDao.updateProfile(profile) companion object { @Volatile private var instance: ProfileRepository? = null - fun getInstance(/*profileDao: ProfileDao*/) = + fun getInstance(profileDao: ProfileDao) = instance ?: synchronized(this) { - instance ?: ProfileRepository(/*profileDao*/).also { instance = it } + instance ?: ProfileRepository(profileDao).also { instance = it } } } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt index f65e529..e2bfd50 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt @@ -1,7 +1,6 @@ package fr.sanchezm.attestationsCovid19.ui.add import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -47,13 +46,10 @@ class AddFragment : Fragment() { } private fun initializeUi() { -// val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) } -// addViewModel = factory?.let { -// ViewModelProvider(this, it) -// .get(AddViewModel::class.java) -// }!! - val factory = InjectorUtils.provideAddViewModelFactory() - addViewModel = ViewModelProvider(this, factory) - .get(AddViewModel::class.java) + val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) } + addViewModel = factory?.let { + ViewModelProvider(this, it) + .get(AddViewModel::class.java) + }!! } } 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 57c39bc..d4bcb14 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 @@ -1,6 +1,7 @@ package fr.sanchezm.attestationsCovid19.ui.add import android.annotation.SuppressLint +import android.os.Build import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData @@ -10,6 +11,10 @@ import fr.sanchezm.attestationsCovid19.data.db.entity.Profile import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository import fr.sanchezm.attestationsCovid19.utilities.Event +import java.text.SimpleDateFormat +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +import java.util.* class AddViewModel( private val profileRepository: ProfileRepository, @@ -39,11 +44,13 @@ class AddViewModel( val reason6 = MutableLiveData(false) val reason7 = MutableLiveData(false) + private val datePattern = "dd/MM/yyyy" + private val timePattern = "HH:mm" + @SuppressLint("LongLogTag") fun onGenerateAttestationClick() { if (checkAllValue()) { profileRepository.insertProfile(getProfileFromView()) - attestationRepository.createAttestation() } else { _errorMessage.value = Event(R.string.error_cannot_create_attestation) Log.e("onGenerateAttestationClick", "Cannot generate Attestation") @@ -52,6 +59,7 @@ class AddViewModel( init { setProfileValue() + setDateHourToday() } private fun setProfileValue() { @@ -66,6 +74,19 @@ class AddViewModel( postalCode.value = profile.value?.postalCode } + private fun setDateHourToday() { + exitDate.value = getDateOrTimeFromPattern(datePattern) + exitHour.value = getDateOrTimeFromPattern(timePattern) + } + + private fun getDateOrTimeFromPattern(pattern: String): String { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.now()) + } else { + SimpleDateFormat(pattern, Locale.FRANCE).format(Date()) + } + } + private fun getProfileFromView(): Profile { return Profile( firstName.value.toString(), diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt index 15e3740..1c4336c 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt @@ -35,13 +35,10 @@ class AttestationsFragment : Fragment() { } private fun initializeUi() { -// val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) } -// attestationsViewModel = factory?.let { -// ViewModelProvider(this, it) -// .get(AttestationsViewModel::class.java) -// }!! - val factory = InjectorUtils.provideAttestationViewModel() - attestationsViewModel = ViewModelProvider(this, factory) - .get(AttestationsViewModel::class.java) + val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) } + attestationsViewModel = factory?.let { + ViewModelProvider(this, it) + .get(AttestationsViewModel::class.java) + }!! } } \ 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 1e1e0e4..f825417 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt @@ -10,10 +10,6 @@ import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFact object InjectorUtils { fun provideAddViewModelFactory(context: Context): AddViewModelFactory { -// val profileRepository = -// ProfileRepository.getInstance() -// val attestationRepository = -// AttestationRepository.getInstance() val profileRepository = ProfileRepository.getInstance(MyDatabase.invoke(context).profileDao()) val attestationRepository = @@ -22,11 +18,9 @@ object InjectorUtils { return AddViewModelFactory(profileRepository, attestationRepository) } - fun provideAttestationViewModel(/*context: Context*/): AttestationsViewModelFactory { + fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory { val attestationRepository = - AttestationRepository.getInstance() -// val attestationRepository = -// AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao()) + AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao()) return AttestationsViewModelFactory(attestationRepository) }