Add Info Page
This commit is contained in:
@ -1,12 +1,15 @@
|
||||
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
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.databinding.FragmentAddAttestationBinding
|
||||
import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils
|
||||
@ -31,14 +34,26 @@ class AddFragment : Fragment() {
|
||||
this.viewModel = addViewModel
|
||||
}
|
||||
|
||||
bindMessage(binding.context)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun bindMessage(context: View) {
|
||||
addViewModel.errorMessage.observe(viewLifecycleOwner, Observer {
|
||||
it.getContentIfNotHandled()?.let { stringId ->
|
||||
Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT ).show()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
addViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AddViewModel::class.java)
|
||||
}!!
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,24 @@ package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
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
|
||||
|
||||
class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel() {
|
||||
class AddViewModel(
|
||||
private val profileRepository: ProfileRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) : ViewModel() {
|
||||
|
||||
private val _errorMessage = MutableLiveData<Event<Int>>()
|
||||
|
||||
val errorMessage: LiveData<Event<Int>>
|
||||
get() = _errorMessage
|
||||
|
||||
val firstName = MutableLiveData<String>()
|
||||
val lastName = MutableLiveData<String>()
|
||||
@ -19,12 +31,22 @@ class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel
|
||||
val exitDate = MutableLiveData<String>()
|
||||
val exitHour = MutableLiveData<String>()
|
||||
|
||||
val reason1 = MutableLiveData(false)
|
||||
val reason2 = MutableLiveData(false)
|
||||
val reason3 = MutableLiveData(false)
|
||||
val reason4 = MutableLiveData(false)
|
||||
val reason5 = MutableLiveData(false)
|
||||
val reason6 = MutableLiveData(false)
|
||||
val reason7 = MutableLiveData(false)
|
||||
|
||||
@SuppressLint("LongLogTag")
|
||||
fun onGenerateAttestationClick() {
|
||||
if (checkAllValue()) {
|
||||
profileRepository.insertProfile(getProfileFromView())
|
||||
attestationRepository.createAttestation()
|
||||
} else {
|
||||
Log.e("onGenerateAttestationClick", "Cannot add profile")
|
||||
_errorMessage.value = Event(R.string.error_cannot_create_attestation)
|
||||
Log.e("onGenerateAttestationClick", "Cannot generate Attestation")
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,6 +86,19 @@ class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel
|
||||
&& !address.value.isNullOrEmpty()
|
||||
&& !city.value.isNullOrEmpty()
|
||||
&& !postalCode.value.isNullOrEmpty()
|
||||
&& !exitDate.value.isNullOrEmpty()
|
||||
&& !exitHour.value.isNullOrEmpty()
|
||||
&& checkAtLeastOneReasonSelected()
|
||||
}
|
||||
|
||||
private fun checkAtLeastOneReasonSelected(): Boolean {
|
||||
return reason1.value!!
|
||||
|| reason2.value!!
|
||||
|| reason3.value!!
|
||||
|| reason4.value!!
|
||||
|| reason5.value!!
|
||||
|| reason6.value!!
|
||||
|| reason7.value!!
|
||||
}
|
||||
|
||||
}
|
@ -2,13 +2,17 @@ package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AddViewModelFactory(private val profileRepository: ProfileRepository) :
|
||||
class AddViewModelFactory(
|
||||
private val profileRepository: ProfileRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AddViewModel(profileRepository) as T
|
||||
return AddViewModel(profileRepository, attestationRepository) as T
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ class AttestationsFragment : Fragment() {
|
||||
initializeUi()
|
||||
val binding = DataBindingUtil.inflate<FragmentAttestationsBinding>(
|
||||
inflater,
|
||||
R.layout.fragment_add_attestation,
|
||||
R.layout.fragment_attestations,
|
||||
container,
|
||||
false
|
||||
).apply {
|
||||
@ -35,10 +35,13 @@ class AttestationsFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
attestationsViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
}!!
|
||||
// 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)
|
||||
}
|
||||
}
|
@ -1,14 +1,12 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.attestations
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
|
||||
class AttestationsViewModel(private val attestationRepository: AttestationRepository) : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is dashboard Fragment"
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
// private val _text = MutableLiveData<String>().apply {
|
||||
// value = "This is dashboard Fragment"
|
||||
// }
|
||||
// val text: LiveData<String> = _text
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.info
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.Fragment
|
||||
import fr.sanchezm.attestationsCovid19.BuildConfig
|
||||
|
||||
import fr.sanchezm.attestationsCovid19.R;
|
||||
|
||||
class InfoFragment : Fragment() {
|
||||
|
||||
override fun onCreateView(
|
||||
inflater:LayoutInflater,
|
||||
container:ViewGroup?,
|
||||
savedInstanceState:Bundle?
|
||||
): View? {
|
||||
val root = inflater.inflate(R.layout.fragment_info, container, false)
|
||||
|
||||
root.findViewById<TextView>(R.id.develop_by).movementMethod = LinkMovementMethod.getInstance()
|
||||
root.findViewById<TextView>(R.id.version).text = getVersionText()
|
||||
return root
|
||||
}
|
||||
|
||||
private fun getVersionText(): String {
|
||||
val versionText = getString(R.string.version_number);
|
||||
val versionName = BuildConfig.VERSION_NAME
|
||||
|
||||
return "$versionText $versionName"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user