Modern Apps git

← Modern-Apps.git

calendar: pressing OK in the recurrence dialog no longer deletes occurrences

Commit
ceaeacf4405f027da2323c1aeffd3517ab74f505
Author
Vayun Mathur <[email protected]>
When
2026-12-01 00:31 UTC (5 days ago)
Parents
a291bcc
RRule parse/asString round-trips MONTHLY BYDAY=2TU,4TU, but
RecurrenceParams could not carry ordinal BYDAY tokens at all,
and the dialog's confirm rebuilt the rule from the start date
only — deriving a single BYDAY=2TU. A no-op OK press rewrote
the stored RRULE and the 4th-Tuesday occurrences vanished
from the provider, permanently. The same path mangled
BYMONTHDAY=1,15 -> 1, monthly BYDAY=MO,WE -> a month-day
rule, yearly BYDAY=20MO -> BYMONTH/BYMONTHDAY, and dropped
advanced BY lists/WKST on other frequencies.

RecurrenceParams now carries byDayRaw, and confirm goes
through the pure buildRecurrenceRule: with frequency and
monthly/yearly preset unchanged it rebuilds from the initial
params untouched (all BY lists, WKST, byDayRaw) while still
applying edited interval, end condition and weekly days;
changing the preset keeps derived-from-start-date behaviour.
Seven new dialog-build tests plus a byDayRaw mapping test.
Reminders, instance delete, all-day/DST and ICS were audited
and cleared. Verified: targeted recurrence suites and lint
pass (0 errors); the full suite's one failure is a
pre-existing Holiday-route search test on files other work in
the shared tree had already modified, unrelated to this fix.

Diff

