From 5e7918f105d2ea1935e0f74b0f711a484f2f58c6 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 13 Nov 2026 21:16:40 +0100 Subject: [PATCH] refactor(#365): share grouped lead-time rows, keep all-day list in one group --- .../calendula/ui/common/Picker.kt | 77 ++++++++++++------- .../calendula/ui/common/ReminderFormatting.kt | 11 ++- .../calendula/ui/edit/EventEditScreen.kt | 27 ++++--- .../calendar/AllDayReminderEncodingTest.kt | 3 +- .../ui/common/ReminderPresetsTest.kt | 8 +- 5 files changed, 77 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/Picker.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/Picker.kt index b1a7f427..c83289e1 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/Picker.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/Picker.kt @@ -89,9 +89,8 @@ fun ReminderDefaultPicker( // which would strand a hand-entered value with no way to re-check it. val seenCustom = remember { mutableSetOf() } seenCustom += selectedMinutes.filter { it !in presets } - // Presets plus every custom value seen this session, each as its own row, - // split into a sub-day and a day-scale group. - val groups = groupReminderLeadTimes(presets + seenCustom) + // Presets plus every custom value seen this session, each as its own row. + val rows = presets + seenCustom var customExpanded by rememberSaveable { mutableStateOf(false) } var amountText by rememberSaveable { mutableStateOf("") } @@ -126,43 +125,63 @@ fun ReminderDefaultPicker( onClick = { apply(ReminderOverride.None) }, ) Spacer(Modifier.height(24.dp)) - groups.forEachIndexed { groupIndex, group -> - val isLast = groupIndex == groups.lastIndex - val rowCount = group.size + if (isLast) 1 else 0 // + the custom row - group.forEachIndexed { index, minute -> + GroupedLeadTimeRows( + minutes = rows, + row = { minute, position -> val checked = minute in selectedMinutes GroupedRow( title = reminderLeadTimeLabel(minute), summary = leadTimeSummary?.invoke(minute), - position = positionOf(index, rowCount), + position = position, selected = checked, trailing = { Checkbox(checked = checked, onCheckedChange = { toggle(minute) }) }, onClick = { toggle(minute) }, ) - } - if (!isLast) Spacer(Modifier.height(12.dp)) - } - CustomExpandRow( - title = stringResource(R.string.event_edit_reminder_custom), - position = if (groups.isEmpty()) Position.Alone else Position.Bottom, - expanded = customExpanded, - onExpandedChange = { customExpanded = it }, - ) { - CustomReminderEditor( - amountText = amountText, - onAmountChange = { amountText = it }, - unit = unit, - onUnitChange = { unit = it }, - onConfirm = { minutes -> - emit(selectedMinutes + minutes) - amountText = "" - customExpanded = false - }, - ) - } + }, + trailing = { position -> + CustomExpandRow( + title = stringResource(R.string.event_edit_reminder_custom), + position = position, + expanded = customExpanded, + onExpandedChange = { customExpanded = it }, + ) { + CustomReminderEditor( + amountText = amountText, + onAmountChange = { amountText = it }, + unit = unit, + onUnitChange = { unit = it }, + onConfirm = { minutes -> + emit(selectedMinutes + minutes) + amountText = "" + customExpanded = false + }, + ) + } + }, + ) } } +/** + * Lead-time rows laid out as [groupReminderLeadTimes] groups, each a connected + * card, with [trailing] (the Custom row) closing the last one. + */ +@Composable +internal fun GroupedLeadTimeRows( + minutes: Collection, + row: @Composable (minute: Int, position: Position) -> Unit, + trailing: @Composable (position: Position) -> Unit, +) { + val groups = groupReminderLeadTimes(minutes) + groups.forEachIndexed { groupIndex, group -> + val isLast = groupIndex == groups.lastIndex + val rowCount = group.size + if (isLast) 1 else 0 + group.forEachIndexed { index, minute -> row(minute, positionOf(index, rowCount)) } + if (!isLast) Spacer(Modifier.height(12.dp)) + } + trailing(if (groups.isEmpty()) Position.Alone else Position.Bottom) +} + /** * The expanded "Custom" lead-time editor: a tonal card connected to the Custom diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ReminderFormatting.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ReminderFormatting.kt index cea96fca..bcb7aa1c 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ReminderFormatting.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ReminderFormatting.kt @@ -22,10 +22,15 @@ val ALLDAY_REMINDER_PRESETS = listOf(0) + DAY_SCALE_PRESETS /** * Sorts lead times and splits them into a sub-day group and a day-scale group, - * so a long picker list reads as two connected sections. Empty groups are dropped. + * so a long picker list reads as two connected sections. A list with no real + * sub-day lead time (only "at time of event", as for all-day) stays one group. */ -fun groupReminderLeadTimes(minutes: Collection): List> = - minutes.distinct().sorted().partition { it < DAY }.toList().filter { it.isNotEmpty() } +fun groupReminderLeadTimes(minutes: Collection): List> { + val sorted = minutes.distinct().sorted() + if (sorted.isEmpty()) return emptyList() + if (sorted.none { it in 1 until DAY }) return listOf(sorted) + return sorted.partition { it < DAY }.toList().filter { it.isNotEmpty() } +} @StringRes fun reminderUnitLabel(unit: ReminderUnit): Int = when (unit) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/edit/EventEditScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/edit/EventEditScreen.kt index 42177646..f207e573 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/edit/EventEditScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/edit/EventEditScreen.kt @@ -167,7 +167,7 @@ import de.jeanlucmakiola.floret.components.Position import de.jeanlucmakiola.floret.components.groupedShape as floretGroupedShape import de.jeanlucmakiola.floret.components.positionOf import de.jeanlucmakiola.calendula.ui.common.REMINDER_PRESETS -import de.jeanlucmakiola.calendula.ui.common.groupReminderLeadTimes +import de.jeanlucmakiola.calendula.ui.common.GroupedLeadTimeRows import de.jeanlucmakiola.calendula.ui.common.TimePickerAlert import de.jeanlucmakiola.floret.reminders.ReminderUnit import de.jeanlucmakiola.floret.locale.currentLocale @@ -1609,23 +1609,22 @@ private fun ReminderPickerDialog( }, ) { if (!customMode) { - val groups = groupReminderLeadTimes(presets.filterNot { it in alreadyChosen }) - groups.forEachIndexed { groupIndex, group -> - val isLast = groupIndex == groups.lastIndex - val rowCount = group.size + if (isLast) 1 else 0 - group.forEachIndexed { index, minutes -> + GroupedLeadTimeRows( + minutes = presets.filterNot { it in alreadyChosen }, + row = { minutes, position -> GroupedRow( title = reminderLeadTimeLabel(minutes), - position = positionOf(index, rowCount), + position = position, onClick = { onSelect(minutes) }, ) - } - if (!isLast) Spacer(Modifier.height(12.dp)) - } - GroupedRow( - title = stringResource(R.string.event_edit_reminder_custom), - position = if (groups.isEmpty()) Position.Alone else Position.Bottom, - onClick = { customMode = true }, + }, + trailing = { position -> + GroupedRow( + title = stringResource(R.string.event_edit_reminder_custom), + position = position, + onClick = { customMode = true }, + ) + }, ) } else { Row( diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/AllDayReminderEncodingTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/AllDayReminderEncodingTest.kt index 31241add..123c5dc6 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/AllDayReminderEncodingTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/AllDayReminderEncodingTest.kt @@ -1,7 +1,6 @@ package de.jeanlucmakiola.calendula.data.calendar import com.google.common.truth.Truth.assertThat -import de.jeanlucmakiola.calendula.ui.common.ALLDAY_REMINDER_PRESETS import org.junit.jupiter.api.Test import java.time.LocalDate import java.time.LocalTime @@ -61,7 +60,7 @@ class AllDayReminderEncodingTest { fun `round-trips for whole-day lead times across both seasons`() { for (date in listOf(summer, winter)) { for (time in listOf(0, nineAm, 20 * 60)) { - for (semantic in ALLDAY_REMINDER_PRESETS) { + for (semantic in listOf(0, 1_440, 2_880, 4_320, 5_760, 7_200, 10_080, 20_160, 30_240)) { val raw = toProviderAllDayMinutes(semantic, date, berlin, time) assertThat(fromProviderAllDayMinutes(raw, date, berlin, time)) .isEqualTo(semantic) diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ReminderPresetsTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ReminderPresetsTest.kt index f4713ea8..0e5d016c 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ReminderPresetsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ReminderPresetsTest.kt @@ -30,9 +30,15 @@ class ReminderPresetsTest { .containsExactly(listOf(0, 10, 45), listOf(1_440, 2_880)).inOrder() } + @Test + fun `at time of event alone joins the day-scale group`() { + assertThat(groupReminderLeadTimes(ALLDAY_REMINDER_PRESETS)) + .containsExactly(ALLDAY_REMINDER_PRESETS) + } + @Test fun `empty groups are dropped`() { - assertThat(groupReminderLeadTimes(listOf(1_440, 0).drop(1))).containsExactly(listOf(0)) + assertThat(groupReminderLeadTimes(listOf(10))).containsExactly(listOf(10)) assertThat(groupReminderLeadTimes(listOf(2_880))).containsExactly(listOf(2_880)) assertThat(groupReminderLeadTimes(emptyList())).isEmpty() }