fix(#516): hide the interval switch without after-change backups and test the interval rules

This commit is contained in:
makiolaj committed 2026-11-25 12:03:36 +01:00
1 parent b15b324965
commit 91cb57f940
8 files changed
+83 -17

No files matched your search

@@ -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
@@ -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
@@ -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) }
@@ -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,
+1
View File
@@ -1037,6 +1037,7 @@
<string name="calendars_auto_backup_interval_off">Off, only after changes</string>
<string name="calendars_auto_backup_on_change">Also back up after changes</string>
<string name="calendars_auto_backup_on_change_summary">Updates the undated copy a few minutes after events change. Dated backups stay on the interval.</string>
<string name="calendars_auto_backup_on_change_only_summary">Updates the undated copy a few minutes after events change. No dated backups.</string>
<string name="calendars_auto_backup_calendars">Calendars</string>
<plurals name="calendars_auto_backup_calendars_count">
<item quantity="one">%d calendar</item>
@@ -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")
@@ -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()
}
}