refactor(#365): share grouped lead-time rows, keep all-day list in one group
This commit is contained in:
1 parent
2a452125eb
commit
5e7918f105
5 files changed
+77
-49
No files matched your search
@@ -89,9 +89,8 @@ fun ReminderDefaultPicker(
|
||||
// which would strand a hand-entered value with no way to re-check it.
|
||||
val seenCustom = remember { mutableSetOf<Int>() }
|
||||
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<Int>,
|
||||
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
|
||||
|
||||
@@ -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<Int>): List<List<Int>> =
|
||||
minutes.distinct().sorted().partition { it < DAY }.toList().filter { it.isNotEmpty() }
|
||||
fun groupReminderLeadTimes(minutes: Collection<Int>): List<List<Int>> {
|
||||
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) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
+1
-2
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user