-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Material 3 date and time pickers, with some style tweaks
FIXES: #53
- Loading branch information
1 parent
3c36163
commit f5432de
Showing
4 changed files
with
90 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 35 additions & 18 deletions
53
app/src/main/java/org/kde/bettercounter/ui/DateTimePicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
package org.kde.bettercounter.ui | ||
|
||
import android.app.Activity | ||
import android.app.DatePickerDialog | ||
import android.app.TimePickerDialog | ||
import android.content.Context | ||
import android.text.format.DateFormat | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.material.datepicker.CalendarConstraints | ||
import com.google.android.material.datepicker.MaterialDatePicker | ||
import com.google.android.material.timepicker.MaterialTimePicker | ||
import com.google.android.material.timepicker.TimeFormat | ||
import java.util.Calendar | ||
import java.util.Date | ||
|
||
fun showDateTimePicker(context: Context, initialDateTime: Calendar, callback: (Calendar) -> Unit) { | ||
fun showDateTimePicker(activity: AppCompatActivity, initialDateTime: Calendar, callback: (Calendar) -> Unit) { | ||
val initialHour = initialDateTime.get(Calendar.HOUR_OF_DAY) | ||
val initialMinute = initialDateTime.get(Calendar.MINUTE) | ||
val use24HourClock = DateFormat.is24HourFormat(context) | ||
showDatePicker(context, initialDateTime) { cal -> | ||
TimePickerDialog(context, { _, hour, minute -> | ||
cal.set(Calendar.MINUTE, minute) | ||
cal.set(Calendar.HOUR_OF_DAY, hour) | ||
callback(cal) | ||
}, initialHour, initialMinute, use24HourClock).show() | ||
}} | ||
val timeFormat = if (DateFormat.is24HourFormat(activity)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H | ||
showDatePicker(activity, initialDateTime) { cal -> | ||
MaterialTimePicker.Builder() | ||
.setTimeFormat(timeFormat) | ||
.setHour(initialHour) | ||
.setMinute(initialMinute) | ||
.setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK) | ||
.build().apply { | ||
addOnPositiveButtonClickListener { | ||
cal.set(Calendar.MINUTE, minute) | ||
cal.set(Calendar.HOUR_OF_DAY, hour) | ||
callback(cal) | ||
} | ||
}.show(activity.supportFragmentManager, "timePicker") | ||
} | ||
} | ||
|
||
fun showDatePicker(context: Context, initialDateTime: Calendar, callback: (Calendar) -> Unit) { | ||
val initialYear = initialDateTime.get(Calendar.YEAR) | ||
val initialMonth = initialDateTime.get(Calendar.MONTH) | ||
val initialDay = initialDateTime.get(Calendar.DAY_OF_MONTH) | ||
DatePickerDialog(context, { _, year, month, day -> | ||
val cal = Calendar.getInstance() | ||
cal.set(year, month, day) | ||
callback(cal) | ||
}, initialYear, initialMonth, initialDay).show() | ||
fun showDatePicker(activity: AppCompatActivity, initialDateTime: Calendar, callback: (Calendar) -> Unit) { | ||
MaterialDatePicker.Builder.datePicker() | ||
.setSelection(initialDateTime.timeInMillis) | ||
.build().apply { | ||
addOnPositiveButtonClickListener { | ||
val cal = Calendar.getInstance() | ||
cal.timeInMillis = it | ||
callback(cal) | ||
} | ||
} | ||
.show(activity.supportFragmentManager, "datePicker") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters