Stage date picks behind Done, keep quick times ahead, review fixes (#101)
This commit is contained in:
1 parent
a1bd03eae8
commit
1f8d8d230d
7 files changed
+89
-32
No files matched your search
@@ -7,6 +7,13 @@ All notable changes to this project are documented here. The format follows
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Changed
|
||||
- Start and due dates are picked in one full-screen sheet with quick dates
|
||||
(today, tomorrow, next week) and preset times (morning, noon, afternoon,
|
||||
evening), so the date or the time can be changed on its own without walking
|
||||
through both pickers. A calendar and a clock are still there for anything
|
||||
else (#101).
|
||||
|
||||
## [1.3.2] - 2026-11-13
|
||||
|
||||
### Added
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
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.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
@@ -26,55 +27,74 @@ import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.GroupedSectionHeader
|
||||
import de.jeanlucmakiola.floret.components.positionOf
|
||||
import de.jeanlucmakiola.floret.locale.currentLocale
|
||||
import de.jeanlucmakiola.floret.locale.localizedDateFormatter
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
import java.time.LocalTime
|
||||
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 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.
|
||||
* other. A tap that completes the value applies it and closes: any time, or a
|
||||
* date when the value already has a time (or is all-day). A date for a value
|
||||
* without a time is staged with a default time and committed by Done. Back
|
||||
* discards anything staged.
|
||||
*
|
||||
* [notBefore] keeps a time picked for an empty value from landing before it
|
||||
* (the start, when picking a due).
|
||||
*/
|
||||
@Composable
|
||||
internal fun DateTimePickerSheet(
|
||||
title: String,
|
||||
initial: Instant?,
|
||||
allDay: Boolean,
|
||||
notBefore: Instant?,
|
||||
onChange: (Instant) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val timeKnown = initial != null && !allDay
|
||||
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 today = remember { LocalDate.now() }
|
||||
val is24Hour = LocalUse24HourFormat.current
|
||||
val locale = currentLocale()
|
||||
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?) {
|
||||
onChange(if (allDay) allDayInstantOf(d) else localToInstant(d, t ?: defaultPickerTime(d, LocalDateTime.now())))
|
||||
onDismiss()
|
||||
}
|
||||
fun pickDate(d: LocalDate) {
|
||||
date = d
|
||||
if (allDay) {
|
||||
onChange(allDayInstantOf(d))
|
||||
onDismiss()
|
||||
if (allDay || timeKnown) {
|
||||
commit(d, time)
|
||||
} else {
|
||||
val t = time ?: defaultPickerTime(LocalTime.now()).also { time = it }
|
||||
onChange(localToInstant(d, t))
|
||||
date = d
|
||||
time = time ?: defaultPickerTime(d, LocalDateTime.now())
|
||||
}
|
||||
}
|
||||
fun pickTime(t: LocalTime) {
|
||||
onChange(localToInstant(date ?: LocalDate.now(), t))
|
||||
onDismiss()
|
||||
val floor = notBefore?.takeUnless { allDay }?.let { LocalDateTime.of(it.calendarDate(false), it.toLocalTime()) }
|
||||
commit(date ?: defaultPickerDate(t, LocalDateTime.now(), floor), t)
|
||||
}
|
||||
|
||||
FullScreenPicker(title = title, onDismiss = onDismiss) {
|
||||
FullScreenPicker(
|
||||
title = title,
|
||||
onDismiss = onDismiss,
|
||||
actions = {
|
||||
TextButton(
|
||||
onClick = { date?.let { commit(it, time) } },
|
||||
enabled = date != null,
|
||||
) { Text(stringResource(R.string.picker_done)) }
|
||||
},
|
||||
) {
|
||||
val quickDate = date?.let { QuickDate.of(it, today) }
|
||||
val dateRows = QuickDate.entries.size + 1
|
||||
GroupedSectionHeader(stringResource(R.string.picker_section_date))
|
||||
@@ -84,7 +104,7 @@ internal fun DateTimePickerSheet(
|
||||
summary = dayLabel(quick.resolve(today)),
|
||||
position = positionOf(index, dateRows),
|
||||
checked = quick == quickDate,
|
||||
onClick = { pickDate(quick.resolve(LocalDate.now())) },
|
||||
onClick = { pickDate(quick.resolve(today)) },
|
||||
)
|
||||
}
|
||||
val customDate = date?.takeIf { quickDate == null }
|
||||
@@ -134,7 +154,7 @@ internal fun DateTimePickerSheet(
|
||||
|
||||
if (showClock) {
|
||||
TimePickerAlert(
|
||||
initial = time ?: defaultPickerTime(LocalTime.now()),
|
||||
initial = time ?: defaultPickerTime(date ?: today, LocalDateTime.now()),
|
||||
onConfirm = {
|
||||
showClock = false
|
||||
pickTime(it)
|
||||
@@ -146,18 +166,14 @@ internal fun DateTimePickerSheet(
|
||||
|
||||
/** 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) }
|
||||
val thisYear: DateTimeFormatter = localizedDateFormatter(locale, "EEEdMMM")
|
||||
val otherYear: DateTimeFormatter = localizedDateFormatter(locale, "EEEdMMMyyyy")
|
||||
}
|
||||
|
||||
private val QuickDate.labelRes: Int
|
||||
get() = when (this) {
|
||||
QuickDate.Today -> R.string.home_due_today
|
||||
QuickDate.Tomorrow -> R.string.home_due_tomorrow
|
||||
QuickDate.Today -> R.string.picker_date_today
|
||||
QuickDate.Tomorrow -> R.string.picker_date_tomorrow
|
||||
QuickDate.NextWeek -> R.string.picker_date_next_week
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.jeanlucmakiola.agendula.ui.edit
|
||||
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
import java.time.LocalTime
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@@ -32,6 +33,16 @@ internal enum class QuickTime(val time: LocalTime) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Time to stage for an empty value: the next full hour, kept on the same day. */
|
||||
internal fun defaultPickerTime(now: LocalTime): LocalTime =
|
||||
if (now.hour == 23) LocalTime.of(23, 59) else LocalTime.of(now.hour + 1, 0)
|
||||
/** Time to stage for a date picked without one: the next full hour today, the morning preset on another day. */
|
||||
internal fun defaultPickerTime(date: LocalDate, now: LocalDateTime): LocalTime = when {
|
||||
date != now.toLocalDate() -> QuickTime.Morning.time
|
||||
now.hour == 23 -> LocalTime.of(23, 59)
|
||||
else -> LocalTime.of(now.hour + 1, 0)
|
||||
}
|
||||
|
||||
/** Date for a time picked without one: the first day on which [time] is not behind [now] or [notBefore]. */
|
||||
internal fun defaultPickerDate(time: LocalTime, now: LocalDateTime, notBefore: LocalDateTime? = null): LocalDate {
|
||||
val floor = if (notBefore != null && notBefore > now) notBefore else now
|
||||
val day = floor.toLocalDate()
|
||||
return if (day.atTime(time) < floor.truncatedTo(ChronoUnit.MINUTES)) day.plusDays(1) else day
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import de.jeanlucmakiola.agendula.domain.recurrence.RecurrenceFreq
|
||||
import de.jeanlucmakiola.agendula.domain.recurrence.SimpleRecurrence
|
||||
import de.jeanlucmakiola.agendula.domain.recurrence.parseSimpleRecurrence
|
||||
import de.jeanlucmakiola.agendula.domain.recurrence.toRRule
|
||||
import de.jeanlucmakiola.agendula.ui.common.DatePickerAlert
|
||||
import de.jeanlucmakiola.agendula.ui.common.nextOccurrencesText
|
||||
import de.jeanlucmakiola.agendula.ui.common.recurrenceText
|
||||
import de.jeanlucmakiola.floret.components.DialogAmountField
|
||||
@@ -53,7 +54,6 @@ 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
|
||||
|
||||
@@ -660,6 +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,
|
||||
notBefore = state.start.takeIf { target == PickerTarget.Due },
|
||||
onChange = { if (target == PickerTarget.Start) viewModel.onStartChange(it) else viewModel.onDueChange(it) },
|
||||
onDismiss = { pickerTarget = null },
|
||||
)
|
||||
|
||||
@@ -100,6 +100,8 @@
|
||||
<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>
|
||||
@@ -107,6 +109,7 @@
|
||||
<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>
|
||||
|
||||
|
||||
@@ -33,9 +33,28 @@ class QuickPicksTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the default time is the next full hour without leaving the day`() {
|
||||
assertThat(defaultPickerTime(LocalTime.of(9, 41))).isEqualTo(LocalTime.of(10, 0))
|
||||
assertThat(defaultPickerTime(LocalTime.of(0, 0))).isEqualTo(LocalTime.of(1, 0))
|
||||
assertThat(defaultPickerTime(LocalTime.of(23, 10))).isEqualTo(LocalTime.of(23, 59))
|
||||
fun `the default time is the next full hour today and the morning on another day`() {
|
||||
assertThat(defaultPickerTime(today, today.atTime(9, 41))).isEqualTo(LocalTime.of(10, 0))
|
||||
assertThat(defaultPickerTime(today, today.atTime(0, 0))).isEqualTo(LocalTime.of(1, 0))
|
||||
assertThat(defaultPickerTime(today, today.atTime(23, 10))).isEqualTo(LocalTime.of(23, 59))
|
||||
assertThat(defaultPickerTime(today.plusDays(1), today.atTime(23, 10))).isEqualTo(LocalTime.of(8, 0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a time without a date lands on the first day it is still ahead`() {
|
||||
val now = today.atTime(16, 30)
|
||||
assertThat(defaultPickerDate(LocalTime.of(20, 0), now)).isEqualTo(today)
|
||||
assertThat(defaultPickerDate(LocalTime.of(16, 30), now)).isEqualTo(today)
|
||||
assertThat(defaultPickerDate(LocalTime.of(8, 0), now)).isEqualTo(LocalDate.of(2026, 12, 29))
|
||||
assertThat(defaultPickerDate(LocalTime.of(8, 0), today.atTime(23, 0))).isEqualTo(LocalDate.of(2026, 12, 29))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a time without a date does not land before the floor`() {
|
||||
val now = today.atTime(16, 30)
|
||||
val start = LocalDate.of(2027, 1, 4).atTime(9, 0)
|
||||
assertThat(defaultPickerDate(LocalTime.of(20, 0), now, start)).isEqualTo(LocalDate.of(2027, 1, 4))
|
||||
assertThat(defaultPickerDate(LocalTime.of(8, 0), now, start)).isEqualTo(LocalDate.of(2027, 1, 5))
|
||||
assertThat(defaultPickerDate(LocalTime.of(20, 0), now, today.minusDays(3).atTime(9, 0))).isEqualTo(today)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user