fix(#147, #325): review round 1 - overrides per calendar, RDATE-only series in files, chosen colours kept, copy row stays while running
This commit is contained in:
1 parent
3ed56b8596
commit
d5079f0618
9 files changed
+78
-18
No files matched your search
@@ -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)
|
||||
|
||||
+5
-1
@@ -252,7 +252,11 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
}
|
||||
|
||||
override suspend fun exportEvents(calendarIds: Set<Long>) = withContext(io) {
|
||||
flattenOverrides(dataSource.calendarRows(calendarIds, allDayReminderTimeMinutes()), ::formatExdateStamp)
|
||||
flattenOverrides(
|
||||
rows = dataSource.calendarRows(calendarIds, allDayReminderTimeMinutes()),
|
||||
exdateStamp = ::formatExdateStamp,
|
||||
keepsRecurrenceDates = false,
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun copyEvents(
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<IcsEvent> {
|
||||
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<Long, MutableList<String>>()
|
||||
val detached = mutableListOf<IcsEvent>()
|
||||
val detached = mutableListOf<Pair<Long, IcsEvent>>()
|
||||
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())
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user