Generator-Attestation-Covid-19/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt

69 lines
2.3 KiB
Kotlin

package fr.sanchezm.attestationsCovid19.ui.add
import android.annotation.SuppressLint
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel() {
val firstName = MutableLiveData<String>()
val lastName = MutableLiveData<String>()
val birthday = MutableLiveData<String>()
val birthPlace = MutableLiveData<String>()
val address = MutableLiveData<String>()
val city = MutableLiveData<String>()
val postalCode = MutableLiveData<String>()
val exitDate = MutableLiveData<String>()
val exitHour = MutableLiveData<String>()
@SuppressLint("LongLogTag")
fun onGenerateAttestationClick() {
if (checkAllValue()) {
profileRepository.insertProfile(getProfileFromView())
} else {
Log.e("onGenerateAttestationClick", "Cannot add profile")
}
}
init {
setProfileValue()
}
private fun setProfileValue() {
val profile = profileRepository.getProfile()
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
}
private fun getProfileFromView(): Profile {
return Profile(
firstName.value.toString(),
lastName.value.toString(),
birthday.value.toString(),
birthPlace.value.toString(),
address.value.toString(),
city.value.toString(),
postalCode.value.toString()
)
}
private fun checkAllValue(): Boolean {
return !firstName.value.isNullOrEmpty()
&& !lastName.value.isNullOrEmpty()
&& !birthday.value.isNullOrEmpty()
&& !birthPlace.value.isNullOrEmpty()
&& !address.value.isNullOrEmpty()
&& !city.value.isNullOrEmpty()
&& !postalCode.value.isNullOrEmpty()
}
}