fix(#516): let the backup interval be switched off while backing up after changes

This commit is contained in:
makiolaj committed 2026-11-20 22:15:57 +01:00
1 parent a24420edfb
commit b15b324965
6 files changed
+71 -24

No files matched your search

@@ -109,16 +109,20 @@ object BackupScheduler {
stopWatching()
return
}
// WorkManager's own floor is 15 min; the UI floors the user choice at 30.
val interval = prefs.autoBackupIntervalMinutes.first().coerceAtLeast(15L)
val request = PeriodicWorkRequestBuilder<BackupWorker>(interval, TimeUnit.MINUTES)
.setInitialDelay(interval, TimeUnit.MINUTES)
.build()
workManager.enqueueUniquePeriodicWork(
WORK_NAME,
ExistingPeriodicWorkPolicy.UPDATE,
request,
)
if (prefs.autoBackupScheduled.first()) {
// WorkManager's own floor is 15 min; the UI floors the user choice at 30.
val interval = prefs.autoBackupIntervalMinutes.first().coerceAtLeast(15L)
val request = PeriodicWorkRequestBuilder<BackupWorker>(interval, TimeUnit.MINUTES)
.setInitialDelay(interval, TimeUnit.MINUTES)
.build()
workManager.enqueueUniquePeriodicWork(
WORK_NAME,
ExistingPeriodicWorkPolicy.UPDATE,
request,
)
} else {
workManager.cancelUniqueWork(WORK_NAME)
}
if (!prefs.autoBackupOnChange.first()) {
stopWatching()
} else if (watchDropped || !BackupChangeJob.isPending(context)) {
@@ -238,7 +242,9 @@ class BackupWorker(
if (changeRun && !prefs.autoBackupOnChange.first()) return Result.success()
val folder = prefs.autoBackupFolderUri.first() ?: return Result.failure()
val outcome = try {
runBackup(deps, prefs, folder.toUri(), latestOnly = changeRun)
// Without the interval there are no dated files, so every run is a change run's.
val latestOnly = changeRun || !prefs.autoBackupScheduled.first()
runBackup(deps, prefs, folder.toUri(), latestOnly = latestOnly)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
@@ -64,6 +64,7 @@ internal object SettingsBackupSchema {
SettingsKeys.autoBackupCombinedStored,
SettingsKeys.autoBackupKeep,
SettingsKeys.autoBackupOnChange,
SettingsKeys.autoBackupScheduledStored,
SettingsKeys.specialDatesEnabled,
SettingsKeys.specialDatesShowYear,
) + SettingsKeys.specialDateTypeEnabled.values + SettingsKeys.specialDatesTitleTemplate.values
@@ -229,6 +229,7 @@ class SettingsPrefs @Inject constructor(
val autoBackupKeep: Flow<Int> = prefs.flow(SettingsKeys.autoBackupKeep)
val autoBackupLedger: Flow<BackupLedger> = prefs.flow(SettingsKeys.autoBackupLedger)
val autoBackupOnChange: Flow<Boolean> = prefs.flow(SettingsKeys.autoBackupOnChange)
val autoBackupScheduled: Flow<Boolean> = prefs.snapshot { it.autoBackupScheduled }
val autoBackupFingerprints: Flow<BackupFingerprints> = prefs.flow(SettingsKeys.autoBackupFingerprints)
val specialDatesEnabled: Flow<Boolean> = prefs.flow(SettingsKeys.specialDatesEnabled)
val specialDatesTypes: Flow<Set<SpecialDateType>> = prefs.snapshot { it.specialDatesTypes }
@@ -523,7 +524,15 @@ class SettingsPrefs @Inject constructor(
suspend fun setAutoBackupKeep(keep: Int) = prefs.set(SettingsKeys.autoBackupKeep, keep)
suspend fun setAutoBackupOnChange(enabled: Boolean) = prefs.set(SettingsKeys.autoBackupOnChange, enabled)
/** 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
}
}
suspend fun setAutoBackupScheduled(enabled: Boolean) = prefs.set(SettingsKeys.autoBackupScheduledStored, enabled)
/**
* Atomic, so a run's ledger writes never undo one made meanwhile (pinning
@@ -1048,6 +1057,9 @@ object SettingsKeys {
/** Whether a change to a backed-up calendar also refreshes the stable copies (#516). Opt-in. */
val autoBackupOnChange: Pref<Boolean> = booleanPref("auto_backup_on_change", false)
/** Whether the dated backups run on the interval; read as [Preferences.autoBackupScheduled]. */
val autoBackupScheduledStored: Pref<Boolean> = booleanPref("auto_backup_scheduled", true)
/** Content hash of each series' stable copy, so a change run skips untouched ones (#516). */
val autoBackupFingerprints: Pref<BackupFingerprints> =
nullableStringPref("auto_backup_fingerprints").map(::parseBackupFingerprints, ::persistBackupFingerprints)
@@ -1107,6 +1119,10 @@ 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. */
val Preferences.autoBackupScheduled: Boolean
get() = this[SettingsKeys.autoBackupScheduledStored] || !this[SettingsKeys.autoBackupOnChange]
/**
* Unset means an install from before #411: one that already had a backup
* folder keeps the single file it was writing, anything else gets per-calendar
@@ -371,9 +371,21 @@ private fun BackupContent(
)
GroupedRow(
title = stringResource(R.string.calendars_auto_backup_interval),
summary = backupIntervalLabel(autoBackup.intervalMinutes),
summary = if (autoBackup.scheduled) {
backupIntervalLabel(autoBackup.intervalMinutes)
} else {
stringResource(R.string.calendars_auto_backup_interval_off)
},
position = Position.Middle,
onClick = onEditInterval,
trailing = {
// Off only alongside backing up after changes, or nothing would run.
Switch(
checked = autoBackup.scheduled,
onCheckedChange = viewModel::setAutoBackupScheduled,
enabled = autoBackup.onChange,
)
},
onClick = if (autoBackup.scheduled) onEditInterval else ({ viewModel.setAutoBackupScheduled(true) }),
)
GroupedSwitchRow(
title = stringResource(R.string.calendars_auto_backup_on_change),
@@ -404,14 +416,17 @@ private fun BackupContent(
),
checked = autoBackup.combined,
onCheckedChange = viewModel::setAutoBackupCombined,
position = Position.Middle,
)
GroupedRow(
title = stringResource(R.string.calendars_auto_backup_keep),
summary = backupKeepLabel(autoBackup.keep),
position = Position.Bottom,
onClick = onEditKeep,
position = if (autoBackup.scheduled) Position.Middle else Position.Bottom,
)
// Retention only applies to the dated files the interval writes.
if (autoBackup.scheduled) {
GroupedRow(
title = stringResource(R.string.calendars_auto_backup_keep),
summary = backupKeepLabel(autoBackup.keep),
position = Position.Bottom,
onClick = onEditKeep,
)
}
GroupedHint(
if (selectedCount == 0) {
stringResource(R.string.calendars_auto_backup_nothing_selected)
@@ -109,9 +109,9 @@ class CalendarsViewModel @Inject constructor(
settingsPrefs.autoBackupSelection,
settingsPrefs.autoBackupCombined,
settingsPrefs.autoBackupKeep,
settingsPrefs.autoBackupOnChange,
) { base, selection, combined, keep, onChange ->
base.copy(selection = selection, combined = combined, keep = keep, onChange = onChange)
combine(settingsPrefs.autoBackupOnChange, settingsPrefs.autoBackupScheduled, ::Pair),
) { base, selection, combined, keep, (onChange, scheduled) ->
base.copy(selection = selection, combined = combined, keep = keep, onChange = onChange, scheduled = scheduled)
}
.flowOn(io)
.stateWhileSubscribed(
@@ -301,6 +301,13 @@ class CalendarsViewModel @Inject constructor(
}
}
fun setAutoBackupScheduled(enabled: Boolean) {
viewModelScope.launch {
settingsPrefs.setAutoBackupScheduled(enabled)
reschedule()
}
}
fun setAutoBackupKeep(keep: Int) {
viewModelScope.launch {
if (settingsPrefs.autoBackupKeep.first() == keep) return@launch
@@ -356,6 +363,7 @@ data class AutoBackupUiState(
val combined: Boolean = false,
val keep: Int = DEFAULT_BACKUP_KEEP,
val onChange: Boolean = false,
val scheduled: Boolean = true,
)
/** Outcome of a whole-calendar backup, surfaced once to the screen. */
+1
View File
@@ -1034,6 +1034,7 @@
<string name="calendars_auto_backup_interval">Interval</string>
<string name="calendars_auto_backup_every">Every %1$s</string>
<string name="calendars_auto_backup_interval_min">Minimum 30 minutes.</string>
<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_calendars">Calendars</string>