calendar/src/main/java/com/vayunmathur/calendar/domain/RecurrenceParams.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/domain/RecurrenceParams.kt b/calendar/src/main/java/com/vayunmathur/calendar/domain/RecurrenceParams.ktindex a2b3c5d8c7..487b92885f 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/domain/RecurrenceParams.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/domain/RecurrenceParams.kt@@ -27,7 +27,12 @@ data class RecurrenceParams(     val wkst: DayOfWeek? = null,     /** Plain BYDAY filter carried by a DAILY rule (e.g. weekdays only); the dialog has no      * editor for it, so it is passed through untouched on confirm instead of being dropped. */-    val byDay: List<DayOfWeek> = emptyList()+    val byDay: List<DayOfWeek> = emptyList(),+    /** Ordinal BYDAY tokens carried by a MONTHLY/YEARLY rule (e.g. ["2TU", "4TU"] or+     * ["1MO", "-1FR"]); the dialog has no editor for a multi-token list, so it is passed+     * through untouched when the user confirms without changing the monthly/yearly+     * preset instead of being rebuilt as a single start-date-derived token. */+    val byDayRaw: List<String> = emptyList() ) {     /** Conversion from a parsed recurrence rule into dialog recurrence settings. */     companion object {@@ -47,8 +52,8 @@ data class RecurrenceParams(             return when (rr) {                 is RRule.EveryXDays -> base.copy(freq = "days", interval = rr.days)                 is RRule.EveryXWeeks -> base.copy(freq = "weeks", interval = rr.weeks, daysOfWeek = rr.daysOfWeek)-                is RRule.EveryXMonths -> base.copy(freq = "months", interval = rr.months, monthlyType = rr.typeE)-                is RRule.EveryXYears -> base.copy(freq = "years", interval = rr.years)+                is RRule.EveryXMonths -> base.copy(freq = "months", interval = rr.months, monthlyType = rr.typeE, byDayRaw = rr.byDayRaw.orEmpty())+                is RRule.EveryXYears -> base.copy(freq = "years", interval = rr.years, byDayRaw = rr.byDayRaw.orEmpty())             }         }     }
calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialog.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialog.kt b/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialog.ktindex efe807a4ec..06ae9f92ef 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialog.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialog.kt@@ -122,39 +122,9 @@ fun RecurrenceDialog(                 // Clamp before buildRRuleString(): 0/negative/unparseable fall back to 1 so the                 // provider never sees INTERVAL=0 (rejected/misbehaving).                 val interval = (intervalStr.toIntOrNull() ?: 1).coerceAtLeast(1)-                val rrule: RRule = when (freq) {-                    // A parsed DAILY rule may carry a BYDAY filter (weekdays only);-                    // the dialog cannot edit it, so pass it through rather than-                    // silently expanding the series onto every day on confirm.-                    "days" -> RRule.EveryXDays(interval, endCondition,-                        byDay = initial?.byDay?.takeIf { it.isNotEmpty() })-                    "weeks" -> RRule.EveryXWeeks(-                        interval,-                        daysOfWeek.ifEmpty { listOf(startDate.dayOfWeek) },-                        endCondition-                    )-                    "months" -> when (monthlyOption) {-                        1 -> RRule.EveryXMonths(interval, 1, endCondition)-                        2 -> RRule.EveryXMonths(interval, 2, endCondition)-                        else -> RRule.EveryXMonths(interval, 0, endCondition, byMonthDay = listOf(startDate.day))-                    }-                    else -> when (yearlyOption) {-                        1 -> RRule.EveryXYears(-                            interval, endCondition,-                            byWeekNo = listOf(isoWeekNumber(startDate)),-                            byDay = listOf(startDate.dayOfWeek)-                        )-                        2 -> RRule.EveryXYears(-                            interval, endCondition,-                            byYearDay = listOf(dayOfYear(startDate))-                        )-                        else -> RRule.EveryXYears(-                            interval, endCondition,-                            byMonth = listOf(startDate.month.number),-                            byMonthDay = listOf(startDate.day)-                        )-                    }-                }+                val rrule = buildRecurrenceRule(+                    freq, interval, endCondition, daysOfWeek, monthlyOption, yearlyOption, startDate, initial,+                )                  scope.launch { registry.dispatchResult(resultKey, rrule) }                 backStack.pop()@@ -333,3 +303,4 @@ fun RecurrenceDialog(         }     ) }+
calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceRuleBuild.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceRuleBuild.kt b/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceRuleBuild.ktnew file mode 100644index 0000000000..41739eb912--- /dev/null+++ b/calendar/src/main/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceRuleBuild.kt@@ -0,0 +1,123 @@+package com.vayunmathur.calendar.ui.dialogs++import com.vayunmathur.calendar.domain.RRule+import com.vayunmathur.calendar.domain.RecurrenceParams+import kotlinx.datetime.DayOfWeek+import kotlinx.datetime.LocalDate+import kotlinx.datetime.number+/**+ * Builds the confirmed rule from the dialog's selections.+ *+ * The dialog only edits frequency, interval, end condition, weekly days, and which+ * monthly/yearly preset is chosen. Everything else a parsed rule may carry (ordinal+ * BYDAY lists such as `2TU,4TU`, multi-value BYMONTHDAY, BYMONTH/BYSETPOS/BYYEARDAY/+ * BYWEEKNO/WKST) has no editor here. When the user confirms without changing the+ * frequency or the monthly/yearly preset, those fields are therefore rebuilt from+ * [initial] untouched — deriving them from [startDate] instead would silently drop+ * occurrences (a `2TU,4TU` rule became just `2TU`; `BYMONTHDAY=1,15` became just the+ * start day). Changing the frequency or preset keeps the old derived behaviour.+ */+internal fun buildRecurrenceRule(+    freq: String,+    interval: Int,+    endCondition: RRule.EndCondition,+    daysOfWeek: List<DayOfWeek>,+    monthlyOption: Int,+    yearlyOption: Int,+    startDate: LocalDate,+    initial: RecurrenceParams?,+): RRule {+    val initialYearlyOption = initial?.let {+        when {+            it.byWeekNo.isNotEmpty() -> 1+            it.byYearDay.isNotEmpty() -> 2+            else -> 0+        }+    }+    val unchanged = initial != null && initial.freq == freq && when (freq) {+        "months" -> monthlyOption == initial.monthlyType+        "years" -> yearlyOption == initialYearlyOption+        else -> true+    }+    if (unchanged) {+        val initial = requireNotNull(initial)+        return when (freq) {+            "days" -> RRule.EveryXDays(+                interval, endCondition,+                byMonthDay = initial.byMonthDay.takeIf { it.isNotEmpty() },+                byMonth = initial.byMonth.takeIf { it.isNotEmpty() },+                bySetPos = initial.bySetPos.takeIf { it.isNotEmpty() },+                byYearDay = initial.byYearDay.takeIf { it.isNotEmpty() },+                byWeekNo = initial.byWeekNo.takeIf { it.isNotEmpty() },+                wkst = initial.wkst,+                byDay = initial.byDay.takeIf { it.isNotEmpty() },+            )+            "weeks" -> RRule.EveryXWeeks(+                interval,+                daysOfWeek.ifEmpty { listOf(startDate.dayOfWeek) },+                endCondition,+                byMonthDay = initial.byMonthDay.takeIf { it.isNotEmpty() },+                byMonth = initial.byMonth.takeIf { it.isNotEmpty() },+                bySetPos = initial.bySetPos.takeIf { it.isNotEmpty() },+                byYearDay = initial.byYearDay.takeIf { it.isNotEmpty() },+                byWeekNo = initial.byWeekNo.takeIf { it.isNotEmpty() },+                wkst = initial.wkst,+            )+            "months" -> RRule.EveryXMonths(+                interval, initial.monthlyType, endCondition,+                byMonthDay = initial.byMonthDay.takeIf { it.isNotEmpty() },+                byMonth = initial.byMonth.takeIf { it.isNotEmpty() },+                bySetPos = initial.bySetPos.takeIf { it.isNotEmpty() },+                byYearDay = initial.byYearDay.takeIf { it.isNotEmpty() },+                byWeekNo = initial.byWeekNo.takeIf { it.isNotEmpty() },+                wkst = initial.wkst,+                byDay = initial.byDay.takeIf { it.isNotEmpty() },+                byDayRaw = initial.byDayRaw.takeIf { it.isNotEmpty() },+            )+            else -> RRule.EveryXYears(+                interval, endCondition,+                byMonthDay = initial.byMonthDay.takeIf { it.isNotEmpty() },+                byMonth = initial.byMonth.takeIf { it.isNotEmpty() },+                bySetPos = initial.bySetPos.takeIf { it.isNotEmpty() },+                byYearDay = initial.byYearDay.takeIf { it.isNotEmpty() },+                byWeekNo = initial.byWeekNo.takeIf { it.isNotEmpty() },+                wkst = initial.wkst,+                byDay = initial.byDay.takeIf { it.isNotEmpty() },+                byDayRaw = initial.byDayRaw.takeIf { it.isNotEmpty() },+            )+        }+    }+    return when (freq) {+        // A parsed DAILY rule may carry a BYDAY filter (weekdays only);+        // the dialog cannot edit it, so pass it through rather than+        // silently expanding the series onto every day on confirm.+        "days" -> RRule.EveryXDays(interval, endCondition,+            byDay = initial?.byDay?.takeIf { it.isNotEmpty() })+        "weeks" -> RRule.EveryXWeeks(+            interval,+            daysOfWeek.ifEmpty { listOf(startDate.dayOfWeek) },+            endCondition+        )+        "months" -> when (monthlyOption) {+            1 -> RRule.EveryXMonths(interval, 1, endCondition)+            2 -> RRule.EveryXMonths(interval, 2, endCondition)+            else -> RRule.EveryXMonths(interval, 0, endCondition, byMonthDay = listOf(startDate.day))+        }+        else -> when (yearlyOption) {+            1 -> RRule.EveryXYears(+                interval, endCondition,+                byWeekNo = listOf(isoWeekNumber(startDate)),+                byDay = listOf(startDate.dayOfWeek)+            )+            2 -> RRule.EveryXYears(+                interval, endCondition,+                byYearDay = listOf(dayOfYear(startDate))+            )+            else -> RRule.EveryXYears(+                interval, endCondition,+                byMonth = listOf(startDate.month.number),+                byMonthDay = listOf(startDate.day)+            )+        }+    }+}
calendar/src/test/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialogBuildTest.kt
diff --git a/calendar/src/test/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialogBuildTest.kt b/calendar/src/test/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialogBuildTest.ktnew file mode 100644index 0000000000..36520a81ce--- /dev/null+++ b/calendar/src/test/java/com/vayunmathur/calendar/ui/dialogs/RecurrenceDialogBuildTest.kt@@ -0,0 +1,125 @@+package com.vayunmathur.calendar.ui.dialogs++import com.vayunmathur.calendar.domain.RecurrenceParams+import com.vayunmathur.calendar.domain.RRule+import kotlinx.datetime.LocalDate+import kotlinx.datetime.TimeZone+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/**+ * Confirming the recurrence dialog without changing the frequency or the+ * monthly/yearly preset must not rewrite a parsed rule from the start date+ * alone: fields the dialog cannot edit (ordinal BYDAY lists, multi-value+ * BYMONTHDAY, advanced BY lists) pass through untouched. Changing the preset+ * keeps the derived-from-start-date behaviour.+ */+class RecurrenceDialogBuildTest {++    private val zone = TimeZone.UTC++    private fun confirmUnchanged(input: String, start: LocalDate): String {+        val parsed = RRule.parse(input, zone)!!+        val params = RecurrenceParams.fromRRule(parsed)!!+        val yearlyOption = when {+            params.byWeekNo.isNotEmpty() -> 1+            params.byYearDay.isNotEmpty() -> 2+            else -> 0+        }+        val built = buildRecurrenceRule(+            freq = params.freq,+            interval = params.interval,+            endCondition = params.endCondition,+            daysOfWeek = params.daysOfWeek,+            monthlyOption = params.monthlyType,+            yearlyOption = yearlyOption,+            startDate = start,+            initial = params,+        )+        return built.asString(start, zone)+    }++    @Test+    fun `monthly multi ordinal BYDAY survives an unchanged confirm`() {+        for (input in listOf(+            "FREQ=MONTHLY;INTERVAL=1;BYDAY=2TU,4TU",+            "FREQ=MONTHLY;INTERVAL=1;BYDAY=1MO,-1FR",+            "FREQ=MONTHLY;INTERVAL=2;BYDAY=2TU,4TU;COUNT=10",+        )) {+            assertEquals(input, confirmUnchanged(input, LocalDate(2025, 1, 14)), input)+        }+    }++    @Test+    fun `monthly multi BYMONTHDAY survives an unchanged confirm`() {+        val input = "FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=1,15"+        assertEquals(input, confirmUnchanged(input, LocalDate(2025, 1, 1)))+    }++    @Test+    fun `monthly plain BYDAY survives an unchanged confirm`() {+        val input = "FREQ=MONTHLY;INTERVAL=1;BYDAY=MO,WE"+        assertEquals(input, confirmUnchanged(input, LocalDate(2025, 1, 6)))+    }++    @Test+    fun `yearly ordinal BYDAY survives an unchanged confirm`() {+        val result = confirmUnchanged(+            "FREQ=YEARLY;INTERVAL=1;BYDAY=20MO",+            LocalDate(2025, 1, 20),+        )+        assertTrue(result.contains("BYDAY=20MO"), result)+    }++    @Test+    fun `daily advanced BY lists survive an unchanged confirm`() {+        val result = confirmUnchanged(+            "FREQ=DAILY;INTERVAL=1;BYMONTH=6;BYSETPOS=1;WKST=SU",+            LocalDate(2025, 6, 2),+        )+        assertTrue(result.contains("BYMONTH=6"), result)+        assertTrue(result.contains("BYSETPOS=1"), result)+        assertTrue(result.contains("WKST=SU"), result)+    }++    @Test+    fun `changing the monthly preset still derives from the start date`() {+        val parsed = RRule.parse("FREQ=MONTHLY;INTERVAL=1;BYDAY=2TU,4TU", zone)!!+        val params = RecurrenceParams.fromRRule(parsed)!!+        val built = buildRecurrenceRule(+            freq = "months",+            interval = 1,+            endCondition = RRule.EndCondition.Never,+            daysOfWeek = emptyList(),+            monthlyOption = 0,+            yearlyOption = 0,+            startDate = LocalDate(2025, 1, 14),+            initial = params,+        )+        val result = built.asString(LocalDate(2025, 1, 14), zone)+        assertTrue(result.contains("BYMONTHDAY=14"), result)+        assertFalse(result.contains("4TU"), result)+    }++    @Test+    fun `an unchanged confirm still applies a new interval and end condition`() {+        val parsed = RRule.parse("FREQ=MONTHLY;INTERVAL=1;BYDAY=2TU,4TU", zone)!!+        val params = RecurrenceParams.fromRRule(parsed)!!+        val built = buildRecurrenceRule(+            freq = "months",+            interval = 3,+            endCondition = RRule.EndCondition.Count(5),+            daysOfWeek = emptyList(),+            monthlyOption = params.monthlyType,+            yearlyOption = 0,+            startDate = LocalDate(2025, 1, 14),+            initial = params,+        )+        val result = built.asString(LocalDate(2025, 1, 14), zone)+        assertTrue(result.contains("INTERVAL=3"), result)+        assertTrue(result.contains("BYDAY=2TU,4TU"), result)+        assertTrue(result.contains("COUNT=5"), result)+    }+}
calendar/src/test/java/com/vayunmathur/calendar/util/RecurrenceParamsTest.kt
diff --git a/calendar/src/test/java/com/vayunmathur/calendar/util/RecurrenceParamsTest.kt b/calendar/src/test/java/com/vayunmathur/calendar/util/RecurrenceParamsTest.ktindex 6cfdfd7987..8f784cc1d7 100644--- a/calendar/src/test/java/com/vayunmathur/calendar/util/RecurrenceParamsTest.kt+++ b/calendar/src/test/java/com/vayunmathur/calendar/util/RecurrenceParamsTest.kt@@ -70,6 +70,18 @@ class RecurrenceParamsTest {         assertEquals(days, params?.byDay)     } +    @Test+    fun `monthly and yearly keep their ordinal BYDAY tokens`() {+        val monthly = RecurrenceParams.fromRRule(+            RRule.parse("FREQ=MONTHLY;INTERVAL=1;BYDAY=2TU,4TU", kotlinx.datetime.TimeZone.UTC),+        )+        assertEquals(listOf("2TU", "4TU"), monthly?.byDayRaw)+        val yearly = RecurrenceParams.fromRRule(+            RRule.parse("FREQ=YEARLY;INTERVAL=1;BYDAY=20MO", kotlinx.datetime.TimeZone.UTC),+        )+        assertEquals(listOf("20MO"), yearly?.byDayRaw)+    }+     @Test     fun `advanced BY lists and WKST survive`() {         val rule = RRule.EveryXDays(