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
|
||||
|
||||
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<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
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Profile> =
|
||||
profileDao.getProfile()
|
||||
|
||||
fun getProfile(): LiveData<Profile> = _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 }
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
addViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AddViewModel::class.java)
|
||||
}!!
|
||||
}
|
||||
}
|
||||
|
@ -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(),
|
||||
|
@ -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)
|
||||
val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) }
|
||||
attestationsViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
}!!
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user