Apply date/time picks live, share the date dialog, review fixes (#101)

This commit is contained in:
makiolaj committed 2026-11-16 19:17:33 +01:00
1 parent 4376a48b1e
commit a1bd03eae8
7 files changed
+104 -108

No files matched your search

@@ -0,0 +1,40 @@
package de.jeanlucmakiola.agendula.ui.common
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneOffset
/** M3 date picker dialog seeded with [initial]; confirming without a selection dismisses. */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatePickerAlert(
initial: LocalDate,
onConfirm: (LocalDate) -> Unit,
onDismiss: () -> Unit,
) {
// M3's DatePicker speaks UTC-midnight millis.
val state = rememberDatePickerState(
initialSelectedDateMillis = initial.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(),
)
DatePickerDialog(
onDismissRequest = onDismiss,
confirmButton = {
TextButton(onClick = {
state.selectedDateMillis
?.let { onConfirm(Instant.ofEpochMilli(it).atZone(ZoneOffset.UTC).toLocalDate()) }
?: onDismiss()
}) { Text(stringResource(android.R.string.ok)) }
},
dismissButton = {
TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) }
},
) { DatePicker(state = state) }
}
@@ -3,12 +3,6 @@ package de.jeanlucmakiola.agendula.ui.edit
import android.text.format.DateFormat
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -21,6 +15,7 @@ import androidx.compose.ui.unit.dp
import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.agendula.domain.allDayInstantOf
import de.jeanlucmakiola.agendula.domain.calendarDate
import de.jeanlucmakiola.agendula.ui.common.DatePickerAlert
import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat
import de.jeanlucmakiola.agendula.ui.common.TimePickerAlert
import de.jeanlucmakiola.agendula.ui.common.formatMinuteOfDay
@@ -33,77 +28,76 @@ import de.jeanlucmakiola.floret.components.positionOf
import de.jeanlucmakiola.floret.locale.currentLocale
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.Locale
import kotlin.time.Instant
/**
* Start / due picker (#101): quick dates and preset times of day in one
* full-screen sheet, so either half can be changed without walking through the
* other. A date tap only stages the day (an all-day value commits at once); a
* time tap commits the staged day with that time, and Done keeps both as staged.
* other. A date applies at once and keeps the sheet open for a time (an all-day
* value closes it); a time applies and closes. The user leaves via back.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun DateTimePickerSheet(
title: String,
initial: Instant?,
allDay: Boolean,
onResult: (Instant) -> Unit,
onChange: (Instant) -> Unit,
onDismiss: () -> Unit,
) {
val today = remember { LocalDate.now() }
var date by rememberSaveable { mutableStateOf(initial?.calendarDate(allDay) ?: today) }
val time = rememberSaveable {
initial?.takeUnless { allDay }?.toLocalTime() ?: defaultPickerTime(LocalTime.now())
}
var date by rememberSaveable { mutableStateOf(initial?.calendarDate(allDay)) }
var time by rememberSaveable { mutableStateOf(initial?.takeUnless { allDay }?.toLocalTime()) }
var showCalendar by rememberSaveable { mutableStateOf(false) }
var showClock by rememberSaveable { mutableStateOf(false) }
val today = LocalDate.now()
val is24Hour = LocalUse24HourFormat.current
val locale = currentLocale()
val dayFormats = remember(locale) {
listOf("EEEdMMM", "EEEdMMMyyyy").map { DateTimeFormatter.ofPattern(DateFormat.getBestDateTimePattern(locale, it), locale) }
}
fun dayLabel(d: LocalDate) = d.format(dayFormats[if (d.year == today.year) 0 else 1])
val dayFormats = remember(locale) { DayFormats(locale) }
fun dayLabel(d: LocalDate) = d.format(if (d.year == today.year) dayFormats.thisYear else dayFormats.otherYear)
fun timeLabel(t: LocalTime) = formatMinuteOfDay(t.hour * 60 + t.minute, is24Hour, locale)
fun commit(d: LocalDate, t: LocalTime) = onResult(if (allDay) allDayInstantOf(d) else localToInstant(d, t))
fun pickDate(d: LocalDate) {
if (allDay) commit(d, time) else date = d
date = d
if (allDay) {
onChange(allDayInstantOf(d))
onDismiss()
} else {
val t = time ?: defaultPickerTime(LocalTime.now()).also { time = it }
onChange(localToInstant(d, t))
}
}
fun pickTime(t: LocalTime) {
onChange(localToInstant(date ?: LocalDate.now(), t))
onDismiss()
}
FullScreenPicker(
title = title,
onDismiss = onDismiss,
actions = {
if (!allDay) {
TextButton(onClick = { commit(date, time) }) { Text(stringResource(R.string.picker_done)) }
}
},
) {
val quickDate = QuickDate.of(date, today)
FullScreenPicker(title = title, onDismiss = onDismiss) {
val quickDate = date?.let { QuickDate.of(it, today) }
val dateRows = QuickDate.entries.size + 1
GroupedSectionHeader(stringResource(R.string.picker_section_date))
QuickDate.entries.forEachIndexed { index, quick ->
val day = quick.resolve(today)
GroupedRow(
title = stringResource(quick.labelRes),
summary = dayLabel(day),
summary = dayLabel(quick.resolve(today)),
position = positionOf(index, dateRows),
checked = quick == quickDate,
onClick = { pickDate(day) },
onClick = { pickDate(quick.resolve(LocalDate.now())) },
)
}
val customDate = date?.takeIf { quickDate == null }
GroupedRow(
title = stringResource(R.string.picker_date_pick),
summary = dayLabel(date).takeIf { quickDate == null },
summary = customDate?.let(::dayLabel),
position = positionOf(QuickDate.entries.size, dateRows),
checked = quickDate == null,
checked = customDate != null,
onClick = { showCalendar = true },
)
if (!allDay) {
val quickTime = QuickTime.of(time)
val quickTime = time?.let { QuickTime.of(it) }
val timeRows = QuickTime.entries.size + 1
Spacer(Modifier.height(8.dp))
GroupedSectionHeader(stringResource(R.string.picker_section_time))
@@ -113,56 +107,57 @@ internal fun DateTimePickerSheet(
summary = timeLabel(quick.time),
position = positionOf(index, timeRows),
checked = quick == quickTime,
onClick = { commit(date, quick.time) },
onClick = { pickTime(quick.time) },
)
}
val customTime = time?.takeIf { quickTime == null }
GroupedRow(
title = stringResource(R.string.picker_time_custom),
summary = timeLabel(time).takeIf { quickTime == null },
summary = customTime?.let(::timeLabel),
position = positionOf(QuickTime.entries.size, timeRows),
checked = quickTime == null,
checked = customTime != null,
onClick = { showClock = true },
)
}
}
if (showCalendar) {
// M3's DatePicker speaks UTC millis.
val state = rememberDatePickerState(
initialSelectedDateMillis = date.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(),
DatePickerAlert(
initial = date ?: today,
onConfirm = {
showCalendar = false
pickDate(it)
},
onDismiss = { showCalendar = false },
)
DatePickerDialog(
onDismissRequest = { showCalendar = false },
confirmButton = {
TextButton(onClick = {
showCalendar = false
state.selectedDateMillis?.let {
pickDate(java.time.Instant.ofEpochMilli(it).atZone(ZoneOffset.UTC).toLocalDate())
}
}) { Text(stringResource(android.R.string.ok)) }
},
dismissButton = {
TextButton(onClick = { showCalendar = false }) { Text(stringResource(android.R.string.cancel)) }
},
) { DatePicker(state = state) }
}
if (showClock) {
TimePickerAlert(
initial = time,
initial = time ?: defaultPickerTime(LocalTime.now()),
onConfirm = {
showClock = false
commit(date, it)
pickTime(it)
},
onDismiss = { showClock = false },
)
}
}
/** Weekday + day + month, with the year only when it isn't the current one. */
private class DayFormats(locale: Locale) {
val thisYear: DateTimeFormatter = bestPattern(locale, "EEEdMMM")
val otherYear: DateTimeFormatter = bestPattern(locale, "EEEdMMMyyyy")
private fun bestPattern(locale: Locale, skeleton: String): DateTimeFormatter =
runCatching { DateTimeFormatter.ofPattern(DateFormat.getBestDateTimePattern(locale, skeleton), locale) }
.getOrElse { DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale) }
}
private val QuickDate.labelRes: Int
get() = when (this) {
QuickDate.Today -> R.string.picker_date_today
QuickDate.Tomorrow -> R.string.picker_date_tomorrow
QuickDate.Today -> R.string.home_due_today
QuickDate.Tomorrow -> R.string.home_due_tomorrow
QuickDate.NextWeek -> R.string.picker_date_next_week
}
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.ui.edit
import java.time.LocalDate
import java.time.LocalTime
import java.time.temporal.ChronoUnit
/** The one-tap dates the start / due picker offers above its calendar row. */
internal enum class QuickDate(val daysFromToday: Long) {
@@ -27,7 +28,7 @@ internal enum class QuickTime(val time: LocalTime) {
;
companion object {
fun of(time: LocalTime): QuickTime? = entries.firstOrNull { it.time == time }
fun of(time: LocalTime): QuickTime? = time.truncatedTo(ChronoUnit.MINUTES).let { t -> entries.firstOrNull { it.time == t } }
}
}
@@ -12,9 +12,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SegmentedButton
import androidx.compose.material3.SegmentedButtonDefaults
@@ -22,7 +19,6 @@ import androidx.compose.material3.SingleChoiceSegmentedButtonRow
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -57,6 +53,7 @@ import kotlinx.datetime.LocalDate
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.toJavaDayOfWeek
import kotlinx.datetime.toJavaLocalDate
import de.jeanlucmakiola.agendula.ui.common.DatePickerAlert
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.format.TextStyle as JavaTextStyle
@@ -315,8 +312,8 @@ fun RecurrencePicker(
}
if (showUntilPicker) {
UntilDatePicker(
initial = untilDate ?: startDate,
DatePickerAlert(
initial = (untilDate ?: startDate).toJavaLocalDate(),
onConfirm = {
untilIso = it.toString()
showUntilPicker = false
@@ -386,38 +383,6 @@ private fun CheckRow(
)
}
/** M3 date picker for the UNTIL day; the picker speaks UTC-midnight millis. */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun UntilDatePicker(
initial: LocalDate,
onConfirm: (LocalDate) -> Unit,
onDismiss: () -> Unit,
) {
val state = rememberDatePickerState(
initialSelectedDateMillis = initial.toEpochDays() * MILLIS_PER_DAY,
)
DatePickerDialog(
onDismissRequest = onDismiss,
confirmButton = {
TextButton(
onClick = {
state.selectedDateMillis?.let { millis ->
onConfirm(LocalDate.fromEpochDays((millis / MILLIS_PER_DAY).toInt()))
} ?: onDismiss()
},
) { Text(stringResource(android.R.string.ok)) }
},
dismissButton = {
TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) }
},
) {
DatePicker(state = state)
}
}
private const val MILLIS_PER_DAY: Long = 86_400_000L
/** One tappable circle per weekday in locale week order, multi-select (BYDAY picks). */
@Composable
private fun WeekdayToggleRow(
@@ -660,10 +660,7 @@ private fun EditContent(
title = stringResource(if (target == PickerTarget.Start) R.string.edit_start_label else R.string.edit_due_label),
initial = current,
allDay = state.isAllDay,
onResult = {
if (target == PickerTarget.Start) viewModel.onStartChange(it) else viewModel.onDueChange(it)
pickerTarget = null
},
onChange = { if (target == PickerTarget.Start) viewModel.onStartChange(it) else viewModel.onDueChange(it) },
onDismiss = { pickerTarget = null },
)
}
-3
View File
@@ -100,8 +100,6 @@
<string name="edit_more_fields">More fields</string>
<string name="picker_section_date">Date</string>
<string name="picker_section_time">Time</string>
<string name="picker_date_today">Today</string>
<string name="picker_date_tomorrow">Tomorrow</string>
<string name="picker_date_next_week">Next week</string>
<string name="picker_date_pick">Pick a date</string>
<string name="picker_time_morning">Morning</string>
@@ -109,7 +107,6 @@
<string name="picker_time_afternoon">Afternoon</string>
<string name="picker_time_evening">Evening</string>
<string name="picker_time_custom">Custom time</string>
<string name="picker_done">Done</string>
<string name="close">Close</string>
<string name="dialog_cancel">Cancel</string>
@@ -29,6 +29,7 @@ class QuickPicksTest {
assertThat(QuickTime.of(LocalTime.of(8, 0))).isEqualTo(QuickTime.Morning)
assertThat(QuickTime.of(LocalTime.of(20, 0))).isEqualTo(QuickTime.Evening)
assertThat(QuickTime.of(LocalTime.of(8, 1))).isNull()
assertThat(QuickTime.of(LocalTime.of(8, 0, 30, 500))).isEqualTo(QuickTime.Morning)
}
@Test