From 5cb3478bb6efee8fbeb0b69c6365ef23ea54d3e2 Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Sat, 2 May 2020 15:46:08 +0200 Subject: [PATCH] Adding pickers for date and time --- .../data/db/entity/Attestation.kt | 2 +- .../ui/add/AddViewModel.kt | 80 ++++++++++++++++++- .../ui/add/AddViewModelFactory.kt | 6 +- .../utilities/InjectorUtils.kt | 3 +- .../attestationsCovid19/utilities/PdfUtils.kt | 4 +- .../res/layout/fragment_add_attestation.xml | 21 +++-- 6 files changed, 100 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt index 35880b7..fc5939a 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt @@ -3,7 +3,7 @@ package fr.sanchezm.attestationsCovid19.data.db.entity import java.text.SimpleDateFormat import java.util.* -const val PATTERN = "dd/MM/yyyy 'à' HH'h'mm" +const val PATTERN = "dd/MM/yyyy 'à' HH:mm" //@Entity(tableName = "attestation") data class Attestation( 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 2ad4498..260237d 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 @@ -1,6 +1,9 @@ package fr.sanchezm.attestationsCovid19.ui.add import android.annotation.SuppressLint +import android.app.DatePickerDialog +import android.app.TimePickerDialog +import android.content.Context import android.os.Build import android.util.Log import androidx.lifecycle.LiveData @@ -13,6 +16,7 @@ import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository import fr.sanchezm.attestationsCovid19.utilities.Event +import java.text.DateFormat import java.text.SimpleDateFormat import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -21,7 +25,8 @@ import java.util.* class AddViewModel( private val configRepository: ConfigRepository, private val profileRepository: ProfileRepository, - private val attestationRepository: AttestationRepository + private val attestationRepository: AttestationRepository, + private val app: Context ) : ViewModel() { private val _errorMessage = MutableLiveData>() @@ -58,7 +63,7 @@ class AddViewModel( // endregion private val datePattern = "dd/MM/yyyy" - private val timePattern = "HH'h'mm" + private val timePattern = "HH:mm" @SuppressLint("LongLogTag") fun onGenerateAttestationClick() { @@ -75,6 +80,68 @@ class AddViewModel( } } + fun onBirthdayClick() { + val c = Calendar.getInstance().also { it.set(1970, 0, 1) } + if (!birthday.value.isNullOrBlank()) { + birthday.value?.let { birthday -> + DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE).parse(birthday) + ?.let { c.time = it } + } + } + val year = c.get(Calendar.YEAR) + val month = c.get(Calendar.MONTH) + val day = c.get(Calendar.DAY_OF_MONTH) + + val dpd = DatePickerDialog( + app, + DatePickerDialog.OnDateSetListener { _, yearPicked, monthOfYear, dayOfMonth -> + birthday.value = + "${getFormattedDayOrMonth(dayOfMonth)}/${getFormattedDayOrMonth(monthOfYear + 1)}/$yearPicked" + }, + year, + month, + day + ) + dpd.show() + } + + fun onExitDateClick() { + val c = Calendar.getInstance() + val year = c.get(Calendar.YEAR) + val month = c.get(Calendar.MONTH) + val day = c.get(Calendar.DAY_OF_MONTH) + + val dpd = DatePickerDialog( + app, + DatePickerDialog.OnDateSetListener { _, yearPicked, monthOfYear, dayOfMonth -> + exitDate.value = + "${getFormattedDayOrMonth(dayOfMonth)}/${getFormattedDayOrMonth(monthOfYear + 1)}/$yearPicked" + }, + year, + month, + day + ) + dpd.show() + } + + fun onExitHourClick() { + val c = Calendar.getInstance() + val hour = c.get(Calendar.HOUR_OF_DAY) + val minute = c.get(Calendar.MINUTE) + + val tpd = TimePickerDialog( + app, + TimePickerDialog.OnTimeSetListener { _, hourPicked, minutePicked -> + exitHour.value = + "${getFormattedDayOrMonth(hourPicked)}:${getFormattedDayOrMonth(minutePicked)}" + }, + hour, + minute, + true + ) + tpd.show() + } + init { setProfileValue() setDateHourToday() @@ -164,4 +231,13 @@ class AddViewModel( || reason7.value!! } + private fun getFormattedDayOrMonth(date: Int): String { + var formattedDate: String = date.toString() + + if (formattedDate.length <= 1) { + formattedDate = "0$formattedDate" + } + return formattedDate + } + } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt index 34545e8..1f45d20 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModelFactory.kt @@ -1,5 +1,6 @@ package fr.sanchezm.attestationsCovid19.ui.add +import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository @@ -9,12 +10,13 @@ import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository class AddViewModelFactory( private val configRepository: ConfigRepository, private val profileRepository: ProfileRepository, - private val attestationRepository: AttestationRepository + private val attestationRepository: AttestationRepository, + private val app: Context ) : ViewModelProvider.NewInstanceFactory() { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T { - return AddViewModel(configRepository, profileRepository, attestationRepository) as T + return AddViewModel(configRepository, profileRepository, attestationRepository, app) 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 9e0f85d..a1fe5b5 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt @@ -16,7 +16,8 @@ object InjectorUtils { AddViewModelFactory( getConfigRepo(context), getProfileRepo(context), - getAttestationRepo(context) + getAttestationRepo(context), + context ) fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory = 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 f254de8..7e85f0f 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt @@ -130,8 +130,8 @@ class PdfUtils private constructor( addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}" cityField.value = profile.city dateField.value = attestation.exitDate - hourField.value = attestation.exitHour.split("h")[0] - minutesField.value = attestation.exitHour.split("h")[1] + hourField.value = attestation.exitHour.split(":")[0] + minutesField.value = attestation.exitHour.split(":")[1] return true } catch (e: Exception) { Log.e(TAG, "${e.message}") diff --git a/app/src/main/res/layout/fragment_add_attestation.xml b/app/src/main/res/layout/fragment_add_attestation.xml index 4ec5afc..8a25283 100644 --- a/app/src/main/res/layout/fragment_add_attestation.xml +++ b/app/src/main/res/layout/fragment_add_attestation.xml @@ -1,7 +1,7 @@ @@ -12,9 +12,9 @@ + android:layout_height="match_parent"> + android:textSize="25sp" + android:textStyle="bold" /> + app:validateDateMessage="@{@string/date_error_message}" /> @@ -164,10 +166,12 @@ + app:validateDateMessage="@{@string/date_error_message}" /> @@ -181,7 +185,9 @@ @@ -257,7 +263,6 @@ android:layout_marginTop="20dp" android:onClick="@{() -> viewModel.onGenerateAttestationClick()}" android:text="@string/generate_attestation_button" /> -