Getting database from room to Json File
Add date and time automatically
This commit is contained in:
parent
dd91303137
commit
c88adaef0b
@ -1,7 +1,44 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.data.db.dao
|
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) {
|
class AttestationDao(private val path: String) {
|
||||||
|
|
||||||
|
private var _attestations = MutableLiveData<MutableCollection<Attestation>>()
|
||||||
|
private var fileName = "attestation.db"
|
||||||
|
private val type = object : TypeToken<ArrayList<Attestation>>() {}.type
|
||||||
|
|
||||||
|
fun getAttestations() : LiveData<MutableCollection<Attestation>> =
|
||||||
|
_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
|
//@Dao
|
||||||
|
@ -28,11 +28,13 @@ class ProfileDao(private val path: String) {
|
|||||||
|
|
||||||
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), Profile::class.java)
|
||||||
|
} else {
|
||||||
|
_profile.value = Profile("", "", "", "", "", "", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun save() {
|
private fun save() {
|
||||||
File("$path/$fileName").writeText(Gson().toJson(this))
|
File("$path/$fileName").writeText(Gson().toJson(_profile.value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,27 +1,16 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.data.repository
|
package fr.sanchezm.attestationsCovid19.data.repository
|
||||||
|
|
||||||
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
|
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*/) {
|
class AttestationRepository private constructor(private val attestationDao: AttestationDao) {
|
||||||
|
|
||||||
// fun getAttestation() = attestationDao.getAttestation()
|
|
||||||
|
|
||||||
fun createAttestation() {
|
|
||||||
|
|
||||||
}
|
|
||||||
// fun insertAttestation(attestation: Attestation) = attestationDao.insert(attestation)
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Volatile
|
@Volatile
|
||||||
private var instance: AttestationRepository? = null
|
private var instance: AttestationRepository? = null
|
||||||
|
|
||||||
fun getInstance(/*attestationDao: AttestationDao*/) =
|
fun getInstance(attestationDao: AttestationDao) =
|
||||||
instance ?: synchronized(this) {
|
instance ?: synchronized(this) {
|
||||||
// instance ?: AttestationRepository(attestationDao).also { instance = it }
|
instance ?: AttestationRepository(attestationDao).also { instance = it }
|
||||||
instance ?: AttestationRepository().also { instance = it }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,35 +1,24 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.data.repository
|
package fr.sanchezm.attestationsCovid19.data.repository
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
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(
|
fun getProfile(): LiveData<Profile> =
|
||||||
"Mathieu",
|
profileDao.getProfile()
|
||||||
"Sanchez",
|
|
||||||
"19/11/1997",
|
|
||||||
"Perpignan",
|
|
||||||
"33 Rue Salvador Dali",
|
|
||||||
"Canohes",
|
|
||||||
"66680"
|
|
||||||
))
|
|
||||||
|
|
||||||
fun getProfile(): LiveData<Profile> = _profile
|
fun insertProfile(profile: Profile) =
|
||||||
|
profileDao.updateProfile(profile)
|
||||||
// fun insertProfile(profile: Profile) = profileDao.insert(profile)
|
|
||||||
fun insertProfile(profile: Profile) {
|
|
||||||
_profile.value = profile
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Volatile
|
@Volatile
|
||||||
private var instance: ProfileRepository? = null
|
private var instance: ProfileRepository? = null
|
||||||
|
|
||||||
fun getInstance(/*profileDao: ProfileDao*/) =
|
fun getInstance(profileDao: ProfileDao) =
|
||||||
instance ?: synchronized(this) {
|
instance ?: synchronized(this) {
|
||||||
instance ?: ProfileRepository(/*profileDao*/).also { instance = it }
|
instance ?: ProfileRepository(profileDao).also { instance = it }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.ui.add
|
package fr.sanchezm.attestationsCovid19.ui.add
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@ -47,13 +46,10 @@ class AddFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeUi() {
|
private fun initializeUi() {
|
||||||
// val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||||
// addViewModel = factory?.let {
|
addViewModel = factory?.let {
|
||||||
// ViewModelProvider(this, it)
|
ViewModelProvider(this, it)
|
||||||
// .get(AddViewModel::class.java)
|
.get(AddViewModel::class.java)
|
||||||
// }!!
|
}!!
|
||||||
val factory = InjectorUtils.provideAddViewModelFactory()
|
|
||||||
addViewModel = ViewModelProvider(this, factory)
|
|
||||||
.get(AddViewModel::class.java)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.ui.add
|
package fr.sanchezm.attestationsCovid19.ui.add
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
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.AttestationRepository
|
||||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||||
import fr.sanchezm.attestationsCovid19.utilities.Event
|
import fr.sanchezm.attestationsCovid19.utilities.Event
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class AddViewModel(
|
class AddViewModel(
|
||||||
private val profileRepository: ProfileRepository,
|
private val profileRepository: ProfileRepository,
|
||||||
@ -39,11 +44,13 @@ class AddViewModel(
|
|||||||
val reason6 = MutableLiveData(false)
|
val reason6 = MutableLiveData(false)
|
||||||
val reason7 = MutableLiveData(false)
|
val reason7 = MutableLiveData(false)
|
||||||
|
|
||||||
|
private val datePattern = "dd/MM/yyyy"
|
||||||
|
private val timePattern = "HH:mm"
|
||||||
|
|
||||||
@SuppressLint("LongLogTag")
|
@SuppressLint("LongLogTag")
|
||||||
fun onGenerateAttestationClick() {
|
fun onGenerateAttestationClick() {
|
||||||
if (checkAllValue()) {
|
if (checkAllValue()) {
|
||||||
profileRepository.insertProfile(getProfileFromView())
|
profileRepository.insertProfile(getProfileFromView())
|
||||||
attestationRepository.createAttestation()
|
|
||||||
} else {
|
} else {
|
||||||
_errorMessage.value = Event(R.string.error_cannot_create_attestation)
|
_errorMessage.value = Event(R.string.error_cannot_create_attestation)
|
||||||
Log.e("onGenerateAttestationClick", "Cannot generate Attestation")
|
Log.e("onGenerateAttestationClick", "Cannot generate Attestation")
|
||||||
@ -52,6 +59,7 @@ class AddViewModel(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
setProfileValue()
|
setProfileValue()
|
||||||
|
setDateHourToday()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setProfileValue() {
|
private fun setProfileValue() {
|
||||||
@ -66,6 +74,19 @@ class AddViewModel(
|
|||||||
postalCode.value = profile.value?.postalCode
|
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 {
|
private fun getProfileFromView(): Profile {
|
||||||
return Profile(
|
return Profile(
|
||||||
firstName.value.toString(),
|
firstName.value.toString(),
|
||||||
|
@ -35,13 +35,10 @@ class AttestationsFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeUi() {
|
private fun initializeUi() {
|
||||||
// val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) }
|
val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) }
|
||||||
// attestationsViewModel = factory?.let {
|
attestationsViewModel = factory?.let {
|
||||||
// ViewModelProvider(this, it)
|
ViewModelProvider(this, it)
|
||||||
// .get(AttestationsViewModel::class.java)
|
.get(AttestationsViewModel::class.java)
|
||||||
// }!!
|
}!!
|
||||||
val factory = InjectorUtils.provideAttestationViewModel()
|
|
||||||
attestationsViewModel = ViewModelProvider(this, factory)
|
|
||||||
.get(AttestationsViewModel::class.java)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,10 +10,6 @@ import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFact
|
|||||||
object InjectorUtils {
|
object InjectorUtils {
|
||||||
|
|
||||||
fun provideAddViewModelFactory(context: Context): AddViewModelFactory {
|
fun provideAddViewModelFactory(context: Context): AddViewModelFactory {
|
||||||
// val profileRepository =
|
|
||||||
// ProfileRepository.getInstance()
|
|
||||||
// val attestationRepository =
|
|
||||||
// AttestationRepository.getInstance()
|
|
||||||
val profileRepository =
|
val profileRepository =
|
||||||
ProfileRepository.getInstance(MyDatabase.invoke(context).profileDao())
|
ProfileRepository.getInstance(MyDatabase.invoke(context).profileDao())
|
||||||
val attestationRepository =
|
val attestationRepository =
|
||||||
@ -22,11 +18,9 @@ object InjectorUtils {
|
|||||||
return AddViewModelFactory(profileRepository, attestationRepository)
|
return AddViewModelFactory(profileRepository, attestationRepository)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun provideAttestationViewModel(/*context: Context*/): AttestationsViewModelFactory {
|
fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory {
|
||||||
val attestationRepository =
|
val attestationRepository =
|
||||||
AttestationRepository.getInstance()
|
AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
|
||||||
// val attestationRepository =
|
|
||||||
// AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
|
|
||||||
|
|
||||||
return AttestationsViewModelFactory(attestationRepository)
|
return AttestationsViewModelFactory(attestationRepository)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user