Adding pickers for date and time

This commit is contained in:
Mathieu Sanchez 2020-05-02 15:46:08 +02:00
parent 52892c13d4
commit 5cb3478bb6
6 changed files with 100 additions and 16 deletions

View File

@ -3,7 +3,7 @@ package fr.sanchezm.attestationsCovid19.data.db.entity
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
const val PATTERN = "dd/MM/yyyy 'à' HH'h'mm" const val PATTERN = "dd/MM/yyyy 'à' HH:mm"
//@Entity(tableName = "attestation") //@Entity(tableName = "attestation")
data class Attestation( data class Attestation(

View File

@ -1,6 +1,9 @@
package fr.sanchezm.attestationsCovid19.ui.add package fr.sanchezm.attestationsCovid19.ui.add
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import androidx.lifecycle.LiveData 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.ConfigRepository
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
import fr.sanchezm.attestationsCovid19.utilities.Event import fr.sanchezm.attestationsCovid19.utilities.Event
import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
@ -21,7 +25,8 @@ import java.util.*
class AddViewModel( class AddViewModel(
private val configRepository: ConfigRepository, private val configRepository: ConfigRepository,
private val profileRepository: ProfileRepository, private val profileRepository: ProfileRepository,
private val attestationRepository: AttestationRepository private val attestationRepository: AttestationRepository,
private val app: Context
) : ViewModel() { ) : ViewModel() {
private val _errorMessage = MutableLiveData<Event<Int>>() private val _errorMessage = MutableLiveData<Event<Int>>()
@ -58,7 +63,7 @@ class AddViewModel(
// endregion // endregion
private val datePattern = "dd/MM/yyyy" private val datePattern = "dd/MM/yyyy"
private val timePattern = "HH'h'mm" private val timePattern = "HH:mm"
@SuppressLint("LongLogTag") @SuppressLint("LongLogTag")
fun onGenerateAttestationClick() { 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 { init {
setProfileValue() setProfileValue()
setDateHourToday() setDateHourToday()
@ -164,4 +231,13 @@ class AddViewModel(
|| reason7.value!! || reason7.value!!
} }
private fun getFormattedDayOrMonth(date: Int): String {
var formattedDate: String = date.toString()
if (formattedDate.length <= 1) {
formattedDate = "0$formattedDate"
}
return formattedDate
}
} }

View File

@ -1,5 +1,6 @@
package fr.sanchezm.attestationsCovid19.ui.add package fr.sanchezm.attestationsCovid19.ui.add
import android.content.Context
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
@ -9,12 +10,13 @@ import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
class AddViewModelFactory( class AddViewModelFactory(
private val configRepository: ConfigRepository, private val configRepository: ConfigRepository,
private val profileRepository: ProfileRepository, private val profileRepository: ProfileRepository,
private val attestationRepository: AttestationRepository private val attestationRepository: AttestationRepository,
private val app: Context
) : ) :
ViewModelProvider.NewInstanceFactory() { ViewModelProvider.NewInstanceFactory() {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return AddViewModel(configRepository, profileRepository, attestationRepository) as T return AddViewModel(configRepository, profileRepository, attestationRepository, app) as T
} }
} }

View File

@ -16,7 +16,8 @@ object InjectorUtils {
AddViewModelFactory( AddViewModelFactory(
getConfigRepo(context), getConfigRepo(context),
getProfileRepo(context), getProfileRepo(context),
getAttestationRepo(context) getAttestationRepo(context),
context
) )
fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory = fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory =

View File

@ -130,8 +130,8 @@ class PdfUtils private constructor(
addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}" addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}"
cityField.value = profile.city cityField.value = profile.city
dateField.value = attestation.exitDate dateField.value = attestation.exitDate
hourField.value = attestation.exitHour.split("h")[0] hourField.value = attestation.exitHour.split(":")[0]
minutesField.value = attestation.exitHour.split("h")[1] minutesField.value = attestation.exitHour.split(":")[1]
return true return true
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "${e.message}") Log.e(TAG, "${e.message}")

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.add.AddFragment"> tools:context=".ui.add.AddFragment">
<data> <data>
@ -12,9 +12,9 @@
</data> </data>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintContext"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:id="@+id/constraintContext">
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
@ -34,10 +34,10 @@
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/attestation_title" android:text="@string/attestation_title"
android:textColor="?attr/colorPrimary" android:textColor="?attr/colorPrimary"
android:textSize="25sp" /> android:textSize="25sp"
android:textStyle="bold" />
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
@ -88,9 +88,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="date" android:inputType="date"
android:focusable="false"
android:onClick="@{() -> viewModel.onBirthdayClick()}"
android:text="@={viewModel.birthday}" android:text="@={viewModel.birthday}"
app:validateDate='@{"dd/MM/yyyy"}' app:validateDate='@{"dd/MM/yyyy"}'
app:validateDateMessage="@{@string/date_error_message}"/> app:validateDateMessage="@{@string/date_error_message}" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
@ -164,10 +166,12 @@
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:focusable="false"
android:inputType="date" android:inputType="date"
android:onClick="@{() -> viewModel.onExitDateClick()}"
android:text="@={viewModel.exitDate}" android:text="@={viewModel.exitDate}"
app:validateDate='@{"dd/MM/yyyy"}' app:validateDate='@{"dd/MM/yyyy"}'
app:validateDateMessage="@{@string/date_error_message}"/> app:validateDateMessage="@{@string/date_error_message}" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
@ -181,7 +185,9 @@
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:focusable="false"
android:inputType="time" android:inputType="time"
android:onClick="@{() -> viewModel.onExitHourClick()}"
android:text="@={viewModel.exitHour}" /> android:text="@={viewModel.exitHour}" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
@ -257,7 +263,6 @@
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:onClick="@{() -> viewModel.onGenerateAttestationClick()}" android:onClick="@{() -> viewModel.onGenerateAttestationClick()}"
android:text="@string/generate_attestation_button" /> android:text="@string/generate_attestation_button" />
<!-- android:enabled="false"-->
</LinearLayout> </LinearLayout>