Skip to content
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

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

Open
LeojAngelico opened this issue Aug 9, 2023 · 0 comments

Comments

@LeojAngelico
Copy link

LeojAngelico commented Aug 9, 2023

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<CalendarConstraints.DateValidator>()
    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<DisabledDatesValidator> {
    override fun createFromParcel(parcel: Parcel): DisabledDatesValidator {
        return DisabledDatesValidator(emptyList())
    }

    override fun newArray(size: Int): Array<DisabledDatesValidator?> {
        return arrayOfNulls(size)
    }
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants