From 041f8ec2f79d17a34d17b729b8f14e5616824f30 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sat, 14 Nov 2026 01:50:22 +0100 Subject: [PATCH] fix(#147, #325): review round 4 - standalone occurrences imported first in any file order, copy sheet independent of the list state --- .../data/calendar/CalendarRepositoryImpl.kt | 6 ++- .../calendula/ui/calendars/BackupScreen.kt | 39 ++++++++++--------- .../ui/calendars/CalendarsViewModel.kt | 2 +- .../calendar/CalendarRepositoryImplTest.kt | 12 ++++++ docs/ARCHITECTURE.md | 4 +- 5 files changed, 40 insertions(+), 23 deletions(-) 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 d2aac8a9..18fc7098 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 @@ -298,7 +298,9 @@ class CalendarRepositoryImpl @Inject constructor( // Occurrences whose standalone copy didn't land, by series UID: the series // keeps them instead of excluding them for good. val unexcluded = mutableMapOf>() - for ((index, event) in events.withIndex()) { + // Standalone occurrences ahead of the series they were cut from, however the file is ordered. + val ordered = events.sortedBy { it.overrideOf == null } + for ((index, event) in ordered.withIndex()) { if (index > 0) onProgress(index) // A known UID means the event is already in this calendar — skip, // keeping a restore idempotent (no overwrite this pass). @@ -317,7 +319,7 @@ class CalendarRepositoryImpl @Inject constructor( Log.w(TAG, "Import stopped: the calendar is no longer writable", e) // A known UID would have been skipped whatever happened, so it is // no part of what a retry still has to import. - val remaining = events.drop(index) + val remaining = ordered.drop(index) val duplicates = remaining.count { it.isAlreadyIn(existing) } skipped += duplicates notAttempted = remaining.size - duplicates 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 3aad783d..adf6b33e 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 @@ -184,6 +184,24 @@ fun BackupScreen( } } + // Not gated on Ready: a running copy keeps its progress and Cancel whatever the list does. + if (showCopy) { + val calendars by copyViewModel.calendars.collectAsStateWithLifecycle() + val selection by copyViewModel.selection.collectAsStateWithLifecycle() + CopyEventsSheet( + calendars = calendars, + selection = selection, + run = copyRun, + onSelectSource = copyViewModel::setSource, + onSelectTarget = copyViewModel::setTarget, + onStart = copyViewModel::start, + onCancel = copyViewModel::cancel, + onDismiss = { + showCopy = false + copyViewModel.clearFinished() + }, + ) + } // Gated on Ready rather than resetting the flag when the state leaves it: // flipping it here would be a write during composition. (state as? BackupUiState.Ready)?.let { ready -> @@ -194,23 +212,6 @@ fun BackupScreen( onDismiss = { showExportPicker = false }, ) } - if (showCopy) { - val calendars by copyViewModel.calendars.collectAsStateWithLifecycle() - val selection by copyViewModel.selection.collectAsStateWithLifecycle() - CopyEventsSheet( - calendars = calendars, - selection = selection, - run = copyRun, - onSelectSource = copyViewModel::setSource, - onSelectTarget = copyViewModel::setTarget, - onStart = copyViewModel::start, - onCancel = copyViewModel::cancel, - onDismiss = { - showCopy = false - copyViewModel.clearFinished() - }, - ) - } if (showBackupCalendars) { AutoBackupCalendarPicker( calendars = ready.exportable, @@ -301,11 +302,11 @@ private fun BackupContent( GroupedHint(restoreSummary, style = MaterialTheme.typography.bodySmall) GroupedRow( title = stringResource(R.string.calendars_restore_action), - position = Position.Alone, + position = if (copyRun != CopyRunState.Idle) Position.Top else Position.Alone, leading = { LeadingAvatar(Icons.Default.FileUpload) }, onClick = onRestoreClick, ) - if (copyRun != CopyRunState.Idle) CopyEventsRow(copyRun, Position.Alone, onCopy) + if (copyRun != CopyRunState.Idle) CopyEventsRow(copyRun, Position.Bottom, onCopy) return } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt index 172269ed..5ebad9b6 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt @@ -143,7 +143,7 @@ class CalendarsViewModel @Inject constructor( uri = uri, content = IcsWriter().writeCalendar(events, Clock.System.now(), name), ) - events.count { it.overrideOf == null } + events.size } BackupResult.Success(count) } catch (e: CancellationException) { diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImplTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImplTest.kt index 0dd24eb1..12e25bc2 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImplTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImplTest.kt @@ -1066,6 +1066,18 @@ class CalendarRepositoryImplTest { assertThat(fake.importedEvents.single().first.exDates).containsExactly("20260119T090000Z") } + @Test + fun `a standalone occurrence listed after its series still goes first`( + @TempDir tempDir: Path, + ) = runTest { + val fake = FakeCalendarDataSource() + val repo = testRepository(fake, tempDir, Dispatchers.Unconfined) + + repo.importEvents(3L, listOf(parsedEvent("s"), parsedEvent("1-s").copy(overrideOf = "s"))) + + assertThat(fake.importedEvents.map { it.first.uid }).containsExactly("1-s", "s").inOrder() + } + @Test fun `a backup writes an RDATE-only series as the one-off a file can hold`( @TempDir tempDir: Path, diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index da369688..0fe0e456 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -510,7 +510,9 @@ 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. The standalone event also names its occurrence (`X-CALENDULA-OVERRIDE-AT`); if it fails to insert, the series is inserted without that exclusion, so the -occurrence stays at its original time rather than vanishing for good. +occurrence stays at its original time rather than vanishing for good. The +import inserts every such event ahead of the series, whatever order the file +lists them in. 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