material-components / material-components/material-components-android
[MaterialDatePicker] Support for selecting multiple dates
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
Currently there is no way to use the date picker to select multiple dates.
Only a single date or a date range is possible.
**Describe the solution you'd like**
I would like to see an date picker to select multiple dates which are not a range.
**Additional context**
We need an calendar which where users can select multiple (independend) dates.
Like
> I want to use this service on 2022-08-04 2022-08-10 and 2022-08-12
I found out that this is somewhat possible to implement already.
Checkout this one:
https://user-images.githubusercontent.com/10229883/182604466-2bb47ac4-89b7-4978-ba6d-e5ce8958803c.mp4
(Ugly!) Kotlin source code
```kotlin
@SuppressLint("RestrictedApi")
class MultipleDatesSelector() : SingleDateSelector() {
private var dates = mutableListOf()
override fun select(selection: Long) {
if (dates.contains(selection)) dates.remove(selection)
else dates.add(selection)
}
override fun isSelectionComplete(): Boolean {
return dates.isNotEmpty()
}
override fun getSelectedDays(): MutableCollection {
return dates.toCollection(arrayListOf())
}
override fun getSelectionDisplayString(context: Context): String {
return "" // TODO: A nice string for multiple dates
}
override fun getDefaultTitleResId(): Int {
return R.string.select_multiple_dates_title
}
/* Parcelable interface */
// TODO: Is this correcty implemented?!
@JvmField
val CREATOR: Parcelable.Creator = object : Parcelable.Creator {
override fun createFromParcel(source: Parcel): SingleDateSelector {
val singleDateSelector = X()
singleDateSelector.dates = source.readValue(List::class.java.classLoader) as MutableList
return singleDateSelector
}
override fun newArray(size: Int): Array {
return arrayOf(X())
}
}
}
```
This is possible because there exist already an method to add a custom selector.
https://github.com/material-components/material-components-android/blob/a6df2545efb84e7bca37fdf987ec35c6846e5025/lib/java/com/google/android/material/datepicker/MaterialDatePicker.java#L623-L627
Unfortunately, the method above as well as the `DateSelector` is restricted to the `Scope.LibraryGroup`, which is odd.
https://github.com/material-components/material-components-android/blob/a6df2545efb84e7bca37fdf987ec35c6846e5025/lib/java/com/google/android/material/datepicker/DateSelector.java#L50-L51
It would be great if to consider to either:
* Remove the restriction
* Support a multiple dates picker via this library
As my code shows the implementation isn't that hard.
Would you interested in support such an picker? I could also help by creating a pull request for it...
I would also help to cleanup the `DateSelector` interface instead and to remove the restriction (because I think it is restricted because the `DateSelector` isn't a great abstraction yeat 😁 )
Thank you.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with MaterialDatePicker.java around the custom-selector entry point and DateSelector.java, especially their Scope.LibraryGroup restrictions. Determine whether the change should expose custom selectors or add a built-in independent multiple-date selector; done means the selected dates can be chosen independently and returned through the picker’s public API.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java, kotlin
- Domain
- mobile
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100