From 83e59bdfdeddafaa42c86eaf4f970f111a6aa713 Mon Sep 17 00:00:00 2001 From: "eric.marchand" Date: Wed, 29 May 2024 13:44:13 +0200 Subject: [PATCH] Fix display of date in action if not filled: must be empty Then allow to send no date to server if not edited --- .../viewholders/DateViewHolder.kt | 37 ++++++++++++------- .../qmobileui/formatters/DateFormat.kt | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/viewholders/DateViewHolder.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/viewholders/DateViewHolder.kt index 01e20118..da00cffe 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/viewholders/DateViewHolder.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/viewholders/DateViewHolder.kt @@ -45,13 +45,9 @@ class DateViewHolder( container.endIconDrawable = ContextCompat.getDrawable(itemView.context, R.drawable.calendar_month) - if (initialPickerDate == -1L) { - initialPickerDate = Calendar.getInstance().timeInMillis - } - val datePicker = MaterialDatePicker.Builder.datePicker() - .setSelection(initialPickerDate) + .setSelection(if (initialPickerDate == -1L) Calendar.getInstance().timeInMillis else initialPickerDate) .setTitleText(container.hint) .setInputMode(MaterialDatePicker.INPUT_MODE_TEXT) .build() @@ -82,6 +78,10 @@ class DateViewHolder( } private fun getDateToSubmit(): String { + if (initialPickerDate == -1L) { + return DateFormat.nullDate + } + val calendar = Calendar.getInstance() calendar.timeInMillis = initialPickerDate return calendar.get(Calendar.DAY_OF_MONTH).toString() + "!" + ( @@ -92,21 +92,30 @@ class DateViewHolder( } private fun updatePickerDate(newDate: String) { - val cal = DateFormat.getDateFromString(newDate) ?: return - val clearedTZ = Calendar.getInstance().apply { - set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)) - set(Calendar.MONTH, cal.get(Calendar.MONTH)) - set(Calendar.YEAR, cal.get(Calendar.YEAR)) + DateFormat.getDateFromString(newDate)?.let { cal -> + val clearedTZ = Calendar.getInstance().apply { + set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)) + set(Calendar.MONTH, cal.get(Calendar.MONTH)) + set(Calendar.YEAR, cal.get(Calendar.YEAR)) + } + initialPickerDate = clearedTZ.timeInMillis + } ?: run { + initialPickerDate = -1L } - initialPickerDate = clearedTZ.timeInMillis } override fun formatToDisplay(input: String): String = FormatterUtils.applyFormat(dateFormat, input) override fun fill(value: Any) { - val string = if (value == JSONObject.NULL) "" else value.toString() - updatePickerDate(string) - super.fill(value) + if (value == JSONObject.NULL) { + val string = DateFormat.nullDate + updatePickerDate(string) + super.fill(string) + } + else { + updatePickerDate(value.toString()) + super.fill(value) + } } } diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/formatters/DateFormat.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/formatters/DateFormat.kt index abfb9cff..60e97ba4 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/formatters/DateFormat.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/formatters/DateFormat.kt @@ -14,7 +14,7 @@ import java.util.Locale object DateFormat { - private const val nullDate = "0!0!0" + public const val nullDate = "0!0!0" fun applyFormat(format: String, baseText: String): String { val calendar = getDateFromString(baseText) ?: return ""