Data binding and Room for storage not working yet
This commit is contained in:
@ -4,25 +4,41 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.databinding.FragmentAddAttestationBinding
|
||||
import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils
|
||||
|
||||
class AddFragment : Fragment() {
|
||||
|
||||
private lateinit var homeViewModel: AddViewModel
|
||||
private lateinit var addViewModel: AddViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
homeViewModel =
|
||||
ViewModelProviders.of(this).get(AddViewModel::class.java)
|
||||
val root = inflater.inflate(R.layout.fragment_add_attestation, container, false)
|
||||
homeViewModel.text.observe(viewLifecycleOwner, Observer {
|
||||
})
|
||||
return root
|
||||
initializeUi()
|
||||
val binding = DataBindingUtil.inflate<FragmentAddAttestationBinding>(
|
||||
inflater,
|
||||
R.layout.fragment_add_attestation,
|
||||
container,
|
||||
false
|
||||
).apply {
|
||||
this.lifecycleOwner = this@AddFragment
|
||||
this.viewModel = addViewModel
|
||||
}
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
addViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AddViewModel::class.java)
|
||||
}!!
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,69 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
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 : ViewModel() {
|
||||
class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "Fragment ajouter une attestation"
|
||||
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")
|
||||
}
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AddViewModelFactory(private val profileRepository: ProfileRepository) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AddViewModel(profileRepository) as T
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user