Getting database from room to Json File
Add date and time automatically
This commit is contained in:
@ -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)
|
||||
}!!
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) }
|
||||
attestationsViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
}!!
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user