Skip to content

Commit

Permalink
Fix display of date in action if not filled: must be empty
Browse files Browse the repository at this point in the history
Then allow to send no date to server if not edited
  • Loading branch information
e-marchand committed May 29, 2024
1 parent 723477a commit 83e59bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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() + "!" + (
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down

0 comments on commit 83e59bd

Please sign in to comment.