-
Notifications
You must be signed in to change notification settings - Fork 310
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
base: master
Are you sure you want to change the base?
Changes from all commits
13bc132
69a2d9d
5d36a33
ff33b09
7cad8ab
a2104b8
c4d2172
0931d45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:-
Notes:-
So I guess something like following:-
|
||
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") | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.