diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f847135..21022a2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,8 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme.Light"> + + - Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT ).show() + Snackbar.make(ctx, stringId, Snackbar.LENGTH_SHORT).show() } }) addViewModel.dataMessage.observe(viewLifecycleOwner, Observer { it.getContentIfNotHandled()?.let { stringId -> - Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT).show() + Snackbar.make(ctx, stringId, Snackbar.LENGTH_SHORT).show() + } + }) + + addViewModel.startActivity.observe(viewLifecycleOwner, Observer { + it.getContentIfNotHandled()?.let { + val intent = Intent(context, QrCodeActivity::class.java).apply { + putExtra("createAt", it) + putExtra("toCreate", true) + } + startActivity(intent) } }) } diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt index 9fe61e4..ca22aa9 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt @@ -24,6 +24,7 @@ class AddViewModel( private val _errorMessage = MutableLiveData>() private val _dataMessage = MutableLiveData>() + private val _startActivity = MutableLiveData>() val errorMessage: LiveData> get() = _errorMessage @@ -31,6 +32,10 @@ class AddViewModel( val dataMessage: LiveData> get() = _dataMessage + val startActivity: LiveData> + get() = _startActivity + + // region ViewField val firstName = MutableLiveData() val lastName = MutableLiveData() val birthday = MutableLiveData() @@ -48,6 +53,7 @@ class AddViewModel( val reason5 = MutableLiveData(false) val reason6 = MutableLiveData(false) val reason7 = MutableLiveData(false) + // endregion private val datePattern = "dd/MM/yyyy" private val timePattern = "HH'h'mm" @@ -56,10 +62,13 @@ class AddViewModel( fun onGenerateAttestationClick() { if (checkAllValue()) { val attestation = getAttestation() + profileRepository.insertProfile(getProfileFromView()) - attestationRepository.generateAttestation(attestation) attestationRepository.addAttestation(attestation) - _dataMessage.value = Event(R.string.attestation_generated) + + _startActivity.value = Event(attestation.createAt) +// attestationRepository.generateAttestation(attestation) +// _dataMessage.value = Event(R.string.attestation_generated) } else { _errorMessage.value = Event(R.string.error_cannot_create_attestation) Log.e("onGenerateAttestationClick", "Cannot generate Attestation") @@ -113,7 +122,7 @@ class AddViewModel( getProfileFromView(), exitDate.value.toString(), exitHour.value.toString(), - 0, + Date().time, getReasons() ) } diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt index eb22c7d..3ab0a65 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt @@ -6,17 +6,22 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import fr.sanchezm.attestationsCovid19.R import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation +import fr.sanchezm.attestationsCovid19.interfaces.ItemClickListener import kotlinx.android.synthetic.main.attestation_item.view.* -class AttestationsAdapter(private val items: ArrayList) : - RecyclerView.Adapter() { +class AttestationsAdapter( + private val items: ArrayList, + private val itemClickListener: ItemClickListener +) : RecyclerView.Adapter() { override fun getItemCount(): Int { return items.size } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = - ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.attestation_item, parent, false)) + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder( + LayoutInflater.from(parent.context).inflate(R.layout.attestation_item, parent, false), + itemClickListener + ) override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.bind(items[(items.size - 1) - position]) @@ -28,7 +33,8 @@ class AttestationsAdapter(private val items: ArrayList) : notifyDataSetChanged() } - class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + class ViewHolder(private var view: View, private val itemClickListener: ItemClickListener) : + RecyclerView.ViewHolder(view) { private val peopleItem = view.people_item_data private val dateItem = view.date_item_data @@ -38,8 +44,10 @@ class AttestationsAdapter(private val items: ArrayList) : peopleItem.text = getName(attestation) dateItem.text = attestation.getCreatedAtFormatedDate() reasonItem.text = attestation.getMotifsText() + view.setOnClickListener { itemClickListener.onItemClick(attestation) } } - private fun getName(attestation: Attestation) = "${attestation.profile.firstName} ${attestation.profile.lastName}" + private fun getName(attestation: Attestation) = + "${attestation.profile.firstName} ${attestation.profile.lastName}" } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt index b01caae..4fad307 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt @@ -12,14 +12,24 @@ import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import fr.sanchezm.attestationsCovid19.PdfViewerActivity import fr.sanchezm.attestationsCovid19.R +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.databinding.FragmentAttestationsBinding +import fr.sanchezm.attestationsCovid19.interfaces.ItemClickListener import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils import kotlinx.android.synthetic.main.fragment_attestations.view.* class AttestationsFragment : Fragment() { private lateinit var attestationsViewModel: AttestationsViewModel - private val attestationsAdapter = AttestationsAdapter(arrayListOf()) + private val attestationsAdapter = AttestationsAdapter(arrayListOf(), + object : ItemClickListener { + override fun onItemClick(item: Attestation) { + val intent = Intent(context, PdfViewerActivity::class.java).apply { + putExtra("createAt", item.createAt) + } + startActivity(intent) + } + }) override fun onCreateView( inflater: LayoutInflater, diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt index 53ca775..8177937 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt @@ -7,11 +7,9 @@ import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.utilities.Event -class AttestationsViewModel(private val attestationRepository: AttestationRepository) : ViewModel() { +class AttestationsViewModel(attestationRepository: AttestationRepository) : ViewModel() { - private var attestationCreateAt: Long private val _startActivity = MutableLiveData>() - private val _attestationName = MutableLiveData() private var _attestations = attestationRepository.getAttestations() as MutableLiveData> @@ -22,8 +20,6 @@ class AttestationsViewModel(private val attestationRepository: AttestationReposi _startActivity init { - val attestation = attestationRepository.getAttestations().value?.last() - attestationCreateAt = attestation?.createAt!! - _attestationName.value = "${attestation.profile.firstName} ${attestation.profile.lastName}" + _attestations = attestationRepository.getAttestations() as MutableLiveData> } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeFragment.kt new file mode 100644 index 0000000..a9f8afe --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeFragment.kt @@ -0,0 +1,57 @@ +package fr.sanchezm.attestationsCovid19.ui.qrcode + +import android.os.Bundle +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.databinding.DataBindingUtil +import androidx.lifecycle.ViewModelProvider +import fr.sanchezm.attestationsCovid19.R +import fr.sanchezm.attestationsCovid19.databinding.QrCodeFragmentBinding +import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils + +class QrCodeFragment : Fragment() { + + companion object { + fun newInstance() = QrCodeFragment() + } + + private lateinit var qrCodeViewModel: QrCodeViewModel + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + arguments?.getBoolean("toCreate", false)?.let { + arguments?.getLong("createAt", 0)?.let { it1 -> + initializeUi( + it1, + it + ) + } + } + val binding = DataBindingUtil.inflate( + inflater, + R.layout.qr_code_fragment, + container, + false + ).apply { + this.lifecycleOwner = this@QrCodeFragment + this.viewModel = qrCodeViewModel + } + + return binding.root + } + + + private fun initializeUi(createAt: Long, toCreate: Boolean) { + val factory = context?.let { InjectorUtils.provideQrCodeViewModel(it) } + qrCodeViewModel = factory?.let { + ViewModelProvider(this, it) + .get(QrCodeViewModel::class.java) + }!! + qrCodeViewModel.addData(createAt, toCreate) + } + +} diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt new file mode 100644 index 0000000..345a8bf --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt @@ -0,0 +1,24 @@ +package fr.sanchezm.attestationsCovid19.ui.qrcode + +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation +import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository + +class QrCodeViewModel(private val attestationRepository: AttestationRepository) : ViewModel() { + + private val _attestation = MutableLiveData() + private var toCreate: Boolean = false + + fun addData(createAt: Long, toCreate: Boolean) { + this.toCreate = toCreate + _attestation.value = attestationRepository.getAttestation(createAt) + if (toCreate) { + createPdf() + } + } + + fun createPdf() { + + } +} diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt new file mode 100644 index 0000000..18f4395 --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModelFactory.kt @@ -0,0 +1,14 @@ +package fr.sanchezm.attestationsCovid19.ui.qrcode + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository + +class QrCodeViewModelFactory(private val attestationRepository: AttestationRepository) : + ViewModelProvider.NewInstanceFactory() { + + @Suppress("UNCHECKED_CAST") + override fun create(modelClass: Class): T { + return QrCodeViewModel(attestationRepository) as T + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt index 0eb0aa9..e75ac3d 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt @@ -7,6 +7,7 @@ import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFactory +import fr.sanchezm.attestationsCovid19.ui.qrcode.QrCodeViewModelFactory object InjectorUtils { @@ -26,6 +27,13 @@ object InjectorUtils { return AttestationsViewModelFactory(attestationRepository) } + fun provideQrCodeViewModel(context: Context): QrCodeViewModelFactory { + val attestationRepository = + AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao()) + + return QrCodeViewModelFactory(attestationRepository) + } + fun providePdfUtils(context: Context): PdfUtils { return PdfUtils.getInstance(context.assets, getMyFilesDir(context)) } diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt index b3c24c4..dce3eb4 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt @@ -38,7 +38,6 @@ class PdfUtils private constructor( // Adding data setFieldsData(acroForm, attestation) setCheckboxFields(acroForm, attestation) - attestation.createAt = Date().time addPageWithQrCode(document, attestation) Log.v("PdfUtils", "Save File to ${getPath(attestation.createAt)}") diff --git a/app/src/main/res/layout/qr_code_activity.xml b/app/src/main/res/layout/qr_code_activity.xml new file mode 100644 index 0000000..3cd1c76 --- /dev/null +++ b/app/src/main/res/layout/qr_code_activity.xml @@ -0,0 +1,7 @@ + + diff --git a/app/src/main/res/layout/qr_code_fragment.xml b/app/src/main/res/layout/qr_code_fragment.xml new file mode 100644 index 0000000..936ee0e --- /dev/null +++ b/app/src/main/res/layout/qr_code_fragment.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + \ No newline at end of file