fix(#147, #411): review round 1 - unique series names, rename-aware pruning, one folder listing, batched reminders

This commit is contained in:
makiolaj committed 2026-11-13 16:28:42 +01:00
1 parent deb2fd4482
commit 240ed5237a
14 files changed
+288 -137

No files matched your search

@@ -26,8 +26,10 @@ import de.jeanlucmakiola.calendula.R
import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository
import de.jeanlucmakiola.calendula.data.ics.IcsExporter
import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.calendula.domain.backup.BackupFilePlan
import de.jeanlucmakiola.calendula.domain.backup.backupTargets
import de.jeanlucmakiola.calendula.domain.backup.planBackupFiles
import de.jeanlucmakiola.calendula.domain.backup.updatedBackupBases
import de.jeanlucmakiola.calendula.domain.ics.IcsWriter
import de.jeanlucmakiola.floret.android.hasPermission
import kotlinx.coroutines.flow.first
@@ -120,55 +122,75 @@ class BackupWorker(
val folder = prefs.autoBackupFolderUri.first()
?: return Result.failure() // nothing to write to — leave scheduling to settings
val now = System.currentTimeMillis()
return try {
val outcome = try {
runBackup(deps, prefs, folder.toUri())
prefs.recordAutoBackupRun(success = true, atMillis = now)
Result.success()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
prefs.recordAutoBackupRun(success = false, atMillis = now)
if (prefs.autoBackupStatus.first().consecutiveFailures >= FAILURE_NOTIFY_THRESHOLD) {
notifyFailure(applicationContext)
}
Log.w(TAG, "Automatic backup failed", e)
Result.retry()
RunOutcome.Failed
}
if (outcome == RunOutcome.NothingSelected) return Result.success()
val success = outcome == RunOutcome.Written
prefs.recordAutoBackupRun(success = success, atMillis = now)
if (!success && prefs.autoBackupStatus.first().consecutiveFailures >= FAILURE_NOTIFY_THRESHOLD) {
notifyFailure(applicationContext)
}
// A retry rewrites every series, so only a run that wrote nothing is
// retried; a partial one waits for the next interval rather than
// rotating the healthy calendars' history out with near-identical copies.
return if (outcome == RunOutcome.Failed) Result.retry() else Result.success()
}
private enum class RunOutcome { Written, PartlyFailed, Failed, NothingSelected }
/**
* Writes every file first and prunes afterwards, and only a series whose
* new file landed, so a failed run never costs an older backup. Throws
* after the rest are done if any series failed.
* Writes each series and prunes it only once its new file has landed, so
* a failed write never costs an older backup.
*/
private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri) {
private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri): RunOutcome {
val repository = deps.repository()
val exporter = deps.exporter()
val all = repository.calendars().first()
// Without the calendar permission the list comes back empty: a failure,
// not an empty selection.
if (all.isEmpty()) return RunOutcome.Failed
val selection = prefs.autoBackupSelection.first()
val calendars = repository.calendars().first().filter(selection::selects)
val targets = backupTargets(calendars, combined = prefs.autoBackupCombined.first())
val previousBases = prefs.autoBackupSeriesNames.first()
val targets = backupTargets(
calendars = all.filter(selection::selects),
combined = prefs.autoBackupCombined.first(),
previousBases = previousBases,
)
if (targets.isEmpty()) return RunOutcome.NothingSelected
val listing = exporter.listFolder(folder)
val plans = planBackupFiles(
targets = targets,
existing = exporter.fileNames(folder),
existing = listing.keys,
at = LocalDateTime.now(),
keep = prefs.autoBackupKeep.first(),
)
val stamp = Clock.System.now()
var failure: Exception? = null
val written = mutableListOf<BackupFilePlan>()
for (plan in plans) {
try {
val events = repository.exportEvents(plan.target.calendarIds)
val content = IcsWriter().writeCalendar(events, stamp, plan.target.calendarName)
exporter.writeToFolder(folder, plan.fileName, content)
exporter.deleteFromFolder(folder, plan.prune)
exporter.writeToFolder(folder, listing, plan.fileName, content)
written += plan
exporter.deleteDocuments(plan.prune.mapNotNull(listing::get))
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.w(TAG, "Backup of one calendar failed", e)
failure = failure ?: e
Log.w(TAG, "Backup of one series failed", e)
}
}
failure?.let { throw it }
prefs.setAutoBackupSeriesNames(updatedBackupBases(previousBases, written))
return when (written.size) {
plans.size -> RunOutcome.Written
0 -> RunOutcome.Failed
else -> RunOutcome.PartlyFailed
}
}
private fun notifyFailure(context: Context) {
@@ -789,14 +789,23 @@ class AndroidCalendarDataSource @Inject constructor(
if (names.isEmpty()) return emptyList()
val idList = names.keys.joinToString(",")
// Skip soft-deleted rows and exception rows (modified occurrences /
// cancellations) — v1 exports masters + one-offs only.
val selection = "${CalendarContract.Events.CALENDAR_ID} IN ($idList) AND " +
"${CalendarContract.Events.DELETED} = 0 AND " +
"${CalendarContract.Events.ORIGINAL_ID} IS NULL"
val eventIds = resolver.query(
CalendarContract.Events.CONTENT_URI,
arrayOf(CalendarContract.Events._ID),
selection,
null,
null,
)?.use { c -> c.mapRows { getLong(0) } }.orEmpty()
val reminders = reminderMinutesByEvent(eventIds)
return resolver.query(
CalendarContract.Events.CONTENT_URI,
EventExportProjection.COLUMNS,
// Skip soft-deleted rows and exception rows (modified occurrences /
// cancellations) — v1 exports masters + one-offs only.
"${CalendarContract.Events.CALENDAR_ID} IN ($idList) AND " +
"${CalendarContract.Events.DELETED} = 0 AND " +
"${CalendarContract.Events.ORIGINAL_ID} IS NULL",
selection,
null,
CalendarContract.Events.DTSTART + " ASC",
)?.use { c ->
@@ -804,7 +813,7 @@ class AndroidCalendarDataSource @Inject constructor(
val eventId = getLong(EventExportProjection.IDX_ID)
val calendarId = getLong(EventExportProjection.IDX_CALENDAR_ID)
toIcsEvent(
reminderMinutes = queryReminders(eventId).map { it.minutes },
reminderMinutes = reminders[eventId].orEmpty(),
calendarName = names[calendarId],
allDayReminderTimeMinutes = allDayReminderTimeMinutes,
)
@@ -812,6 +821,26 @@ class AndroidCalendarDataSource @Inject constructor(
} ?: emptyList()
}
/**
* Reminder minutes of [eventIds], a chunked query at a time rather than one
* per event — a synced calendar can hold thousands (#147).
*/
private fun reminderMinutesByEvent(eventIds: List<Long>): Map<Long, List<Int>> {
val byEvent = mutableMapOf<Long, MutableList<Int>>()
eventIds.chunked(REMINDER_QUERY_CHUNK).forEach { chunk ->
resolver.query(
CalendarContract.Reminders.CONTENT_URI,
arrayOf(CalendarContract.Reminders.EVENT_ID, CalendarContract.Reminders.MINUTES),
"${CalendarContract.Reminders.EVENT_ID} IN (${chunk.joinToString(",")})",
null,
null,
)?.use { c ->
while (c.moveToNext()) byEvent.getOrPut(c.getLong(0)) { mutableListOf() } += c.getInt(1)
}
}
return byEvent
}
override fun existingUids(calendarId: Long): Set<String> = resolver.query(
CalendarContract.Events.CONTENT_URI,
arrayOf(CalendarContract.Events.UID_2445),
@@ -1746,6 +1775,9 @@ class AndroidCalendarDataSource @Inject constructor(
private companion object {
const val TAG = "CalendarDataSource"
/** Ids per `IN (…)` list, well under SQLite's bound-variable and statement limits. */
const val REMINDER_QUERY_CHUNK = 500
/** Normalised key an attendee is matched/deduped on (case-insensitive email). */
fun emailKey(email: String): String = email.trim().lowercase()
@@ -63,11 +63,9 @@ interface CalendarRepository {
/**
* Every event of [calendarIds], ready to serialise into an `.ics` backup
* (see [CalendarDataSource.calendarEvents]). Any calendar can be read,
* synced and read-only ones included (#147); `null` means the calendars a
* backup covers by default
* ([de.jeanlucmakiola.calendula.domain.backup.isBackedUpByDefault]).
* synced and read-only ones included (#147).
*/
suspend fun exportEvents(calendarIds: Set<Long>? = null): List<IcsEvent>
suspend fun exportEvents(calendarIds: Set<Long>): List<IcsEvent>
/**
* Bulk-import parsed `.ics` [events] into [targetCalendarId]. Events whose
@@ -12,7 +12,6 @@ import de.jeanlucmakiola.calendula.domain.EventDetail
import de.jeanlucmakiola.calendula.domain.EventForm
import de.jeanlucmakiola.calendula.domain.EventInstance
import de.jeanlucmakiola.calendula.domain.SearchCandidate
import de.jeanlucmakiola.calendula.domain.backup.isBackedUpByDefault
import de.jeanlucmakiola.calendula.domain.ics.IcsImportSummary
import de.jeanlucmakiola.calendula.domain.ics.ParsedIcsEvent
import kotlinx.coroutines.CoroutineDispatcher
@@ -248,12 +247,8 @@ class CalendarRepositoryImpl @Inject constructor(
}
}
override suspend fun exportEvents(calendarIds: Set<Long>?) =
withContext(io) {
val ids = calendarIds
?: dataSource.calendars().filter { it.isBackedUpByDefault }.map { it.id }.toSet()
dataSource.calendarEvents(ids, allDayReminderTimeMinutes())
}
override suspend fun exportEvents(calendarIds: Set<Long>) =
withContext(io) { dataSource.calendarEvents(calendarIds, allDayReminderTimeMinutes()) }
override suspend fun importEvents(
targetCalendarId: Long,
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.calendula.data.ics
import android.content.Context
import android.net.Uri
import android.provider.DocumentsContract
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -30,41 +31,50 @@ class IcsExporter @Inject constructor(
}
/**
* Write [content] to [fileName] inside the persisted SAF tree [folder],
* overwriting it if it already exists (the automatic-backup destination).
* Requires a persisted write grant on [folder]. Throws on failure.
* The files directly inside the persisted SAF tree [folder] (the
* automatic-backup destination), by display name — one provider query, so
* a run over many calendars doesn't pay a lookup per file. Throws when the
* folder is gone or not writable.
*/
fun writeToFolder(folder: Uri, fileName: String, content: String) {
val dir = writableFolder(folder)
// Reuse the file if present and clean up any "name (1).ics" duplicates
// SAF may have created if two runs ever raced, so a name converges on a
// single overwritten file.
val base = fileName.substringBeforeLast('.')
val ext = fileName.substringAfterLast('.', "")
var target: DocumentFile? = null
for (child in dir.listFiles()) {
val name = child.name ?: continue
when {
name == fileName -> target = child
name.startsWith("$base (") && name.endsWith(".$ext") -> child.delete()
fun listFolder(folder: Uri): Map<String, Uri> {
writableFolder(folder)
val treeId = DocumentsContract.getTreeDocumentId(folder)
val children = DocumentsContract.buildChildDocumentsUriUsingTree(folder, treeId)
val columns = arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_MIME_TYPE,
)
val cursor = context.contentResolver.query(children, columns, null, null, null)
?: throw IOException("Backup folder can't be listed")
return cursor.use { c ->
buildMap {
while (c.moveToNext()) {
if (c.getString(2) == DocumentsContract.Document.MIME_TYPE_DIR) continue
val name = c.getString(1) ?: continue
put(name, DocumentsContract.buildDocumentUriUsingTree(folder, c.getString(0)))
}
}
}
val file = target
?: dir.createFile(MIME_CALENDAR, fileName)
?: throw IOException("Could not create backup file in the chosen folder")
writeDocument(file.uri, content)
}
/** Names of the files directly inside [folder]. Throws when it can't be read. */
fun fileNames(folder: Uri): List<String> = writableFolder(folder).listFiles().mapNotNull { it.name }
/**
* Write [content] to [fileName] in [folder], overwriting the file of that
* name if [listing] (from [listFolder]) has one. Throws on failure.
*/
fun writeToFolder(folder: Uri, listing: Map<String, Uri>, fileName: String, content: String) {
val uri = listing[fileName] ?: DocumentsContract.createDocument(
context.contentResolver,
DocumentsContract.buildDocumentUriUsingTree(folder, DocumentsContract.getTreeDocumentId(folder)),
MIME_CALENDAR,
fileName,
) ?: throw IOException("Could not create backup file in the chosen folder")
writeDocument(uri, content)
}
/** Delete the files called [names] from [folder]; one that won't go is left behind. */
fun deleteFromFolder(folder: Uri, names: Collection<String>) {
if (names.isEmpty()) return
val wanted = names.toSet()
writableFolder(folder).listFiles()
.filter { it.isFile && it.name in wanted }
.forEach { runCatching { it.delete() } }
/** Delete [documents]; one that won't go is left behind. */
fun deleteDocuments(documents: Collection<Uri>) {
documents.forEach { runCatching { DocumentsContract.deleteDocument(context.contentResolver, it) } }
}
private fun writableFolder(folder: Uri): DocumentFile {
@@ -81,7 +81,7 @@ class CalendarPrefs @Inject constructor(
}
/** A comma-joined id set; non-numeric tokens are dropped, and an empty set removes the key. */
private fun idSetPref(name: String): Pref<Set<Long>> = nullableStringPref(name).map(
internal fun idSetPref(name: String): Pref<Set<Long>> = nullableStringPref(name).map(
{ stored -> stored.orEmpty().split(',').mapNotNull { it.trim().toLongOrNull() }.toSet() },
{ ids -> if (ids.isEmpty()) null else ids.sorted().joinToString(",") },
)
@@ -8,6 +8,8 @@ import de.jeanlucmakiola.calendula.domain.FontRole
import de.jeanlucmakiola.calendula.domain.backup.BackupSelection
import de.jeanlucmakiola.calendula.domain.backup.DEFAULT_BACKUP_KEEP
import de.jeanlucmakiola.calendula.domain.backup.normalizeBackupKeep
import de.jeanlucmakiola.calendula.domain.backup.parseBackupBases
import de.jeanlucmakiola.calendula.domain.backup.persistBackupBases
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange
import de.jeanlucmakiola.calendula.ui.agenda.parseAgendaRange
@@ -188,6 +190,7 @@ class SettingsPrefs @Inject constructor(
val autoBackupSelection: Flow<BackupSelection> = prefs.snapshot { it.autoBackupSelection }
val autoBackupCombined: Flow<Boolean> = prefs.snapshot { it.autoBackupCombined }
val autoBackupKeep: Flow<Int> = prefs.flow(SettingsKeys.autoBackupKeep)
val autoBackupSeriesNames: Flow<Map<Long, Set<String>>> = prefs.flow(SettingsKeys.autoBackupSeriesNames)
val specialDatesEnabled: Flow<Boolean> = prefs.flow(SettingsKeys.specialDatesEnabled)
val specialDatesTypes: Flow<Set<SpecialDateType>> = prefs.snapshot { it.specialDatesTypes }
val specialDatesCalendars: Flow<Map<SpecialDateType, Long>> = prefs.snapshot { it.specialDatesCalendars }
@@ -356,6 +359,9 @@ class SettingsPrefs @Inject constructor(
suspend fun setAutoBackupKeep(keep: Int) = prefs.set(SettingsKeys.autoBackupKeep, keep)
suspend fun setAutoBackupSeriesNames(names: Map<Long, Set<String>>) =
prefs.set(SettingsKeys.autoBackupSeriesNames, names)
/** Switch [calendar] in or out of the automatic backup (#147). */
suspend fun setAutoBackupCalendar(calendar: CalendarSource, included: Boolean) {
prefs.edit {
@@ -751,6 +757,10 @@ object SettingsKeys {
val autoBackupKeep: Pref<Int> =
intPref("auto_backup_keep", DEFAULT_BACKUP_KEEP).coerced(::normalizeBackupKeep)
/** Each calendar's backup series name from its last run, so a rename still prunes the old files. */
val autoBackupSeriesNames: Pref<Map<Long, Set<String>>> =
nullableStringPref("auto_backup_series").map(::parseBackupBases, ::persistBackupBases)
/** Master switch for the contact special-dates mirror (issue #15). Opt-in. */
val specialDatesEnabled: Pref<Boolean> = booleanPref("special_dates_enabled", false)
@@ -850,12 +860,6 @@ private fun <T> Pref<T>.coerced(clamp: (T) -> T): Pref<T> = map(clamp, clamp)
private fun agendaRangePref(name: String): Pref<AgendaRange> =
nullableStringPref(name).map({ parseAgendaRange(it, AgendaRange.Month) }, { it.storageValue() })
private fun idSetPref(name: String): Pref<Set<Long>> =
nullableStringPref(name).map(
{ stored -> stored?.split(LIST_SEP).orEmpty().mapNotNull { it.trim().toLongOrNull() }.toSet() },
{ ids -> ids.takeIf { it.isNotEmpty() }?.sorted()?.joinToString(LIST_SEP) },
)
private fun reminderListPref(name: String): Pref<List<Int>> =
nullableStringPref(name).map({ it.toReminderList() }, { it.toStoredReminders() })
@@ -47,10 +47,6 @@ data class BackupSelection(
excluded = if (!on && default) excluded + id else excluded - id,
)
}
/** Ids of the selected calendars among [calendars]. */
fun selectedIds(calendars: List<CalendarSource>): Set<Long> =
calendars.filter(::selects).map { it.id }.toSet()
}
/** One backup file series: the calendars it holds and the name its files start with. */
@@ -59,6 +55,8 @@ data class BackupTarget(
val baseName: String,
/** The calendar name written into the file, or null for a combined file. */
val calendarName: String?,
/** Names this calendar's series went by before a rename; their files are pruned with it. */
val formerBaseNames: Set<String> = emptySet(),
)
/** What one run writes and which earlier files it removes afterwards. */
@@ -67,6 +65,8 @@ data class BackupFilePlan(
val fileName: String,
/** Earlier files of this series beyond the retention count, oldest first. */
val prune: List<String>,
/** Former names of the series that still have files after [prune], so they stay tracked. */
val liveFormerBaseNames: Set<String> = emptySet(),
)
/** The combined file's series name; also the fixed name used before #411. */
@@ -100,31 +100,51 @@ fun sanitizeBackupName(name: String, fallback: String = "calendar"): String {
.replace(WHITESPACE, " ")
.trim()
.trimStart('.')
.take(MAX_BASE_LENGTH)
.trim()
return cleaned.ifEmpty { fallback }
// Cut by code point, so an emoji at the limit isn't split into a lone surrogate.
val end = if (cleaned.codePointCount(0, cleaned.length) > MAX_BASE_LENGTH) {
cleaned.offsetByCodePoints(0, MAX_BASE_LENGTH)
} else {
cleaned.length
}
return cleaned.substring(0, end).trim().ifEmpty { fallback }
}
/**
* The file series the automatic backup writes: one combined file, or one per
* calendar named after it. Two calendars that sanitise to the same name (a local
* and a synced "Personal") keep apart by their id, so neither prunes the other's
* calendar named after it. Names are compared case-insensitively, as most
* storage is; calendars whose names clash (a local and a synced "Personal")
* take their id as a suffix until every series is distinct, so none prunes
* another's files. [previousBases] — the series names each calendar's files
* still carry from earlier runs — let a renamed calendar keep pruning its old
* files.
*/
fun backupTargets(calendars: List<CalendarSource>, combined: Boolean): List<BackupTarget> {
fun backupTargets(
calendars: List<CalendarSource>,
combined: Boolean,
previousBases: Map<Long, Set<String>> = emptyMap(),
): List<BackupTarget> {
if (calendars.isEmpty()) return emptyList()
if (combined) {
return listOf(BackupTarget(calendars.map { it.id }.toSet(), COMBINED_BACKUP_BASE, calendarName = null))
}
val bases = calendars.associate { it.id to sanitizeBackupName(it.displayName) }
val taken = bases.values.groupingBy { it.lowercase() }.eachCount()
val bases = calendars.associateTo(LinkedHashMap()) { it.id to sanitizeBackupName(it.displayName) }
while (true) {
val counts = bases.values.groupingBy { it.lowercase() }.eachCount()
val clashing = bases.filterValues {
counts.getValue(it.lowercase()) > 1 || it.equals(COMBINED_BACKUP_BASE, ignoreCase = true)
}
if (clashing.isEmpty()) break
clashing.forEach { (id, base) -> bases[id] = "$base-$id" }
}
val current = bases.values.map { it.lowercase() }.toSet()
return calendars.map { calendar ->
val base = bases.getValue(calendar.id)
val clash = taken.getValue(base.lowercase()) > 1 || base.equals(COMBINED_BACKUP_BASE, ignoreCase = true)
BackupTarget(
calendarIds = setOf(calendar.id),
baseName = if (clash) "$base-${calendar.id}" else base,
baseName = base,
calendarName = calendar.displayName,
formerBaseNames = previousBases[calendar.id].orEmpty()
.filterTo(mutableSetOf()) { it.lowercase() !in current },
)
}
}
@@ -149,24 +169,62 @@ fun planBackupFiles(
val retain = keep.coerceAtLeast(1)
return targets.map { target ->
val fileName = backupFileName(target.baseName, at)
val series = Regex("^${Regex.escape(target.baseName)}_($STAMP_REGEX)${Regex.escape(ICS_EXTENSION)}$")
val series = (target.formerBaseNames + target.baseName).map { base ->
base to Regex("^${Regex.escape(base)}_($STAMP_REGEX)${Regex.escape(ICS_EXTENSION)}$")
}
val earlier = existing
.filter { it != fileName }
.mapNotNull { name ->
val stamp = series.find(name)?.groupValues?.get(1)
?: if (target.baseName == COMBINED_BACKUP_BASE && name == LEGACY_BACKUP_FILE) "" else null
stamp?.let { name to it }
series.firstNotNullOfOrNull { (base, regex) ->
regex.find(name)?.let { SeriesFile(name, base, it.groupValues[1]) }
} ?: if (target.baseName == COMBINED_BACKUP_BASE && name == LEGACY_BACKUP_FILE) {
SeriesFile(name, COMBINED_BACKUP_BASE, stamp = "")
} else {
null
}
}
.sortedByDescending { it.second }
.map { it.first }
.sortedByDescending { it.stamp }
val kept = earlier.take(retain - 1)
BackupFilePlan(
target = target,
fileName = fileName,
prune = earlier.drop(retain - 1).reversed(),
prune = earlier.drop(retain - 1).reversed().map { it.name },
liveFormerBaseNames = kept.map { it.base }.filterTo(mutableSetOf()) { it in target.formerBaseNames },
)
}
}
private data class SeriesFile(val name: String, val base: String, val stamp: String)
/**
* The series names each calendar's files carry after a run that wrote
* [written]: its current name plus former ones that still have files. Calendars
* not written this run keep what [previous] had.
*/
fun updatedBackupBases(previous: Map<Long, Set<String>>, written: List<BackupFilePlan>): Map<Long, Set<String>> =
previous + written.filter { it.target.calendarName != null }.associate {
it.target.calendarIds.single() to (it.liveFormerBaseNames + it.target.baseName)
}
private const val BASES_ENTRY_SEP = "|"
private const val BASES_KEY_SEP = "/"
/** Stores the id → series names map; both separators are characters a sanitised name can't hold. */
fun persistBackupBases(bases: Map<Long, Set<String>>): String? =
bases.filterValues { it.isNotEmpty() }.takeIf { it.isNotEmpty() }
?.entries?.sortedBy { it.key }
?.joinToString(BASES_ENTRY_SEP) { (id, names) ->
(listOf(id.toString()) + names.sorted()).joinToString(BASES_KEY_SEP)
}
fun parseBackupBases(stored: String?): Map<Long, Set<String>> =
stored?.split(BASES_ENTRY_SEP).orEmpty().mapNotNull { entry ->
val parts = entry.split(BASES_KEY_SEP)
val id = parts.first().toLongOrNull() ?: return@mapNotNull null
val names = parts.drop(1).filter { it.isNotEmpty() }.toSet()
names.takeIf { it.isNotEmpty() }?.let { id to it }
}.toMap()
/** Closest offered retention count to [stored], so a stray value still shows a row. */
fun normalizeBackupKeep(stored: Int): Int =
BACKUP_KEEP_OPTIONS.minBy { kotlin.math.abs(it - stored) }
@@ -98,11 +98,15 @@ fun BackupScreen(
val snackbarHostState = remember { SnackbarHostState() }
// The one-calendar shortcut; the per-calendar selector owns its own launcher.
// The id is taken at the tap, so a result delivered into a recreated screen
// still knows what to export.
var shortcutExportId by rememberSaveable { mutableStateOf<Long?>(null) }
val createBackup = rememberLauncherForActivityResult(
contract = ActivityResultContracts.CreateDocument("text/calendar"),
) { uri ->
val ids = (state as? BackupUiState.Ready)?.exportable?.map { it.id }?.toSet()
if (uri != null && ids != null) viewModel.exportBackup(uri, ids)
val id = shortcutExportId
if (uri != null && id != null) viewModel.exportBackup(uri, setOf(id))
shortcutExportId = null
}
var showExportPicker by rememberSaveable { mutableStateOf(false) }
@@ -159,7 +163,8 @@ fun BackupScreen(
viewModel = viewModel,
onRestore = { runCatching { openBackup.launch(RESTORE_MIME_TYPES) } },
onPickFolder = { runCatching { pickFolder.launch(null) } },
onExportAll = {
onExportOnly = {
shortcutExportId = s.exportable.singleOrNull()?.id
runCatching { createBackup.launch(defaultBackupName()) }
},
onExportPick = { showExportPicker = true },
@@ -243,7 +248,7 @@ private fun BackupContent(
viewModel: CalendarsViewModel,
onRestore: () -> Unit,
onPickFolder: () -> Unit,
onExportAll: () -> Unit,
onExportOnly: () -> Unit,
onExportPick: () -> Unit,
onEditInterval: () -> Unit,
onEditCalendars: () -> Unit,
@@ -277,7 +282,7 @@ private fun BackupContent(
position = Position.Top,
leading = { LeadingAvatar(Icons.Default.FileDownload) },
// A single exportable calendar skips the selector.
onClick = if (exportable.size == 1) onExportAll else onExportPick,
onClick = if (exportable.size == 1) onExportOnly else onExportPick,
)
GroupedRow(
title = stringResource(R.string.calendars_restore_action),
@@ -352,7 +357,7 @@ private fun BackupContent(
@Composable
private fun ExportCalendarPicker(
calendars: List<CalendarSource>,
onExport: (Uri, Set<Long>?) -> Unit,
onExport: (Uri, Set<Long>) -> Unit,
onDismiss: () -> Unit,
) {
// Deliberately not keyed on [calendars]: that list is observer-driven, so
@@ -124,11 +124,11 @@ class CalendarsViewModel @Inject constructor(
fun consumeBackupResult() { _backupResult.value = null }
/**
* Serialise every event of [calendarIds] (null: the local calendars a backup
* covers by default) into the chosen SAF document [uri] as one `VCALENDAR`. Result (event count, or failure) lands
* Serialise every event of [calendarIds] into the chosen SAF document [uri]
* as one `VCALENDAR`. Result (event count, or failure) lands
* in [backupResult] for a one-shot message.
*/
fun exportBackup(uri: Uri, calendarIds: Set<Long>? = null) {
fun exportBackup(uri: Uri, calendarIds: Set<Long>) {
viewModelScope.launch {
_backupResult.value = try {
val count = withContext(io) {
+1 -1
View File
@@ -911,7 +911,7 @@
<item quantity="one">Only the latest</item>
<item quantity="other">The latest %d</item>
</plurals>
<string name="calendars_auto_backup_keep_hint">Counted per file: with one file per calendar, each calendar keeps this many. Older backups Calendula wrote are deleted; other files in the folder are left alone.</string>
<string name="calendars_auto_backup_keep_hint">Counted per file: with one file per calendar, each calendar keeps this many. Older backups Calendula wrote are deleted after each run; a calendar you stop backing up keeps its last files, and nothing else in the folder is touched.</string>
<string name="calendars_auto_backup_status_never">No automatic backup yet</string>
<string name="calendars_auto_backup_status_ok">Last backup: %1$s</string>
<string name="calendars_auto_backup_status_failed">Last backup failed: %1$s</string>
@@ -949,37 +949,6 @@ class CalendarRepositoryImplTest {
assertThat(fake.lastCalendarEventsIds).containsExactly(1L, 4L)
}
@Test
fun `exportEvents without ids reads the writable local calendars only`(
@TempDir tempDir: Path,
) = runTest {
fun cal(id: Long, local: Boolean, writable: Boolean = true, managed: Boolean = false) =
de.jeanlucmakiola.calendula.domain.CalendarSource(
id = id,
displayName = "C$id",
accountName = "a",
accountType = if (local) "LOCAL" else "com.google",
color = 0,
isVisibleInSystem = true,
canModifyContents = writable,
isLocal = local,
isManaged = managed,
)
val fake = FakeCalendarDataSource().apply {
calendarsResult = listOf(
cal(1L, local = true),
cal(2L, local = false),
cal(3L, local = true, managed = true),
cal(4L, local = true, writable = false),
)
}
val repo = testRepository(fake, tempDir, Dispatchers.Unconfined)
repo.exportEvents()
assertThat(fake.lastCalendarEventsIds).containsExactly(1L)
}
@Test
fun `exportEvents reads synced calendars when asked for them`(
@TempDir tempDir: Path,
@@ -145,4 +145,60 @@ class BackupPlanTest {
assertThat(normalizeBackupKeep(3)).isEqualTo(3)
assertThat(normalizeBackupKeep(50)).isEqualTo(10)
}
@Test
fun `a suffixed name that meets a real one is told apart again`() {
val targets = backupTargets(
listOf(cal(5L, "Work"), cal(6L, "Work"), cal(9L, "Work-5")),
combined = false,
)
assertThat(targets.map { it.baseName }.toSet()).hasSize(3)
assertThat(targets.map { it.baseName }).containsExactly("Work-5-5", "Work-6", "Work-5-9").inOrder()
}
@Test
fun `an emoji at the length limit is not split`() {
val name = "a".repeat(59) + "\uD83D\uDCC5" + "tail"
val cleaned = sanitizeBackupName(name)
assertThat(cleaned).isEqualTo("a".repeat(59) + "\uD83D\uDCC5")
assertThat(Character.isHighSurrogate(cleaned.last())).isFalse()
}
@Test
fun `a renamed calendar keeps pruning the files of its old name`() {
val target = backupTargets(
listOf(cal(1L, "Job")),
combined = false,
previousBases = mapOf(1L to setOf("Work")),
).single()
assertThat(target.formerBaseNames).containsExactly("Work")
val existing = listOf("Work_2026-11-10_09-00-00.ics", "Work_2026-11-12_09-00-00.ics")
val keepTwo = planBackupFiles(listOf(target), existing, at, keep = 2).single()
assertThat(keepTwo.prune).containsExactly("Work_2026-11-10_09-00-00.ics")
assertThat(keepTwo.liveFormerBaseNames).containsExactly("Work")
assertThat(updatedBackupBases(emptyMap(), listOf(keepTwo))).containsExactly(1L, setOf("Work", "Job"))
val keepOne = planBackupFiles(listOf(target), existing, at, keep = 1).single()
assertThat(keepOne.prune).hasSize(2)
assertThat(updatedBackupBases(mapOf(1L to setOf("Work"), 2L to setOf("X")), listOf(keepOne)))
.containsExactly(1L, setOf("Job"), 2L, setOf("X"))
}
@Test
fun `a former name now used by another calendar is not claimed`() {
val targets = backupTargets(
listOf(cal(1L, "Job"), cal(2L, "Work")),
combined = false,
previousBases = mapOf(1L to setOf("Work")),
)
assertThat(targets.first().formerBaseNames).isEmpty()
}
@Test
fun `series names round-trip through storage`() {
val bases = mapOf(3L to setOf("Work", "Job"), 1L to setOf("Home"))
assertThat(parseBackupBases(persistBackupBases(bases))).isEqualTo(bases)
assertThat(persistBackupBases(emptyMap())).isNull()
assertThat(parseBackupBases("garbage|x/y|4/")).isEmpty()
}
}
+3 -1
View File
@@ -471,7 +471,9 @@ restore runs through the normal import, whose targets stay `isEventTarget`. The
automatic backup writes one `<calendar name>_<date>_<time>.ics` series per
calendar, or one `calendula-backup_…` series in combined mode (#411), and prunes
each series to the retention count after its new file has landed. Pruning only
matches names of that series, so nothing else in the folder is touched; the
matches names of that series, so nothing else in the folder is touched (a
renamed calendar's old names are remembered in `auto_backup_series` until their
files are gone); the
pre-#411 fixed `calendula-backup.ics` counts as the combined series' oldest file,
and an install that already had a folder stays on the combined layout.