Skip to content

Date Picker Constraint Issue Fix. #2514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,27 @@ internal object DatePickerViewHolderFactory :
}

private fun buildMaterialDatePicker(localDate: LocalDate?): MaterialDatePicker<Long> {
val selectedDateMillis =
localDate?.atStartOfDay(ZONE_ID_UTC)?.toInstant()?.toEpochMilli()
?: MaterialDatePicker.todayInUtcMilliseconds()
val minDateInMillis =
(questionnaireViewItem.minAnswerValue as? DateType)
?.value
?.localDate
?.atStartOfDay(
ZONE_ID_UTC,
)
?.toInstant()
?.toEpochMilli()
?: localDate?.atStartOfDay(ZONE_ID_UTC)?.toInstant()?.toEpochMilli()
?: MaterialDatePicker.todayInUtcMilliseconds()
Comment on lines +158 to +167
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of this can probably be simplified, the conversion from local date to epoch milli is the same for the min value and the local date input, so you can simplify this code a bit by rewriting that.

but the bigger question is why this logic is written as such - you're reading from the questionnaire constraint's min value, and if it's missing, then you take the input value (which could have been an input from before, and then if that's also missing, you take today's date. and then you set this as the selection. and then you use this for two things: 1) the selection for the date picker and 2) the min date for the calendar constraint. i don't really understand why this is done this way.

to me, a perhaps much simpler fix is to simply not set the selection below:

.setSelection(selectedDateMillis)

if the selection will not make any sense. have you tried removing this line?

in that case i'd imagine the behaviour of the date picker will be that you cannot click ok since there simply isn't any possible date to choose from that would satisfy the calendar constraints.

have you tried that? i feel that's a more logical fix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jingtang10 , this makes sense. Removing the .setSelection(selectedDateMillis) will disable the "ok" button and enable only when selected a date. I think this could be more better fix than what I had proposed. Thanks for pointing it out.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still to set the selection if there is an existing answer right? why are you removing it completely?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is to streamline user experience, Jing. This is what I had in mind:-
Invalid Date Range: If minDate is later than maxDate, disable the date picker and display a clear error message.
Existing Answer: If the user has already selected a date (i.e., localDate is not null), set the date picker to that value.
No Answer, Valid Range: If there's no previous answer but the date range is valid:

  1. Prioritize Today's Date: Since users are likely to choose today's date, set the date picker to the today's date if it falls within the valid range (minDate to maxDate).
  2. Fallback to minDate: If today's date is outside the valid range, use minDate as the default selection to avoid excessive scrolling or clicking for the user.

Notes:-

  1. Once user selects a valid date, ie, localDate is non null, then we should set the date picker to that value only.
  2. minDateInMillis should be calculated solely based on minValueAnswer, not influenced by the user's current selection or the current date.

So I guess something like following:-

val dateToSelect = when { 
    localDate != null -> localDate
    currentDate between minDate..maxDate -> currentDate 
    else -> minDate // If not in range, minDate fallback
}

val maxDateInMillis = (questionnaireViewItem.maxAnswerValue as? DateType)?.value?.time
val calendarConstraints = getCalenderConstraint(minDateInMillis, maxDateInMillis)

return MaterialDatePicker.Builder.datePicker()
.setTitleText(R.string.select_date)
.setSelection(selectedDateMillis)
.setCalendarConstraints(getCalenderConstraint())
.setCalendarConstraints(calendarConstraints)
.build()
}

private fun getCalenderConstraint(): CalendarConstraints {
val min = (questionnaireViewItem.minAnswerValue as? DateType)?.value?.time
val max = (questionnaireViewItem.maxAnswerValue as? DateType)?.value?.time

private fun getCalenderConstraint(min: Long?, max: Long?): CalendarConstraints {
if (min != null && max != null && min > max) {
throw IllegalArgumentException("minValue cannot be greater than maxValue")
}
Expand Down