Pick start/due date and time in one sheet with quick presets (#101)
This commit is contained in:
1 parent
1463b08d6f
commit
4376a48b1e
5 files changed
+265
-72
No files matched your search
@@ -0,0 +1,175 @@
|
||||
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
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
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.LocalUse24HourFormat
|
||||
import de.jeanlucmakiola.agendula.ui.common.TimePickerAlert
|
||||
import de.jeanlucmakiola.agendula.ui.common.formatMinuteOfDay
|
||||
import de.jeanlucmakiola.agendula.ui.common.localToInstant
|
||||
import de.jeanlucmakiola.agendula.ui.common.toLocalTime
|
||||
import de.jeanlucmakiola.floret.components.FullScreenPicker
|
||||
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 java.time.LocalDate
|
||||
import java.time.LocalTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.format.DateTimeFormatter
|
||||
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.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
internal fun DateTimePickerSheet(
|
||||
title: String,
|
||||
initial: Instant?,
|
||||
allDay: Boolean,
|
||||
onResult: (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 showCalendar by rememberSaveable { mutableStateOf(false) }
|
||||
var showClock by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
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])
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
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),
|
||||
position = positionOf(index, dateRows),
|
||||
checked = quick == quickDate,
|
||||
onClick = { pickDate(day) },
|
||||
)
|
||||
}
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.picker_date_pick),
|
||||
summary = dayLabel(date).takeIf { quickDate == null },
|
||||
position = positionOf(QuickDate.entries.size, dateRows),
|
||||
checked = quickDate == null,
|
||||
onClick = { showCalendar = true },
|
||||
)
|
||||
|
||||
if (!allDay) {
|
||||
val quickTime = QuickTime.of(time)
|
||||
val timeRows = QuickTime.entries.size + 1
|
||||
Spacer(Modifier.height(8.dp))
|
||||
GroupedSectionHeader(stringResource(R.string.picker_section_time))
|
||||
QuickTime.entries.forEachIndexed { index, quick ->
|
||||
GroupedRow(
|
||||
title = stringResource(quick.labelRes),
|
||||
summary = timeLabel(quick.time),
|
||||
position = positionOf(index, timeRows),
|
||||
checked = quick == quickTime,
|
||||
onClick = { commit(date, quick.time) },
|
||||
)
|
||||
}
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.picker_time_custom),
|
||||
summary = timeLabel(time).takeIf { quickTime == null },
|
||||
position = positionOf(QuickTime.entries.size, timeRows),
|
||||
checked = quickTime == null,
|
||||
onClick = { showClock = true },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showCalendar) {
|
||||
// M3's DatePicker speaks UTC millis.
|
||||
val state = rememberDatePickerState(
|
||||
initialSelectedDateMillis = date.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(),
|
||||
)
|
||||
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,
|
||||
onConfirm = {
|
||||
showClock = false
|
||||
commit(date, it)
|
||||
},
|
||||
onDismiss = { showClock = false },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private val QuickDate.labelRes: Int
|
||||
get() = when (this) {
|
||||
QuickDate.Today -> R.string.picker_date_today
|
||||
QuickDate.Tomorrow -> R.string.picker_date_tomorrow
|
||||
QuickDate.NextWeek -> R.string.picker_date_next_week
|
||||
}
|
||||
|
||||
private val QuickTime.labelRes: Int
|
||||
get() = when (this) {
|
||||
QuickTime.Morning -> R.string.picker_time_morning
|
||||
QuickTime.Noon -> R.string.picker_time_noon
|
||||
QuickTime.Afternoon -> R.string.picker_time_afternoon
|
||||
QuickTime.Evening -> R.string.picker_time_evening
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package de.jeanlucmakiola.agendula.ui.edit
|
||||
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalTime
|
||||
|
||||
/** The one-tap dates the start / due picker offers above its calendar row. */
|
||||
internal enum class QuickDate(val daysFromToday: Long) {
|
||||
Today(0),
|
||||
Tomorrow(1),
|
||||
NextWeek(7),
|
||||
;
|
||||
|
||||
fun resolve(today: LocalDate): LocalDate = today.plusDays(daysFromToday)
|
||||
|
||||
companion object {
|
||||
/** The quick date [date] matches, or null when it needs the calendar. */
|
||||
fun of(date: LocalDate, today: LocalDate): QuickDate? = entries.firstOrNull { it.resolve(today) == date }
|
||||
}
|
||||
}
|
||||
|
||||
/** The one-tap times of day the start / due picker offers. */
|
||||
internal enum class QuickTime(val time: LocalTime) {
|
||||
Morning(LocalTime.of(8, 0)),
|
||||
Noon(LocalTime.of(12, 0)),
|
||||
Afternoon(LocalTime.of(15, 0)),
|
||||
Evening(LocalTime.of(20, 0)),
|
||||
;
|
||||
|
||||
companion object {
|
||||
fun of(time: LocalTime): QuickTime? = entries.firstOrNull { it.time == time }
|
||||
}
|
||||
}
|
||||
|
||||
/** 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)
|
||||
@@ -81,8 +81,6 @@ import androidx.compose.material.icons.rounded.Schedule
|
||||
import androidx.compose.material.icons.rounded.Search
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.DatePicker
|
||||
import androidx.compose.material3.DatePickerDialog
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
@@ -96,11 +94,8 @@ import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TimePicker
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.material3.rememberDatePickerState
|
||||
import androidx.compose.material3.rememberTimePickerState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
@@ -138,16 +133,12 @@ import de.jeanlucmakiola.floret.components.InlineTextField
|
||||
import de.jeanlucmakiola.agendula.ui.common.priorityFill
|
||||
import de.jeanlucmakiola.floret.time.formatDate
|
||||
import de.jeanlucmakiola.floret.time.formatTime
|
||||
import de.jeanlucmakiola.agendula.ui.common.localToInstant
|
||||
import de.jeanlucmakiola.floret.components.pastelize
|
||||
import de.jeanlucmakiola.floret.components.positionOf
|
||||
import de.jeanlucmakiola.agendula.domain.allDayInstantOf
|
||||
import de.jeanlucmakiola.agendula.domain.calendarDate
|
||||
import de.jeanlucmakiola.agendula.ui.common.toLocalTime
|
||||
import de.jeanlucmakiola.agendula.ui.tasklist.priorityLabel
|
||||
import java.time.LocalTime
|
||||
import java.time.ZoneId
|
||||
import java.time.ZoneOffset
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
|
||||
@@ -665,7 +656,8 @@ private fun EditContent(
|
||||
|
||||
pickerTarget?.let { target ->
|
||||
val current = if (target == PickerTarget.Start) state.start else state.due
|
||||
DateTimePickerFlow(
|
||||
DateTimePickerSheet(
|
||||
title = stringResource(if (target == PickerTarget.Start) R.string.edit_start_label else R.string.edit_due_label),
|
||||
initial = current,
|
||||
allDay = state.isAllDay,
|
||||
onResult = {
|
||||
@@ -982,68 +974,6 @@ private fun LabelledValueLayout(label: @Composable () -> Unit, value: @Composabl
|
||||
}
|
||||
}
|
||||
|
||||
private fun nowInstant(): Instant = Instant.fromEpochMilliseconds(System.currentTimeMillis())
|
||||
|
||||
/** Pick a date, then (unless all-day) a time; emits the combined instant once. */
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
private fun DateTimePickerFlow(
|
||||
initial: Instant?,
|
||||
allDay: Boolean,
|
||||
onResult: (Instant) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
var pendingDate by rememberSaveable { mutableStateOf<java.time.LocalDate?>(null) }
|
||||
var showTime by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
if (!showTime) {
|
||||
// M3's DatePicker speaks UTC millis. An all-day value is already UTC-based,
|
||||
// a timed one is read in the device zone — calendarDate picks the right frame
|
||||
// so the dialog opens on the day the rest of the UI shows.
|
||||
val initialMillis = (initial ?: nowInstant())
|
||||
.calendarDate(allDay).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli()
|
||||
val dateState = rememberDatePickerState(initialSelectedDateMillis = initialMillis)
|
||||
DatePickerDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
val millis = dateState.selectedDateMillis ?: run { onDismiss(); return@TextButton }
|
||||
val date = java.time.Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).toLocalDate()
|
||||
if (allDay) {
|
||||
onResult(allDayInstantOf(date))
|
||||
} else {
|
||||
pendingDate = date
|
||||
showTime = true
|
||||
}
|
||||
}) { Text(stringResource(android.R.string.ok)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) }
|
||||
},
|
||||
) { DatePicker(state = dateState) }
|
||||
} else {
|
||||
val base = initial ?: nowInstant()
|
||||
val timeState = rememberTimePickerState(
|
||||
initialHour = base.toLocalTime().hour,
|
||||
initialMinute = base.toLocalTime().minute,
|
||||
is24Hour = LocalUse24HourFormat.current,
|
||||
)
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
text = { TimePicker(state = timeState) },
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
val date = pendingDate ?: return@TextButton
|
||||
onResult(localToInstant(date, LocalTime.of(timeState.hour, timeState.minute)))
|
||||
}) { Text(stringResource(android.R.string.ok)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) }
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** The target list, full-screen and grouped under each list's account. */
|
||||
@Composable
|
||||
private fun ListPickerDialog(
|
||||
|
||||
@@ -98,6 +98,18 @@
|
||||
<string name="edit_title_hint">Task title</string>
|
||||
<string name="edit_when">When</string>
|
||||
<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>
|
||||
<string name="picker_time_noon">Noon</string>
|
||||
<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>
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package de.jeanlucmakiola.agendula.ui.edit
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalTime
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class QuickPicksTest {
|
||||
|
||||
private val today = LocalDate.of(2026, 12, 28)
|
||||
|
||||
@Test
|
||||
fun `quick dates resolve relative to today across a year boundary`() {
|
||||
assertThat(QuickDate.Today.resolve(today)).isEqualTo(today)
|
||||
assertThat(QuickDate.Tomorrow.resolve(today)).isEqualTo(LocalDate.of(2026, 12, 29))
|
||||
assertThat(QuickDate.NextWeek.resolve(today)).isEqualTo(LocalDate.of(2027, 1, 4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a date maps back to its quick date or to none`() {
|
||||
assertThat(QuickDate.of(today, today)).isEqualTo(QuickDate.Today)
|
||||
assertThat(QuickDate.of(LocalDate.of(2027, 1, 4), today)).isEqualTo(QuickDate.NextWeek)
|
||||
assertThat(QuickDate.of(LocalDate.of(2026, 12, 30), today)).isNull()
|
||||
assertThat(QuickDate.of(today.minusDays(1), today)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a time maps to its preset only on an exact match`() {
|
||||
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()
|
||||
}
|
||||
|
||||
@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))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user