material-components / material-components/material-components-android

Cancel Date Range Selection when there are disabled dates in between of the selection

Open
#3,515 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request Widget: DatePicker
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

Hi, Is there a way to Cancel Date Range Selection when there are disabled dates in between of the date range?

So currently what I did is I disabled some dates in my calendar picker and then when I select a date range with disabled date in between, it shows that it is still a valid selection (see the attached image below, where in aug. 20 and aug 22 are the disabled dates)

So I am wondering if there is a way to cancel the range selection when there are disabled dates in between of the selected date range.

Screen Shot 2023-08-09 at 2 51 23 PM

here is my code:
`val minDate = Date()
val maxDate = getDateOneYearFromToday()

val dateValidatorMin = DateValidatorPointForward.from(minDate.time)
val dateValidatorMax = DateValidatorPointBackward.before(maxDate.time)

val listValidators = mutableListOf()
listValidators.add(dateValidatorMin)
listValidators.add(dateValidatorMax)
listValidators.add(DisabledDatesValidator(listOf("2023-08-20", "2023-08-22", "2023-08-31")))

val validators = CompositeDateValidator.allOf(listValidators)

val constraintsBuilder = CalendarConstraints.Builder()
.setValidator(validators)

val dateRangePicker = MaterialDatePicker.Builder
.dateRangePicker()
.setTitleText(getString(R.string.select_date))
.setCalendarConstraints(constraintsBuilder.build())
.build()
dateRangePicker.show()`

DisabledDatesValidator.class

`class DisabledDatesValidator(private val disabledDates: List) : CalendarConstraints.DateValidator {

override fun isValid(date: Long): Boolean {
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val selectedDate = Calendar.getInstance()
selectedDate.timeInMillis = date

val formattedDate = sdf.format(selectedDate.time)
return !disabledDates.contains(formattedDate)
}

override fun writeToParcel(dest: Parcel, flags: Int) {
// Write any necessary parcelable data if needed
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator {
override fun createFromParcel(parcel: Parcel): DisabledDatesValidator {
return DisabledDatesValidator(emptyList())
}

override fun newArray(size: Int): Array {
return arrayOfNulls(size)
}
}
}`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with MaterialDatePicker.dateRangePicker(), CalendarConstraints.DateValidator, and CompositeDateValidator.allOf() to trace how individual dates are checked during range selection. Confirm the existing range-selection behavior and identify the relevant picker tests; done means a range containing any disabled date is rejected or cancelled consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.