Adding pickers for date and time
This commit is contained in:
parent
52892c13d4
commit
5cb3478bb6
@ -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(
|
||||
|
@ -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<Event<Int>>()
|
||||
@ -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
|
||||
}
|
||||
|
||||
}
|
@ -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 <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AddViewModel(configRepository, profileRepository, attestationRepository) as T
|
||||
return AddViewModel(configRepository, profileRepository, attestationRepository, app) as T
|
||||
}
|
||||
}
|
@ -16,7 +16,8 @@ object InjectorUtils {
|
||||
AddViewModelFactory(
|
||||
getConfigRepo(context),
|
||||
getProfileRepo(context),
|
||||
getAttestationRepo(context)
|
||||
getAttestationRepo(context),
|
||||
context
|
||||
)
|
||||
|
||||
fun provideAttestationViewModel(context: Context): AttestationsViewModelFactory =
|
||||
|
@ -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}")
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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:tools="http://schemas.android.com/tools"
|
||||
tools:context=".ui.add.AddFragment">
|
||||
|
||||
<data>
|
||||
@ -12,9 +12,9 @@
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/constraintContext"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/constraintContext">
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:layout_width="match_parent"
|
||||
@ -34,10 +34,10 @@
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textStyle="bold"
|
||||
android:text="@string/attestation_title"
|
||||
android:textColor="?attr/colorPrimary"
|
||||
android:textSize="25sp" />
|
||||
android:textSize="25sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
@ -88,6 +88,8 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="date"
|
||||
android:focusable="false"
|
||||
android:onClick="@{() -> viewModel.onBirthdayClick()}"
|
||||
android:text="@={viewModel.birthday}"
|
||||
app:validateDate='@{"dd/MM/yyyy"}'
|
||||
app:validateDateMessage="@{@string/date_error_message}" />
|
||||
@ -164,7 +166,9 @@
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:focusable="false"
|
||||
android:inputType="date"
|
||||
android:onClick="@{() -> viewModel.onExitDateClick()}"
|
||||
android:text="@={viewModel.exitDate}"
|
||||
app:validateDate='@{"dd/MM/yyyy"}'
|
||||
app:validateDateMessage="@{@string/date_error_message}" />
|
||||
@ -181,7 +185,9 @@
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:focusable="false"
|
||||
android:inputType="time"
|
||||
android:onClick="@{() -> viewModel.onExitHourClick()}"
|
||||
android:text="@={viewModel.exitHour}" />
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
@ -257,7 +263,6 @@
|
||||
android:layout_marginTop="20dp"
|
||||
android:onClick="@{() -> viewModel.onGenerateAttestationClick()}"
|
||||
android:text="@string/generate_attestation_button" />
|
||||
<!-- android:enabled="false"-->
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
Loading…
Reference in New Issue
Block a user