EdricChan03 / EdricChan03/androidx-ktx-extras

[New API] MDC Date Picker APIs should accept java.time equivalents

Open
#21 0 comments 0 reactions 0 assignees View on GitHub
api request enhancement
Dominant language
Kotlin
Stars
1
Forks
1
PR merge metrics
No merged PRs in 30d

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Library name

Material Components for Android extensions

### Problem

Material Components for Android's Date Picker currently expects a `long` to be used, which isn't very intuitive as to what you should pass to their APIs, especially when their parameters don't have a suffix that tells you what the unit should be:

```java
@NonNull
@CanIgnoreReturnValue
public Builder setSelection(S selection) {
this.selection = selection;
return this;
}
```

The `datePicker` static method for `MaterialDatePicker.Builder` returns a `Builder`, so it would look like such:

```java
@NonNull
@CanIgnoreReturnValue
public Builder setSelection(Long selection) {
this.selection = selection;
return this;
}
```

Which is still confusing as to what unit to pass the selection as (milliseconds? seconds? etc).

### Description

There should be extension functions to accept the more suitable `java.time` APIs first introduced in Java 8, and are designed to resolve the issue Java developers have with dates (and any existing bugs in the old `Calendar`/`Date` APIs).

I believe, however, that the Material Components for Android devs didn't go with this as it was only first introduced in Android Oreo (which first brought Java 8 support), so without core library desugaring it isn't possible.

Anyways, here's some proposed APIs:

* [ ] `MaterialDatePicker.Builder.setSelection` should take an `Instant`, or a `Pair` for a single date/date-range picker respectively. This would proxy to calling its `toEpochMilli` method to get its milliseconds representation:

```kt
fun MaterialSingleDatePickerBuilder.setSelection(instant: Instant) = setSelection(instant.toEpochMilli())
fun MaterialDateRangePickerBuilder.setSelection(instants: Pair) = setSelection(...)
```

* [ ] `CalendarConstraints.Builder` should take an `Instant` for its respective APIs that take a `long`:
* [ ] `setStart`
* [ ] `setEnd`
* [ ] `setOpenAt`

* [ ] `CalendarConstraints.Builder.setDayOfWeek` should take a `DayOfWeek`

### Alternatives?

Using the appropriate methods in the `java.time` APIs to retrieve its raw representation to then be passed to the existing Java API.

There are also already some example code snippets in some of the Javadocs for `java.time`.

### Additional info

See https://github.com/EdricChan03/studybuddy-android/commit/657c55191d7a1e7f8856591cee315585ec071550 for context

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.