Free position and weekday choice for monthly repeats, move start onto the first match (#520)
This commit is contained in:
1 parent
7fa30ab155
commit
09f5a7c21a
5 files changed
+189
-64
No files matched your search
@@ -52,6 +52,18 @@ data class MonthlyWeekday(val ordinal: Int, val day: DayOfWeek) {
|
||||
/** The RRULE BYDAY token, e.g. "2TU" or "-1SA". */
|
||||
val token: String get() = "$ordinal${RRULE_DAY_CODES.getValue(day)}"
|
||||
|
||||
/**
|
||||
* The first date on or after [date] at this position: later in [date]'s
|
||||
* month, or in the next month that has one (a fifth weekday can be months away).
|
||||
*/
|
||||
fun firstOnOrAfter(date: LocalDate): LocalDate {
|
||||
var month = LocalDate(date.year, date.month, 1)
|
||||
while (true) {
|
||||
dateIn(month.year, month.month.number)?.takeIf { it >= date }?.let { return it }
|
||||
month = month.plus(1, DateTimeUnit.MONTH)
|
||||
}
|
||||
}
|
||||
|
||||
/** This weekday's date in [year]/[month], or null when the month has no such day. */
|
||||
fun dateIn(year: Int, month: Int): LocalDate? {
|
||||
val first = LocalDate(year, month, 1)
|
||||
@@ -79,15 +91,8 @@ data class MonthlyWeekday(val ordinal: Int, val day: DayOfWeek) {
|
||||
return MonthlyWeekday(ordinal, day)
|
||||
}
|
||||
|
||||
/**
|
||||
* The weekday positions [date] stands at, to offer as prefills: its
|
||||
* ordinal within the month, plus "last" when it falls in the final
|
||||
* seven days.
|
||||
*/
|
||||
fun optionsFor(date: LocalDate): List<MonthlyWeekday> = listOfNotNull(
|
||||
MonthlyWeekday((date.day - 1) / 7 + 1, date.dayOfWeek),
|
||||
MonthlyWeekday(LAST, date.dayOfWeek).takeIf { date.plus(7, DateTimeUnit.DAY).month != date.month },
|
||||
)
|
||||
/** [date]'s own position: its weekday, counted from the start of its month (1..5). */
|
||||
fun of(date: LocalDate): MonthlyWeekday = MonthlyWeekday((date.day - 1) / 7 + 1, date.dayOfWeek)
|
||||
|
||||
private val TOKEN = Regex("""([+-]?\d)(MO|TU|WE|TH|FR|SA|SU)""")
|
||||
}
|
||||
@@ -249,3 +254,14 @@ private fun parseUntilDate(raw: String, zone: TimeZone): LocalDate? {
|
||||
if (raw.length < 15 || raw[8] != 'T') return date
|
||||
return parseBasicDateTime(raw)?.toInstant(TimeZone.UTC)?.toLocalDateTime(zone)?.date
|
||||
}
|
||||
|
||||
/**
|
||||
* The form moved forward to the first day its monthly weekday-position rule
|
||||
* fires on, keeping time of day and length (#520); unchanged for any other rule
|
||||
* or when the start already matches.
|
||||
*/
|
||||
fun EventForm.alignedToRecurrence(deviceZone: TimeZone): EventForm {
|
||||
val position = rrule?.let { parseSimpleRecurrence(it, deviceZone) }?.monthlyWeekday ?: return this
|
||||
val target = position.firstOnOrAfter(start.date)
|
||||
return shiftedByDays((target.toEpochDays() - start.date.toEpochDays()).toInt(), deviceZone)
|
||||
}
|
||||
@@ -1743,13 +1743,15 @@ private fun RecurrencePickerDialog(
|
||||
(parsed?.byDays?.takeIf { it.isNotEmpty() } ?: setOf(startDay)).toMask(),
|
||||
)
|
||||
}
|
||||
// Monthly: the BYDAY token of a weekday position ("2TU"), null for "on day N".
|
||||
var monthlyWeekdayToken by rememberSaveable { mutableStateOf(parsed?.monthlyWeekday?.token) }
|
||||
val monthlyOptions = remember(startDate, parsed) {
|
||||
val fromStart = MonthlyWeekday.optionsFor(startDate)
|
||||
val kept = parsed?.monthlyWeekday?.takeIf { it !in fromStart }
|
||||
listOf<MonthlyWeekday?>(null) + fromStart + listOfNotNull(kept)
|
||||
// Monthly: "on day N" or a weekday position, the latter prefilled from the
|
||||
// rule or else the start date and kept as a BYDAY token ("2TU", "-1SA").
|
||||
var byWeekday by rememberSaveable { mutableStateOf(parsed?.monthlyWeekday != null) }
|
||||
var weekdayToken by rememberSaveable {
|
||||
mutableStateOf((parsed?.monthlyWeekday ?: MonthlyWeekday.of(startDate)).token)
|
||||
}
|
||||
val weekdayPosition = MonthlyWeekday.parse(weekdayToken) ?: MonthlyWeekday.of(startDate)
|
||||
var showPositionPicker by rememberSaveable { mutableStateOf(false) }
|
||||
var showWeekdayPicker by rememberSaveable { mutableStateOf(false) }
|
||||
var endMode by rememberSaveable {
|
||||
mutableStateOf(
|
||||
when (parsed?.end) {
|
||||
@@ -1797,16 +1799,14 @@ private fun RecurrencePickerDialog(
|
||||
interval = interval,
|
||||
end = customEnd,
|
||||
byDays = if (freq == RecurrenceFreq.Weekly) daysMask.toDaySet() else emptySet(),
|
||||
monthlyWeekday = if (freq == RecurrenceFreq.Monthly) {
|
||||
monthlyWeekdayToken?.let(MonthlyWeekday::parse)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
monthlyWeekday = weekdayPosition.takeIf { freq == RecurrenceFreq.Monthly && byWeekday },
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val customResult: String? = customRule?.toRRule()
|
||||
// Saving a weekday position moves the start onto its first match, so preview from there.
|
||||
val previewStart = customRule?.monthlyWeekday?.firstOnOrAfter(startDate) ?: startDate
|
||||
|
||||
FullScreenPicker(
|
||||
title = stringResource(R.string.event_detail_recurrence),
|
||||
@@ -1877,7 +1877,7 @@ private fun RecurrencePickerDialog(
|
||||
// Rendered even while incomplete (as an empty line), so the
|
||||
// controls below don't shift as it comes and goes.
|
||||
Text(
|
||||
text = customRule?.let { nextOccurrencesText(it, startDate, locale) }.orEmpty(),
|
||||
text = customRule?.let { nextOccurrencesText(it, previewStart, locale) }.orEmpty(),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier
|
||||
@@ -1958,28 +1958,48 @@ private fun RecurrencePickerDialog(
|
||||
}
|
||||
}
|
||||
|
||||
// Monthly: same day of the month, or a weekday position derived
|
||||
// from the start date (plus a kept imported one).
|
||||
// Monthly: same day of the month, or a free weekday position
|
||||
// whose position and weekday open their own pickers.
|
||||
AnimatedVisibility(
|
||||
visible = freq == RecurrenceFreq.Monthly,
|
||||
enter = expandEnter(),
|
||||
exit = collapseExit(),
|
||||
) {
|
||||
Column(modifier = Modifier.padding(top = 16.dp)) {
|
||||
monthlyOptions.forEachIndexed { index, option ->
|
||||
GroupedRow(
|
||||
title = if (option == null) {
|
||||
stringResource(R.string.event_edit_recurrence_monthly_day, startDate.day)
|
||||
} else {
|
||||
stringResource(
|
||||
monthlyWeekdayLabel(option.ordinal),
|
||||
option.day.toJavaDayOfWeek().getDisplayName(JavaTextStyle.FULL, locale),
|
||||
)
|
||||
},
|
||||
position = positionOf(index, monthlyOptions.size),
|
||||
checked = option?.token == monthlyWeekdayToken,
|
||||
onClick = { monthlyWeekdayToken = option?.token },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.event_edit_recurrence_monthly_day, startDate.day),
|
||||
position = Position.Top,
|
||||
checked = !byWeekday,
|
||||
onClick = { byWeekday = false },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(
|
||||
monthlyWeekdayLabel(weekdayPosition.ordinal),
|
||||
weekdayPosition.day.toJavaDayOfWeek().getDisplayName(JavaTextStyle.FULL, locale),
|
||||
),
|
||||
position = if (byWeekday) Position.Middle else Position.Bottom,
|
||||
checked = byWeekday,
|
||||
onClick = { byWeekday = true },
|
||||
)
|
||||
AnimatedVisibility(
|
||||
visible = byWeekday,
|
||||
enter = expandEnter(),
|
||||
exit = collapseExit(),
|
||||
) {
|
||||
Column {
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.event_edit_recurrence_position),
|
||||
summary = stringResource(weekdayPositionName(weekdayPosition.ordinal)),
|
||||
position = Position.Middle,
|
||||
onClick = { showPositionPicker = true },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.event_edit_recurrence_weekday),
|
||||
summary = weekdayName(weekdayPosition.day, locale),
|
||||
position = Position.Bottom,
|
||||
onClick = { showWeekdayPicker = true },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2053,6 +2073,27 @@ private fun RecurrencePickerDialog(
|
||||
}
|
||||
}
|
||||
|
||||
if (showPositionPicker) {
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.event_edit_recurrence_position),
|
||||
options = MONTHLY_POSITIONS,
|
||||
selected = weekdayPosition.ordinal,
|
||||
label = { stringResource(weekdayPositionName(it)) },
|
||||
onSelect = { weekdayToken = weekdayPosition.copy(ordinal = it).token },
|
||||
onDismiss = { showPositionPicker = false },
|
||||
)
|
||||
}
|
||||
if (showWeekdayPicker) {
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.event_edit_recurrence_weekday),
|
||||
options = (0 until 7).map { DayOfWeek(((firstDayOfWeek.isoDayNumber - 1 + it) % 7) + 1) },
|
||||
selected = weekdayPosition.day,
|
||||
label = { weekdayName(it, locale) },
|
||||
onSelect = { weekdayToken = weekdayPosition.copy(day = it).token },
|
||||
onDismiss = { showWeekdayPicker = false },
|
||||
)
|
||||
}
|
||||
|
||||
if (showUntilPicker) {
|
||||
CalendarDatePickerDialog(
|
||||
initial = untilDate
|
||||
@@ -2136,6 +2177,21 @@ private fun recurrencePresetLabel(freq: RecurrenceFreq): Int = when (freq) {
|
||||
RecurrenceFreq.Yearly -> R.string.recurrence_yearly
|
||||
}
|
||||
|
||||
private val MONTHLY_POSITIONS = (1..MonthlyWeekday.MAX_ORDINAL).toList() + MonthlyWeekday.LAST
|
||||
|
||||
private fun weekdayName(day: DayOfWeek, locale: Locale): String =
|
||||
day.toJavaDayOfWeek().getDisplayName(JavaTextStyle.FULL_STANDALONE, locale)
|
||||
.replaceFirstChar { it.titlecase(locale) }
|
||||
|
||||
private fun weekdayPositionName(ordinal: Int): Int = when (ordinal) {
|
||||
1 -> R.string.recurrence_position_first
|
||||
2 -> R.string.recurrence_position_second
|
||||
3 -> R.string.recurrence_position_third
|
||||
4 -> R.string.recurrence_position_fourth
|
||||
5 -> R.string.recurrence_position_fifth
|
||||
else -> R.string.recurrence_position_last
|
||||
}
|
||||
|
||||
private fun monthlyWeekdayLabel(ordinal: Int): Int = when (ordinal) {
|
||||
1 -> R.string.event_edit_recurrence_monthly_weekday_first
|
||||
2 -> R.string.event_edit_recurrence_monthly_weekday_second
|
||||
|
||||
@@ -31,6 +31,7 @@ import de.jeanlucmakiola.calendula.domain.EventAttendee
|
||||
import de.jeanlucmakiola.calendula.domain.EventColorOption
|
||||
import de.jeanlucmakiola.calendula.domain.EventDetail
|
||||
import de.jeanlucmakiola.calendula.domain.EventForm
|
||||
import de.jeanlucmakiola.calendula.domain.alignedToRecurrence
|
||||
import de.jeanlucmakiola.calendula.domain.nextFullHour
|
||||
import de.jeanlucmakiola.calendula.domain.toFormTime
|
||||
import de.jeanlucmakiola.calendula.domain.resolvedZone
|
||||
@@ -758,8 +759,11 @@ class EventEditViewModel @Inject constructor(
|
||||
applyDefault { it.copy(calendarId = shown.calendarId, colorKey = shown.colorKey, color = shown.color) }
|
||||
}
|
||||
|
||||
/** Bare RRULE value from the recurrence picker; null = does not repeat. */
|
||||
fun setRecurrence(rrule: String?) = edit { it.copy(rrule = rrule) }
|
||||
/**
|
||||
* Bare RRULE value from the recurrence picker; null = does not repeat. A
|
||||
* monthly weekday-position rule moves the start onto its first matching day (#520).
|
||||
*/
|
||||
fun setRecurrence(rrule: String?) = edit { it.copy(rrule = rrule).alignedToRecurrence(zoneSource.current()) }
|
||||
|
||||
fun addReminder(minutes: Int) {
|
||||
_remindersTouched.value = true
|
||||
|
||||
@@ -230,6 +230,15 @@
|
||||
<string name="event_edit_recurrence_monthly_weekday_fourth">On the fourth %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_fifth">On the fifth %1$s</string>
|
||||
<string name="event_edit_recurrence_monthly_weekday_last">On the last %1$s</string>
|
||||
<!-- Monthly weekday position: picker titles and its options (#520) -->
|
||||
<string name="event_edit_recurrence_position">Position</string>
|
||||
<string name="event_edit_recurrence_weekday">Weekday</string>
|
||||
<string name="recurrence_position_first">First</string>
|
||||
<string name="recurrence_position_second">Second</string>
|
||||
<string name="recurrence_position_third">Third</string>
|
||||
<string name="recurrence_position_fourth">Fourth</string>
|
||||
<string name="recurrence_position_fifth">Fifth</string>
|
||||
<string name="recurrence_position_last">Last</string>
|
||||
<string name="event_edit_error_recurrence_ends_before_start">Repeats end before the event starts</string>
|
||||
<string name="event_availability_busy">Busy</string>
|
||||
<string name="event_access_default">Default</string>
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.jeanlucmakiola.calendula.domain
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.datetime.DayOfWeek
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@@ -77,34 +78,73 @@ class MonthlyWeekdayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefill offers the start date's ordinal`() {
|
||||
// 10 Nov 2026 is the second Tuesday, not in the last week.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 11, 10)))
|
||||
.containsExactly(MonthlyWeekday(2, DayOfWeek.TUESDAY))
|
||||
fun `prefill is the start date's own position`() {
|
||||
// 10 Nov 2026 is the second Tuesday; 31 Oct 2026 the fifth Saturday.
|
||||
assertThat(MonthlyWeekday.of(LocalDate(2026, 11, 10))).isEqualTo(MonthlyWeekday(2, DayOfWeek.TUESDAY))
|
||||
assertThat(MonthlyWeekday.of(LocalDate(2026, 10, 31))).isEqualTo(MonthlyWeekday(5, DayOfWeek.SATURDAY))
|
||||
assertThat(MonthlyWeekday.of(LocalDate(2026, 11, 1))).isEqualTo(MonthlyWeekday(1, DayOfWeek.SUNDAY))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `prefill adds last when the start is in the month's final seven days`() {
|
||||
// 24 Feb 2026: fourth Tuesday and also the last one.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 2, 24)))
|
||||
.containsExactly(
|
||||
MonthlyWeekday(4, DayOfWeek.TUESDAY),
|
||||
MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.TUESDAY),
|
||||
).inOrder()
|
||||
// 31 Oct 2026: fifth Saturday.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 10, 31)))
|
||||
.containsExactly(
|
||||
MonthlyWeekday(5, DayOfWeek.SATURDAY),
|
||||
MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.SATURDAY),
|
||||
).inOrder()
|
||||
// 24 Nov 2026 is the fourth and last Tuesday; 23 Nov is a fourth but not last Monday.
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 11, 24)))
|
||||
.containsExactly(
|
||||
MonthlyWeekday(4, DayOfWeek.TUESDAY),
|
||||
MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.TUESDAY),
|
||||
).inOrder()
|
||||
assertThat(MonthlyWeekday.optionsFor(LocalDate(2026, 11, 23)))
|
||||
.containsExactly(MonthlyWeekday(4, DayOfWeek.MONDAY))
|
||||
fun `first match on or after a date`() {
|
||||
val secondTue = MonthlyWeekday(2, DayOfWeek.TUESDAY)
|
||||
// Already a match: stays.
|
||||
assertThat(secondTue.firstOnOrAfter(LocalDate(2026, 11, 10))).isEqualTo(LocalDate(2026, 11, 10))
|
||||
// Before it in the month: later the same month.
|
||||
assertThat(secondTue.firstOnOrAfter(LocalDate(2026, 11, 2))).isEqualTo(LocalDate(2026, 11, 10))
|
||||
// Past it: next month.
|
||||
assertThat(secondTue.firstOnOrAfter(LocalDate(2026, 11, 11))).isEqualTo(LocalDate(2026, 12, 8))
|
||||
// Fifth Saturday: Nov and Dec 2026 have none, so across the year end to 30 Jan 2027.
|
||||
assertThat(MonthlyWeekday(5, DayOfWeek.SATURDAY).firstOnOrAfter(LocalDate(2026, 11, 1)))
|
||||
.isEqualTo(LocalDate(2027, 1, 30))
|
||||
// Last Thursday: 31 Dec 2026, then across the year end to 28 Jan 2027.
|
||||
val lastThu = MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.THURSDAY)
|
||||
assertThat(lastThu.firstOnOrAfter(LocalDate(2026, 12, 20))).isEqualTo(LocalDate(2026, 12, 31))
|
||||
assertThat(lastThu.firstOnOrAfter(LocalDate(2027, 1, 1))).isEqualTo(LocalDate(2027, 1, 28))
|
||||
}
|
||||
|
||||
private fun form(start: LocalDateTime, end: LocalDateTime, rrule: String, isAllDay: Boolean = false) =
|
||||
EventForm(calendarId = 1L, isAllDay = isAllDay, start = FormTime(start), end = FormTime(end), rrule = rrule)
|
||||
|
||||
private val berlin = TimeZone.of("Europe/Berlin")
|
||||
|
||||
@Test
|
||||
fun `a timed start moves to the first matching day keeping time and length`() {
|
||||
// Thu 3 Dec 2026 10:00–11:30, rule "fifth Saturday" → Sat 30 Jan 2027.
|
||||
val moved = form(
|
||||
LocalDateTime(2026, 12, 3, 10, 0),
|
||||
LocalDateTime(2026, 12, 3, 11, 30),
|
||||
"FREQ=MONTHLY;BYDAY=5SA",
|
||||
).alignedToRecurrence(berlin)
|
||||
assertThat(moved.start.dateTime).isEqualTo(LocalDateTime(2027, 1, 30, 10, 0))
|
||||
assertThat(moved.end.dateTime).isEqualTo(LocalDateTime(2027, 1, 30, 11, 30))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an all-day span moves by whole days`() {
|
||||
// All-day Sat 26 Dec – Sun 27 Dec 2026 (end exclusive Mon 28), rule "last Thursday" → Thu 31 Dec.
|
||||
val moved = form(
|
||||
LocalDateTime(2026, 12, 26, 0, 0),
|
||||
LocalDateTime(2026, 12, 28, 0, 0),
|
||||
"FREQ=MONTHLY;BYDAY=-1TH",
|
||||
isAllDay = true,
|
||||
).alignedToRecurrence(berlin)
|
||||
assertThat(moved.start.dateTime).isEqualTo(LocalDateTime(2026, 12, 31, 0, 0))
|
||||
assertThat(moved.end.dateTime).isEqualTo(LocalDateTime(2027, 1, 2, 0, 0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a matching start or another rule leaves the form alone`() {
|
||||
val matching = form(
|
||||
LocalDateTime(2026, 11, 10, 9, 0),
|
||||
LocalDateTime(2026, 11, 10, 10, 0),
|
||||
"FREQ=MONTHLY;INTERVAL=2;BYDAY=2TU;COUNT=5",
|
||||
)
|
||||
assertThat(matching.alignedToRecurrence(berlin)).isEqualTo(matching)
|
||||
val weekly = matching.copy(rrule = "FREQ=WEEKLY;BYDAY=FR")
|
||||
assertThat(weekly.alignedToRecurrence(berlin)).isEqualTo(weekly)
|
||||
val unsupported = matching.copy(rrule = "FREQ=MONTHLY;BYDAY=1MO,3MO")
|
||||
assertThat(unsupported.alignedToRecurrence(berlin)).isEqualTo(unsupported)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user