From d5079f0618df903fc97b625bb1935b34bbc294cd Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sat, 14 Nov 2026 01:23:45 +0100 Subject: [PATCH] fix(#147, #325): review round 1 - overrides per calendar, RDATE-only series in files, chosen colours kept, copy row stays while running --- .../data/calendar/CalendarDataSource.kt | 7 ++-- .../data/calendar/CalendarRepositoryImpl.kt | 6 +++- .../calendula/data/calendar/Projections.kt | 2 ++ .../calendula/domain/ics/CalendarRows.kt | 28 +++++++++------ .../calendula/ui/calendars/BackupScreen.kt | 3 +- .../calendula/ui/calendars/CopyEventsSheet.kt | 4 ++- .../ui/calendars/CopyEventsViewModel.kt | 5 ++- .../calendula/domain/ics/CalendarRowsTest.kt | 35 +++++++++++++++++++ docs/ARCHITECTURE.md | 6 ++-- 9 files changed, 78 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index 5de0f22f..5ee27e55 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -794,8 +794,11 @@ class AndroidCalendarDataSource @Inject constructor( val calendarId = getLong(EventExportProjection.IDX_CALENDAR_ID) val calendar = calendars[calendarId] // Some adapters stamp the calendar's colour onto every row; that is no - // colour of the event's own and would pin it in a copy. - val color = getIntOrNull(EventExportProjection.IDX_EVENT_COLOR)?.takeIf { it != calendar?.color } + // colour of the event's own and would pin it in a copy. A palette key + // means it was chosen. + val color = getIntOrNull(EventExportProjection.IDX_EVENT_COLOR)?.takeIf { + it != calendar?.color || !getString(EventExportProjection.IDX_EVENT_COLOR_KEY).isNullOrEmpty() + } val mapped = event(calendar?.displayName).copy(color = color) val originalId = getLongOrNull(EventExportProjection.IDX_ORIGINAL_ID) val originalSyncId = getString(EventExportProjection.IDX_ORIGINAL_SYNC_ID) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt index f2f7190c..3945c5f6 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt @@ -252,7 +252,11 @@ class CalendarRepositoryImpl @Inject constructor( } override suspend fun exportEvents(calendarIds: Set) = withContext(io) { - flattenOverrides(dataSource.calendarRows(calendarIds, allDayReminderTimeMinutes()), ::formatExdateStamp) + flattenOverrides( + rows = dataSource.calendarRows(calendarIds, allDayReminderTimeMinutes()), + exdateStamp = ::formatExdateStamp, + keepsRecurrenceDates = false, + ) } override suspend fun copyEvents( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/Projections.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/Projections.kt index 0d092e04..6931d31d 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/Projections.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/Projections.kt @@ -184,6 +184,7 @@ internal object EventExportProjection { CalendarContract.Events.ORIGINAL_INSTANCE_TIME, CalendarContract.Events.RDATE, CalendarContract.Events.ACCESS_LEVEL, + CalendarContract.Events.EVENT_COLOR_KEY, ) const val IDX_ID = 0 @@ -208,6 +209,7 @@ internal object EventExportProjection { const val IDX_ORIGINAL_INSTANCE_TIME = 19 const val IDX_RDATE = 20 const val IDX_ACCESS_LEVEL = 21 + const val IDX_EVENT_COLOR_KEY = 22 } /** diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRows.kt b/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRows.kt index 85963527..91ec0035 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRows.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRows.kt @@ -52,27 +52,33 @@ data class CalendarRows( * occurrence missing its series, never a series missing an occurrence. * * [exdateStamp] formats an occurrence as an `EXDATE` entry in the provider's - * shape, the same one Calendula's own exclusions use. + * shape, the same one Calendula's own exclusions use. [keepsRecurrenceDates] is + * false where the output can't carry `RDATE` (an `.ics` file): a series that + * repeats by listed dates alone then reads as the one-off it will be written as, + * and its overrides are left alone rather than excluded from nothing. A + * cancelled series' modified occurrences aren't brought back to life either. */ fun flattenOverrides( rows: CalendarRows, exdateStamp: (occurrenceMillis: Long, isAllDay: Boolean) -> String, + keepsRecurrenceDates: Boolean = true, ): List { val byId = rows.series.associateBy { it.eventId } // Sync ids are only unique within an account, so the link is per calendar. val bySyncId = rows.series.filter { it.syncId != null }.associateBy { it.calendarId to it.syncId } val excluded = mutableMapOf>() - val detached = mutableListOf() + val detached = mutableListOf>() for (override in rows.overrides) { val parent = override.originalId?.let(byId::get)?.takeIf { it.calendarId == override.calendarId } ?: override.originalSyncId?.let { bySyncId[override.calendarId to it] } ?: continue // An exception to a one-off overrides nothing a plain event could carry. - if (!parent.event.recurs) continue + val recurs = if (keepsRecurrenceDates) parent.event.recurs else !parent.event.recurrenceRule.isNullOrBlank() + if (!recurs) continue val occurrence = override.originalInstance.toEpochMilliseconds() excluded.getOrPut(parent.eventId) { mutableListOf() } += exdateStamp(occurrence, parent.event.isAllDay) - if (override.isCancelled) continue - detached += override.event.copy( + if (override.isCancelled || parent.event.status == EventStatus.Cancelled) continue + detached += override.calendarId to override.event.copy( uid = "$occurrence-${parent.event.uid}", recurrenceRule = null, recurrenceDates = null, @@ -80,9 +86,11 @@ fun flattenOverrides( overrideOf = parent.event.uid, ) } - // Two exception rows for one occurrence would derive the same UID. - return detached.distinctBy { it.uid } + rows.series.map { s -> - val extra = excluded[s.eventId].orEmpty() - if (extra.isEmpty()) s.event else s.event.copy(exDates = (s.event.exDates + extra).distinct()) - } + // Two exception rows for one occurrence would derive the same UID; the same + // series in two calendars legitimately does. + return detached.distinctBy { (calendarId, event) -> calendarId to event.uid }.map { it.second } + + rows.series.map { s -> + val extra = excluded[s.eventId].orEmpty() + if (extra.isEmpty()) s.event else s.event.copy(exDates = (s.event.exDates + extra).distinct()) + } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt index 03f4c77a..4e68c341 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt @@ -323,7 +323,8 @@ private fun BackupContent( leading = { LeadingAvatar(Icons.Default.FileUpload) }, onClick = onRestoreClick, ) - if (canCopy) { + // A running or finished copy keeps its row, the way back to its sheet. + if (canCopy || copyRun != CopyRunState.Idle) { GroupedRow( title = stringResource(R.string.calendars_copy_action), summary = when (copyRun) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt index f99c419d..c17629d6 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt @@ -213,7 +213,9 @@ private fun CopySummary(run: CopyRunState.Done) { ) Row(horizontalArrangement = Arrangement.spacedBy(24.dp)) { CopyCount(stringResource(R.string.import_done_added_label), summary.imported) - CopyCount(stringResource(R.string.import_done_skipped_label), summary.skippedDuplicate) + if (summary.skippedDuplicate > 0) { + CopyCount(stringResource(R.string.import_done_skipped_label), summary.skippedDuplicate) + } if (summary.failed > 0) CopyCount(stringResource(R.string.import_done_failed_label), summary.failed) } if (summary.skippedDuplicate > 0) CopyNote(stringResource(R.string.import_done_dedup_note)) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt index 32bcbd34..602df3b8 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt @@ -80,7 +80,10 @@ class CopyEventsViewModel @Inject constructor( job = viewModelScope.launch { try { val summary = repository.copyEvents(source, target) { done, total -> - _run.value = CopyRunState.Running(done, total) + // About every half percent: thousands of inserts needn't mean thousands of frames. + if (done == 0 || done == total || done % (total / 200).coerceAtLeast(1) == 0) { + _run.value = CopyRunState.Running(done, total) + } } _run.value = CopyRunState.Done(summary) } catch (e: CancellationException) { diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRowsTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRowsTest.kt index 172baedd..1c0fe9f7 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRowsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/domain/ics/CalendarRowsTest.kt @@ -185,4 +185,39 @@ class CalendarRowsTest { assertThat(IcsParser().parse(text).events.single().overrideOf).isEqualTo("s;1") } + + @Test + fun `a file can't carry RDATE, so an RDATE-only series keeps its overrides out of it`() { + val rows = CalendarRows( + listOf(MasterRow(1, 1, null, event("s").copy(recurrenceDates = "20260112T090000Z"))), + listOf(OverrideRow(1, 1, null, occurrence(1), event("x"))), + ) + + val flat = flattenOverrides(rows, ::formatExdateStamp, keepsRecurrenceDates = false) + assertThat(flat.map { it.uid }).containsExactly("s") + assertThat(flat.single().exDates).isEmpty() + } + + @Test + fun `a cancelled series' moved occurrence isn't brought back`() { + val flat = flatten( + listOf(series("s", status = EventStatus.Cancelled)), + listOf(OverrideRow(1, 1, null, occurrence(1), event("x"))), + ) + + assertThat(flat.map { it.uid }).containsExactly("s") + } + + @Test + fun `the same series in two calendars keeps each calendar's moved occurrence`() { + val flat = flatten( + listOf(series("s", id = 1, calendar = 1), series("s", id = 2, calendar = 2)), + listOf( + OverrideRow(1, 1, null, occurrence(1), event("x")), + OverrideRow(2, 2, null, occurrence(1), event("x")), + ), + ) + + assertThat(flat.count { it.overrideOf == "s" }).isEqualTo(2) + } } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index b13f037b..9a5972a9 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -508,8 +508,10 @@ same in every backup, and it carries the series UID as `X-CALENDULA-OVERRIDE-OF` `importEvents` skips such an occurrence wherever its series already is — that series there has the override itself — so restoring into the calendar a backup came from adds nothing twice, while a restore into an empty one gets both. -Exceptions whose series isn't among the rows are left out, and sync ids link -only within their own calendar. Backups made before this carry no exceptions +Exceptions whose series isn't among the rows are left out, a cancelled +series' moved occurrences stay cancelled, and sync ids link only within their +own calendar. A file can't carry `RDATE`, so a backup treats a series that +repeats by listed dates alone as the one-off it writes it as. Backups made before this carry no exceptions and restore exactly as before. **Copying a calendar.** Backup & restore → Copy events (#325) runs the source's