diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt index 8904eee9..f6aadf7e 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt @@ -33,6 +33,7 @@ import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.domain.backup.BackupFilePlan import de.jeanlucmakiola.calendula.domain.backup.BackupTarget import de.jeanlucmakiola.calendula.domain.backup.backupFingerprint +import de.jeanlucmakiola.calendula.domain.backup.backupRunLatestOnly import de.jeanlucmakiola.calendula.domain.backup.backupTargets import de.jeanlucmakiola.calendula.domain.backup.inBackupOrder import de.jeanlucmakiola.calendula.domain.backup.latestHeldElsewhere @@ -242,8 +243,7 @@ class BackupWorker( if (changeRun && !prefs.autoBackupOnChange.first()) return Result.success() val folder = prefs.autoBackupFolderUri.first() ?: return Result.failure() val outcome = try { - // Without the interval there are no dated files, so every run is a change run's. - val latestOnly = changeRun || !prefs.autoBackupScheduled.first() + val latestOnly = backupRunLatestOnly(changeRun, prefs.autoBackupScheduled.first()) runBackup(deps, prefs, folder.toUri(), latestOnly = latestOnly) } catch (e: CancellationException) { throw e diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt index ee9e40fa..e881688f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt @@ -9,6 +9,7 @@ import de.jeanlucmakiola.calendula.domain.DefaultCalendarRef import de.jeanlucmakiola.calendula.domain.EventFormField import de.jeanlucmakiola.calendula.domain.FontRole import de.jeanlucmakiola.calendula.domain.backup.BackupSelection +import de.jeanlucmakiola.calendula.domain.backup.backupIntervalOn import de.jeanlucmakiola.calendula.domain.backup.COMBINED_SERIES import de.jeanlucmakiola.calendula.domain.backup.DEFAULT_BACKUP_KEEP import de.jeanlucmakiola.calendula.domain.backup.LEGACY_BACKUP_FILE @@ -524,12 +525,8 @@ class SettingsPrefs @Inject constructor( suspend fun setAutoBackupKeep(keep: Int) = prefs.set(SettingsKeys.autoBackupKeep, keep) - /** Turning it off brings the interval back for good, not only while it stays off. */ suspend fun setAutoBackupOnChange(enabled: Boolean) { - prefs.edit { - it[SettingsKeys.autoBackupOnChange] = enabled - if (!enabled) it[SettingsKeys.autoBackupScheduledStored] = true - } + prefs.edit { applyAutoBackupOnChange(it, enabled) } } suspend fun setAutoBackupScheduled(enabled: Boolean) = prefs.set(SettingsKeys.autoBackupScheduledStored, enabled) @@ -1119,9 +1116,15 @@ val Preferences.autoBackupSelection: BackupSelection excluded = this[SettingsKeys.autoBackupExcluded], ) -/** The interval can only be off while backing up after changes is on, or nothing would ever run. */ +/** Whether the dated backups run on the interval; see [backupIntervalOn]. */ val Preferences.autoBackupScheduled: Boolean - get() = this[SettingsKeys.autoBackupScheduledStored] || !this[SettingsKeys.autoBackupOnChange] + get() = backupIntervalOn(this[SettingsKeys.autoBackupScheduledStored], this[SettingsKeys.autoBackupOnChange]) + +/** Turning it off brings the interval back for good, not only while it stays off. */ +internal fun applyAutoBackupOnChange(prefs: MutablePreferences, enabled: Boolean) { + prefs[SettingsKeys.autoBackupOnChange] = enabled + if (!enabled) prefs[SettingsKeys.autoBackupScheduledStored] = true +} /** * Unset means an install from before #411: one that already had a backup diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt b/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt index d47101e3..de39fe12 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt @@ -311,6 +311,12 @@ fun parseBackupLedger(stored: String?): BackupLedger = parts.first().takeIf { it.isNotEmpty() && names.isNotEmpty() }?.let { it to names } }.toMap() +/** The interval can only be off while backing up after changes is on (#516), or nothing would ever run. */ +fun backupIntervalOn(stored: Boolean, onChange: Boolean): Boolean = stored || !onChange + +/** Without the interval there are no dated files, so every run only refreshes the stable copies. */ +fun backupRunLatestOnly(changeRun: Boolean, intervalOn: Boolean): Boolean = changeRun || !intervalOn + /** 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) } 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 3828a0da..f07846e8 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 @@ -377,19 +377,23 @@ private fun BackupContent( stringResource(R.string.calendars_auto_backup_interval_off) }, position = Position.Middle, - trailing = { - // Off only alongside backing up after changes, or nothing would run. - Switch( - checked = autoBackup.scheduled, - onCheckedChange = viewModel::setAutoBackupScheduled, - enabled = autoBackup.onChange, - ) + // Off is only offered alongside backing up after changes, or nothing would run. + trailing = if (autoBackup.onChange) { + { Switch(checked = autoBackup.scheduled, onCheckedChange = viewModel::setAutoBackupScheduled) } + } else { + null }, onClick = if (autoBackup.scheduled) onEditInterval else ({ viewModel.setAutoBackupScheduled(true) }), ) GroupedSwitchRow( title = stringResource(R.string.calendars_auto_backup_on_change), - summary = stringResource(R.string.calendars_auto_backup_on_change_summary), + summary = stringResource( + if (autoBackup.scheduled) { + R.string.calendars_auto_backup_on_change_summary + } else { + R.string.calendars_auto_backup_on_change_only_summary + }, + ), checked = autoBackup.onChange, onCheckedChange = viewModel::setAutoBackupOnChange, position = Position.Middle, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 045d6399..13f56e85 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1037,6 +1037,7 @@ Off, only after changes Also back up after changes Updates the undated copy a few minutes after events change. Dated backups stay on the interval. + Updates the undated copy a few minutes after events change. No dated backups. Calendars %d calendar diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt index 6a35196f..fef0a603 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt @@ -932,6 +932,36 @@ class SettingsPrefsTest { assertThat(prefs.autoBackupKeep.first()).isEqualTo(10) } + @Test + fun `the backup interval is on by default and can only go off alongside after-change backups`(@TempDir tempDir: Path) = runTest { + val prefs = testSettings(tempDir) + assertThat(prefs.autoBackupScheduled.first()).isTrue() + + prefs.setAutoBackupScheduled(false) + assertThat(prefs.autoBackupScheduled.first()).isTrue() + + prefs.setAutoBackupOnChange(true) + prefs.setAutoBackupScheduled(false) + assertThat(prefs.autoBackupScheduled.first()).isFalse() + prefs.setAutoBackupScheduled(true) + assertThat(prefs.autoBackupScheduled.first()).isTrue() + } + + @Test + fun `turning after-change backups off brings the interval back for good`(@TempDir tempDir: Path) = runTest { + val prefs = testSettings(tempDir) + prefs.setAutoBackupIntervalMinutes(6 * 60L) + prefs.setAutoBackupOnChange(true) + prefs.setAutoBackupScheduled(false) + + prefs.setAutoBackupOnChange(false) + assertThat(prefs.autoBackupScheduled.first()).isTrue() + assertThat(prefs.autoBackupIntervalMinutes.first()).isEqualTo(6 * 60L) + + prefs.setAutoBackupOnChange(true) + assertThat(prefs.autoBackupScheduled.first()).isTrue() + } + @Test fun `a pre-411 install adopts its old file when the layout is pinned in place`(@TempDir tempDir: Path) = runTest { val store = testDataStore(tempDir, "test_settings") diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt index 408a69db..a39cd873 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt @@ -402,4 +402,20 @@ class BackupPlanTest { assertThat(one.inBackupOrder()).isEqualTo(other.inBackupOrder()) assertThat(one.inBackupOrder().map { it.uid }).containsExactly("a", "b").inOrder() } + + @Test + fun `the interval stays on unless after-change backups cover for it`() { + assertThat(backupIntervalOn(stored = true, onChange = false)).isTrue() + assertThat(backupIntervalOn(stored = false, onChange = false)).isTrue() + assertThat(backupIntervalOn(stored = true, onChange = true)).isTrue() + assertThat(backupIntervalOn(stored = false, onChange = true)).isFalse() + } + + @Test + fun `only a run with the interval on writes dated files`() { + assertThat(backupRunLatestOnly(changeRun = false, intervalOn = true)).isFalse() + assertThat(backupRunLatestOnly(changeRun = true, intervalOn = true)).isTrue() + assertThat(backupRunLatestOnly(changeRun = false, intervalOn = false)).isTrue() + assertThat(backupRunLatestOnly(changeRun = true, intervalOn = false)).isTrue() + } } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 9577384d..ef5c1801 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -642,6 +642,12 @@ rewritten. A copy that went missing is restored by the next run that lists the folder; one a still-loading listing doesn't show, or one edited in place outside the app, is left to the next scheduled run. +While the option is on, the interval itself can be switched off +(`auto_backup_scheduled`, read through `backupIntervalOn`, so it never counts as +off without the option): the periodic work is cancelled and every run, including +run-now, only refreshes the stable copies, so no dated files are written or +pruned. Turning the option off sets the interval back on, at the interval it had. + **A backup carries the series' deleted and moved occurrences.** `EXDATE` lives on the master row and is part of `EventExportProjection`; `IcsWriter` marks an all-day series' exclusions `VALUE=DATE`, because RFC 5545 ties `EXDATE`'s value