fix(#520): review - realign on start change, keep DTSTART on unchanged rule, last weekday prefill

This commit is contained in:
makiolaj committed 2026-12-02 18:10:02 +01:00
1 parent 09f5a7c21a
commit 966de3011e
5 files changed
+105 -7

No files matched your search

@@ -94,6 +94,10 @@ data class MonthlyWeekday(val ordinal: Int, val day: DayOfWeek) {
/** [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)
/** The position offered for [date]: its own, with a fifth weekday offered as the last one. */
fun suggestedFor(date: LocalDate): MonthlyWeekday =
of(date).let { if (it.ordinal == MAX_ORDINAL) it.copy(ordinal = LAST) else it }
private val TOKEN = Regex("""([+-]?\d)(MO|TU|WE|TH|FR|SA|SU)""")
}
}
@@ -265,3 +269,15 @@ fun EventForm.alignedToRecurrence(deviceZone: TimeZone): EventForm {
val target = position.firstOnOrAfter(start.date)
return shiftedByDays((target.toEpochDays() - start.date.toEpochDays()).toInt(), deviceZone)
}
/**
* The form repeating by [rrule]. The start only moves onto the rule's first day
* when the weekday position is new, so re-confirming a series' own rule never
* shifts its start.
*/
fun EventForm.withRecurrence(rrule: String?, deviceZone: TimeZone): EventForm {
val before = this.rrule?.let { parseSimpleRecurrence(it, deviceZone) }?.monthlyWeekday
val after = rrule?.let { parseSimpleRecurrence(it, deviceZone) }?.monthlyWeekday
val next = copy(rrule = rrule)
return if (after != null && after != before) next.alignedToRecurrence(deviceZone) else next
}
@@ -1747,9 +1747,9 @@ private fun RecurrencePickerDialog(
// 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)
mutableStateOf((parsed?.monthlyWeekday ?: MonthlyWeekday.suggestedFor(startDate)).token)
}
val weekdayPosition = MonthlyWeekday.parse(weekdayToken) ?: MonthlyWeekday.of(startDate)
val weekdayPosition = MonthlyWeekday.parse(weekdayToken) ?: MonthlyWeekday.suggestedFor(startDate)
var showPositionPicker by rememberSaveable { mutableStateOf(false) }
var showWeekdayPicker by rememberSaveable { mutableStateOf(false) }
var endMode by rememberSaveable {
@@ -32,6 +32,7 @@ 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.withRecurrence
import de.jeanlucmakiola.calendula.domain.nextFullHour
import de.jeanlucmakiola.calendula.domain.toFormTime
import de.jeanlucmakiola.calendula.domain.resolvedZone
@@ -760,10 +761,10 @@ class EventEditViewModel @Inject constructor(
}
/**
* 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).
* Bare RRULE value from the recurrence picker; null = does not repeat. A new
* monthly weekday position moves the start onto its first matching day (#520).
*/
fun setRecurrence(rrule: String?) = edit { it.copy(rrule = rrule).alignedToRecurrence(zoneSource.current()) }
fun setRecurrence(rrule: String?) = edit { it.withRecurrence(rrule, zoneSource.current()) }
fun addReminder(minutes: Int) {
_remindersTouched.value = true
@@ -1042,8 +1043,10 @@ class EventEditViewModel @Inject constructor(
_saveState.value = SaveUiState.Idle
}
/** A new start date under a monthly weekday-position rule moves on to the rule's next day (#520). */
private fun moveStart(transform: (LocalDateTime) -> LocalDateTime) = edit { form ->
form.withStart(transform(form.start.dateTime), form.resolvedZone(zoneSource.current()), opened(form)?.start)
val moved = form.withStart(transform(form.start.dateTime), form.resolvedZone(zoneSource.current()), opened(form)?.start)
if (moved.start.date != form.start.date) moved.alignedToRecurrence(zoneSource.current()) else moved
}
private fun moveEnd(transform: (LocalDateTime) -> LocalDateTime) = edit { form ->
@@ -183,4 +183,33 @@ class MonthlyWeekdayTest {
assertThat(lastEveryOther.upcomingOccurrences(LocalDate(2026, 11, 27), limit = 5))
.containsExactly(LocalDate(2026, 11, 27), LocalDate(2027, 1, 29)).inOrder()
}
@Test
fun `a fifth weekday is suggested as the last one`() {
assertThat(MonthlyWeekday.suggestedFor(LocalDate(2026, 10, 31)))
.isEqualTo(MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.SATURDAY))
assertThat(MonthlyWeekday.suggestedFor(LocalDate(2026, 12, 29)))
.isEqualTo(MonthlyWeekday(MonthlyWeekday.LAST, DayOfWeek.TUESDAY))
// The fourth Tuesday of December 2026 is not its last, but stays the fourth.
assertThat(MonthlyWeekday.suggestedFor(LocalDate(2026, 12, 22)))
.isEqualTo(MonthlyWeekday(4, DayOfWeek.TUESDAY))
}
@Test
fun `withRecurrence aligns only on a new weekday position`() {
// Mon 7 Dec 2026, off the second-Tuesday pattern.
val offPattern = form(
LocalDateTime(2026, 12, 7, 9, 0),
LocalDateTime(2026, 12, 7, 10, 0),
"FREQ=MONTHLY;BYDAY=2TU",
)
assertThat(offPattern.withRecurrence("FREQ=MONTHLY;BYDAY=2TU;COUNT=3", berlin).start)
.isEqualTo(offPattern.start)
assertThat(offPattern.withRecurrence("FREQ=MONTHLY;BYDAY=3TU", berlin).start.dateTime)
.isEqualTo(LocalDateTime(2026, 12, 15, 9, 0))
assertThat(offPattern.withRecurrence(null, berlin).start).isEqualTo(offPattern.start)
val plain = offPattern.copy(rrule = "FREQ=MONTHLY")
assertThat(plain.withRecurrence("FREQ=MONTHLY;BYDAY=2TU", berlin).start.dateTime)
.isEqualTo(LocalDateTime(2026, 12, 8, 9, 0))
}
}
@@ -1012,5 +1012,55 @@ class EventEditViewModelTest {
assertThat(vm.state.value?.form?.color).isEqualTo(9)
job.cancel()
}
}
@Test
fun `re-confirming a weekday-position rule keeps an off-pattern start`(
@TempDir tempDir: Path,
) = runTest(dispatcher) {
// Mon 7 Dec 2026 09:00 Berlin, a series on second Tuesdays (#520).
val begin = Instant.parse("2026-12-07T08:00:00Z")
val end = Instant.parse("2026-12-07T09:00:00Z")
val fake = FakeCalendarDataSource().apply {
calendarsResult = listOf(cal(1L))
eventDetailResult = { detail(calendarId = 1L, rrule = "FREQ=MONTHLY;BYDAY=2TU", start = begin, end = end) }
}
val vm = viewModel(tempDir, fake, zones = FakeZoneSource(berlin))
val job = activate(vm)
vm.openForEdit(eventId = 1L, beginMillis = begin.toEpochMilliseconds(), endMillis = end.toEpochMilliseconds())
advanceUntilIdle()
vm.setRecurrence("FREQ=MONTHLY;BYDAY=2TU")
vm.setRecurrence("FREQ=MONTHLY;BYDAY=2TU;COUNT=5")
assertThat(vm.state.value!!.form!!.start.dateTime).isEqualTo(LocalDateTime(2026, 12, 7, 9, 0))
vm.setRecurrence("FREQ=MONTHLY;BYDAY=-1TU;COUNT=5")
assertThat(vm.state.value!!.form!!.start.dateTime).isEqualTo(LocalDateTime(2026, 12, 29, 9, 0))
job.cancel()
}
@Test
fun `a new start date under a weekday-position rule moves on to the rule's next day`(
@TempDir tempDir: Path,
) = runTest(dispatcher) {
// Tue 8 Dec 2026 09:00–10:30 Berlin, second Tuesdays (#520).
val begin = Instant.parse("2026-12-08T08:00:00Z")
val end = Instant.parse("2026-12-08T09:30:00Z")
val fake = FakeCalendarDataSource().apply {
calendarsResult = listOf(cal(1L))
eventDetailResult = { detail(calendarId = 1L, rrule = "FREQ=MONTHLY;BYDAY=2TU", start = begin, end = end) }
}
val vm = viewModel(tempDir, fake, zones = FakeZoneSource(berlin))
val job = activate(vm)
vm.openForEdit(eventId = 1L, beginMillis = begin.toEpochMilliseconds(), endMillis = end.toEpochMilliseconds())
advanceUntilIdle()
vm.setStartDate(LocalDate(2026, 12, 16))
val form = vm.state.value!!.form!!
assertThat(form.start.dateTime).isEqualTo(LocalDateTime(2027, 1, 12, 9, 0))
assertThat(form.end.dateTime).isEqualTo(LocalDateTime(2027, 1, 12, 10, 30))
vm.setStartTime(LocalTime(11, 0))
assertThat(vm.state.value!!.form!!.start.dateTime).isEqualTo(LocalDateTime(2027, 1, 12, 11, 0))
job.cancel()
}
}