fix(#481): carry the per-calendar settings from #147, #171, #270, #272, #459 and classify the new keys

This commit is contained in:
makiolaj committed 2026-11-18 17:43:28 +01:00
1 parent 43e5e67752
commit 71c3a73abf
8 files changed
+169 -16

No files matched your search

@@ -4,9 +4,14 @@ import androidx.datastore.preferences.core.Preferences
import de.jeanlucmakiola.calendula.data.appname.LauncherName
import de.jeanlucmakiola.calendula.data.calendar.ManagedCalendarRow
import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefKeys
import de.jeanlucmakiola.calendula.data.prefs.FixedTimeOverride
import de.jeanlucmakiola.calendula.data.prefs.SettingsKeys
import de.jeanlucmakiola.calendula.data.prefs.fixedTimeOverrideFor
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
import de.jeanlucmakiola.calendula.domain.reminders.FixedTimeReminder
import de.jeanlucmakiola.calendula.domain.reminders.ReminderAlertStyle
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.floret.backup.BackupDocument
import de.jeanlucmakiola.floret.backup.BackupResult
import de.jeanlucmakiola.floret.backup.PrefsSection
@@ -17,6 +22,7 @@ import de.jeanlucmakiola.floret.backup.optObject
import de.jeanlucmakiola.floret.backup.optObjectList
import de.jeanlucmakiola.floret.backup.optInt
import de.jeanlucmakiola.floret.backup.optString
import de.jeanlucmakiola.floret.backup.optStringList
import de.jeanlucmakiola.floret.backup.readBackup
import de.jeanlucmakiola.floret.backup.requireString
import de.jeanlucmakiola.floret.prefs.Pref
@@ -48,6 +54,18 @@ data class CalendarSettings(
/** Timed reminder override; an empty list is "no reminder". */
val reminders: List<Int>? = null,
val allDayReminders: List<Int>? = null,
/** The views it is hidden in only (#270). */
val hiddenInViews: Set<CalendarView> = emptySet(),
/** False when Calendula posts none of its reminders (#272). */
val notifications: Boolean = true,
/** Its alert style (#171); null is a plain notification. */
val alertStyle: ReminderAlertStyle? = null,
/** Its set-time reminder override (#459). */
val fixedTimeReminder: FixedTimeOverride = FixedTimeOverride.Inherit,
/** False when the daily summary leaves it out (#459). */
val inDailyDigest: Boolean = true,
/** The automatic backup's choice for it (#147); null follows the default for its kind. */
val autoBackup: Boolean? = null,
)
/** The app language a file carries; [tag] null follows the system. */
@@ -80,7 +98,15 @@ fun buildSettingsBackup(
val durations = snapshot[SettingsKeys.perCalendarEventDuration]
val reminders = snapshot[SettingsKeys.perCalendarReminderOverride]
val allDay = snapshot[SettingsKeys.perCalendarAllDayReminderOverride]
val ids = hidden + listOfNotNull(lastUsed) + durations.keys + reminders.keys + allDay.keys
val hiddenViews = snapshot[SettingsKeys.perCalendarHiddenViews]
val muted = snapshot[SettingsKeys.mutedReminderCalendarIds]
val alertStyles = snapshot[SettingsKeys.reminderAlertStyles]
val fixedTime = snapshot[SettingsKeys.perCalendarFixedTimeReminderOverride]
val notInDigest = snapshot[SettingsKeys.digestExcludedCalendarIds]
val backupIncluded = snapshot[SettingsKeys.autoBackupIncluded]
val backupExcluded = snapshot[SettingsKeys.autoBackupExcluded]
val ids = hidden + listOfNotNull(lastUsed) + durations.keys + reminders.keys + allDay.keys +
hiddenViews.keys + muted + alertStyles.keys + fixedTime.keys + notInDigest + backupIncluded + backupExcluded
val byId = calendars.associateBy { it.id }
val managedType = managed.associate { it.id to it.type }
val entries = ids.sorted().mapNotNull { id ->
@@ -97,6 +123,16 @@ fun buildSettingsBackup(
eventDurationMinutes = durations[id],
reminders = reminders[id],
allDayReminders = allDay[id],
hiddenInViews = hiddenViews[id].orEmpty(),
notifications = id !in muted,
alertStyle = alertStyles[id]?.takeUnless { it == ReminderAlertStyle.Notification },
fixedTimeReminder = fixedTime.fixedTimeOverrideFor(id),
inDailyDigest = id !in notInDigest,
autoBackup = when (id) {
in backupIncluded -> true
in backupExcluded -> false
else -> null
},
)
}
return SettingsBackup(
@@ -157,6 +193,18 @@ private fun CalendarSettings.toJson(): JsonObject = buildJsonObject {
eventDurationMinutes?.let { put(KEY_EVENT_DURATION, JsonPrimitive(it)) }
reminders?.let { put(KEY_REMINDERS, JsonArray(it.map(::JsonPrimitive))) }
allDayReminders?.let { put(KEY_ALL_DAY_REMINDERS, JsonArray(it.map(::JsonPrimitive))) }
if (hiddenInViews.isNotEmpty()) {
put(KEY_HIDDEN_IN_VIEWS, JsonArray(hiddenInViews.sortedBy { it.ordinal }.map { JsonPrimitive(it.name) }))
}
if (!notifications) put(KEY_NOTIFICATIONS, JsonPrimitive(false))
alertStyle?.let { put(KEY_ALERT_STYLE, JsonPrimitive(it.storageValue)) }
when (val fixed = fixedTimeReminder) {
FixedTimeOverride.Inherit -> Unit
FixedTimeOverride.Off -> put(KEY_FIXED_TIME_REMINDER, JsonPrimitive(FIXED_TIME_OFF))
is FixedTimeOverride.At -> put(KEY_FIXED_TIME_REMINDER, JsonPrimitive(fixed.reminder.toToken()))
}
if (!inDailyDigest) put(KEY_IN_DAILY_DIGEST, JsonPrimitive(false))
autoBackup?.let { put(KEY_AUTO_BACKUP, JsonPrimitive(it)) }
}
/**
@@ -176,6 +224,18 @@ private fun JsonObject.toCalendarSettings(): CalendarSettings {
eventDurationMinutes = optInt(KEY_EVENT_DURATION),
reminders = optIntList(KEY_REMINDERS)?.cleanReminders(),
allDayReminders = optIntList(KEY_ALL_DAY_REMINDERS)?.cleanReminders(),
hiddenInViews = optStringList(KEY_HIDDEN_IN_VIEWS).orEmpty()
.mapNotNullTo(mutableSetOf()) { name -> CalendarView.entries.firstOrNull { it.name == name } },
notifications = optBoolean(KEY_NOTIFICATIONS) ?: true,
alertStyle = optString(KEY_ALERT_STYLE)?.let(ReminderAlertStyle::fromStorage)
?.takeUnless { it == ReminderAlertStyle.Notification },
fixedTimeReminder = when (val token = optString(KEY_FIXED_TIME_REMINDER)) {
null -> FixedTimeOverride.Inherit
FIXED_TIME_OFF -> FixedTimeOverride.Off
else -> FixedTimeReminder.parse(token)?.let(FixedTimeOverride::At) ?: FixedTimeOverride.Inherit
},
inDailyDigest = optBoolean(KEY_IN_DAILY_DIGEST) ?: true,
autoBackup = optBoolean(KEY_AUTO_BACKUP),
)
}
@@ -196,3 +256,10 @@ private const val KEY_LAST_USED = "last_used"
private const val KEY_EVENT_DURATION = "event_duration_minutes"
private const val KEY_REMINDERS = "reminders"
private const val KEY_ALL_DAY_REMINDERS = "all_day_reminders"
private const val KEY_HIDDEN_IN_VIEWS = "hidden_in_views"
private const val KEY_NOTIFICATIONS = "notifications"
private const val KEY_ALERT_STYLE = "alert_style"
private const val KEY_FIXED_TIME_REMINDER = "fixed_time_reminder"
private const val FIXED_TIME_OFF = "off"
private const val KEY_IN_DAILY_DIGEST = "in_daily_digest"
private const val KEY_AUTO_BACKUP = "auto_backup"
@@ -128,14 +128,7 @@ class SettingsBackupEngine @Inject constructor(
}
}
step("reminders") { reminderScanner.scan() }
step("auto backup") {
BackupScheduler.apply(
context = context,
enabled = after[SettingsKeys.autoBackupEnabled],
intervalMinutes = after[SettingsKeys.autoBackupIntervalMinutes],
hasFolder = after[SettingsKeys.autoBackupFolderUri] != null,
)
}
step("auto backup") { BackupScheduler.reconcile(context, settingsPrefs) }
plan.launcherName?.let { name ->
step("launcher name") { if (launcherNames.current() != name) launcherNames.set(name) }
}
@@ -46,6 +46,8 @@ internal object SettingsBackupSchema {
SettingsKeys.drawerViewOrder,
SettingsKeys.defaultFormFields,
SettingsKeys.autofocusEventTitle,
// Names its calendar by account and display name, so it finds it again on another device.
SettingsKeys.defaultNewEventCalendar,
SettingsKeys.defaultEventDurationMinutes,
SettingsKeys.remindersEnabled,
SettingsKeys.allowColorOnUnsupportedCalendars,
@@ -53,8 +55,15 @@ internal object SettingsBackupSchema {
SettingsKeys.defaultAllDayReminderMinutes,
SettingsKeys.allDayReminderTimeMinutes,
SettingsKeys.snoozeMinutes,
SettingsKeys.defaultFixedTimeReminder,
SettingsKeys.dailyDigestEnabled,
SettingsKeys.dailyDigestTimeMinutes,
SettingsKeys.dailyDigestDay,
SettingsKeys.autoBackupEnabled,
SettingsKeys.autoBackupIntervalMinutes,
SettingsKeys.autoBackupCombinedStored,
SettingsKeys.autoBackupKeep,
SettingsKeys.autoBackupOnChange,
SettingsKeys.specialDatesEnabled,
SettingsKeys.specialDatesShowYear,
) + SettingsKeys.specialDateTypeEnabled.values + SettingsKeys.specialDatesTitleTemplate.values
@@ -69,6 +78,13 @@ internal object SettingsBackupSchema {
SettingsKeys.perCalendarEventDuration,
SettingsKeys.perCalendarReminderOverride,
SettingsKeys.perCalendarAllDayReminderOverride,
SettingsKeys.perCalendarHiddenViews,
SettingsKeys.mutedReminderCalendarIds,
SettingsKeys.reminderAlertStyles,
SettingsKeys.perCalendarFixedTimeReminderOverride,
SettingsKeys.digestExcludedCalendarIds,
SettingsKeys.autoBackupIncluded,
SettingsKeys.autoBackupExcluded,
)
/** Never exported: this install's own state, bookkeeping, or meaningless on another device. */
@@ -82,6 +98,13 @@ internal object SettingsBackupSchema {
SettingsKeys.widgetSize,
// Usage history, not a setting.
SettingsKeys.recentTimeZones,
SettingsKeys.recentFixedTimeReminders,
// A dismissed hint of this install.
SettingsKeys.notificationCategoryHintDismissed,
// This device's notification channels.
SettingsKeys.reminderChannelVersions,
// Sync-request grants of this device's accounts.
CalendarPrefKeys.syncAccessDismissed,
// First-run wizard progress of this install.
SettingsKeys.onboardingWizardArmed,
CalendarPrefKeys.visibilityNoticePending,
@@ -94,6 +117,11 @@ internal object SettingsBackupSchema {
SettingsKeys.specialDatesLastRun,
SettingsKeys.specialDatesStalled,
SettingsKeys.specialDatesLastForegroundSync,
SettingsKeys.dailyDigestArmedAt,
ReminderStateKeys.lastDigestDay,
// What the automatic backup wrote to this install's folder.
SettingsKeys.autoBackupLedger,
SettingsKeys.autoBackupFingerprints,
// Switch-offs still owed to the provider on this device.
CalendarPrefKeys.pendingDisabledCalendarIds,
// The reminder delivery watermark.
@@ -5,6 +5,7 @@ import androidx.datastore.preferences.core.Preferences
import de.jeanlucmakiola.calendula.data.appname.LauncherName
import de.jeanlucmakiola.calendula.data.calendar.ManagedCalendarRow
import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefKeys
import de.jeanlucmakiola.calendula.data.prefs.FixedTimeOverride
import de.jeanlucmakiola.calendula.data.prefs.SettingsKeys
import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.calendula.domain.CalendarSource
@@ -189,6 +190,25 @@ fun MutablePreferences.addCalendarSettings(id: Long, entry: CalendarSettings) {
this[SettingsKeys.perCalendarAllDayReminderOverride] =
this[SettingsKeys.perCalendarAllDayReminderOverride] + (id to it)
}
if (entry.hiddenInViews.isNotEmpty()) {
this[SettingsKeys.perCalendarHiddenViews] = this[SettingsKeys.perCalendarHiddenViews] + (id to entry.hiddenInViews)
}
if (!entry.notifications) this[SettingsKeys.mutedReminderCalendarIds] = this[SettingsKeys.mutedReminderCalendarIds] + id
entry.alertStyle?.let { this[SettingsKeys.reminderAlertStyles] = this[SettingsKeys.reminderAlertStyles] + (id to it) }
val fixed = when (val override = entry.fixedTimeReminder) {
FixedTimeOverride.Inherit -> null
FixedTimeOverride.Off -> id to null
is FixedTimeOverride.At -> id to override.reminder
}
fixed?.let {
this[SettingsKeys.perCalendarFixedTimeReminderOverride] = this[SettingsKeys.perCalendarFixedTimeReminderOverride] + it
}
if (!entry.inDailyDigest) this[SettingsKeys.digestExcludedCalendarIds] = this[SettingsKeys.digestExcludedCalendarIds] + id
when (entry.autoBackup) {
true -> this[SettingsKeys.autoBackupIncluded] = this[SettingsKeys.autoBackupIncluded] + id
false -> this[SettingsKeys.autoBackupExcluded] = this[SettingsKeys.autoBackupExcluded] + id
null -> Unit
}
}
fun CalendarIdentity.skipped(ambiguous: Boolean = false): SkippedSetting =
@@ -80,8 +80,6 @@ private val RESTORE_MIME_TYPES = arrayOf(
/**
* Backup & restore (#69): `.ics` export and restore, with
* optional automatic backup. Shares [CalendarsViewModel] with the manager.
* Backup & restore (#69): `.ics` export and restore for local calendars, with
* optional automatic backup, and the app's settings as JSON (#481). Shares
* [CalendarsViewModel] with the manager; the settings block works whatever
* state the calendar list is in.
@@ -95,6 +93,7 @@ fun BackupScreen(
onImport: (Uri) -> Unit,
visible: Boolean = true,
viewModel: CalendarsViewModel = hiltViewModel(),
copyViewModel: CopyEventsViewModel = hiltViewModel(),
settingsBackup: SettingsBackupViewModel = hiltViewModel(),
) {
val state by viewModel.backupState.collectAsStateWithLifecycle()
@@ -6,10 +6,14 @@ import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.calendula.data.appname.LauncherName
import de.jeanlucmakiola.calendula.data.calendar.ManagedCalendarRow
import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefKeys
import de.jeanlucmakiola.calendula.data.prefs.FixedTimeOverride
import de.jeanlucmakiola.calendula.data.prefs.SettingsKeys
import de.jeanlucmakiola.calendula.data.prefs.fixedTimeOverrideFor
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.FontRole
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
import de.jeanlucmakiola.calendula.domain.reminders.FixedTimeReminder
import de.jeanlucmakiola.calendula.domain.reminders.ReminderAlertStyle
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.theme.FONT_CUSTOM_TOKEN
import de.jeanlucmakiola.floret.backup.BackupProblem
@@ -155,15 +159,44 @@ class SettingsBackupTest {
source[SettingsKeys.perCalendarEventDuration] = mapOf(1L to 30)
source[SettingsKeys.perCalendarReminderOverride] = mapOf(1L to listOf(5))
source[SettingsKeys.perCalendarAllDayReminderOverride] = mapOf(1L to listOf(0))
source[SettingsKeys.perCalendarHiddenViews] = mapOf(1L to setOf(CalendarView.Month, CalendarView.Week))
source[SettingsKeys.mutedReminderCalendarIds] = setOf(1L)
source[SettingsKeys.reminderAlertStyles] = mapOf(1L to ReminderAlertStyle.FullScreenRing)
source[SettingsKeys.perCalendarFixedTimeReminderOverride] = mapOf(1L to FixedTimeReminder(2, 1_200))
source[SettingsKeys.digestExcludedCalendarIds] = setOf(1L)
source[SettingsKeys.autoBackupIncluded] = setOf(1L)
source[SettingsKeys.autoBackupExcluded] = setOf(1L)
val target = mutablePreferencesOf()
import(roundTrip(source), target, environment(calendars = listOf(calendar(9, "Work")), managed = emptyList()))
// One assertion per key in the schema: a new per-calendar key fails here until it is carried.
SettingsBackupSchema.perCalendar.forEach { pref ->
// Both auto-backup keys are set at the source so each is checked; one calendar sits in only one.
SettingsBackupSchema.perCalendar.filter { it != SettingsKeys.autoBackupExcluded }.forEach { pref ->
assertThat(target.asMap().keys.map { it.name }).contains(pref.name)
}
assertThat(target[SettingsKeys.perCalendarAllDayReminderOverride]).containsExactly(9L, listOf(0))
assertThat(target[SettingsKeys.perCalendarHiddenViews]).containsExactly(9L, setOf(CalendarView.Month, CalendarView.Week))
assertThat(target[SettingsKeys.mutedReminderCalendarIds]).containsExactly(9L)
assertThat(target[SettingsKeys.reminderAlertStyles]).containsExactly(9L, ReminderAlertStyle.FullScreenRing)
assertThat(target[SettingsKeys.perCalendarFixedTimeReminderOverride]).containsExactly(9L, FixedTimeReminder(2, 1_200))
assertThat(target[SettingsKeys.digestExcludedCalendarIds]).containsExactly(9L)
assertThat(target[SettingsKeys.autoBackupIncluded]).containsExactly(9L)
}
@Test
fun `an off set-time override and an excluded backup calendar survive the round trip`() {
val source = mutablePreferencesOf()
source[SettingsKeys.perCalendarFixedTimeReminderOverride] = mapOf(1L to null)
source[SettingsKeys.autoBackupExcluded] = setOf(1L)
val target = mutablePreferencesOf()
import(roundTrip(source), target, environment(calendars = listOf(calendar(9, "Work")), managed = emptyList()))
assertThat(target[SettingsKeys.perCalendarFixedTimeReminderOverride].fixedTimeOverrideFor(9L))
.isEqualTo(FixedTimeOverride.Off)
assertThat(target[SettingsKeys.autoBackupExcluded]).containsExactly(9L)
assertThat(target[SettingsKeys.autoBackupIncluded]).isEmpty()
}
@Test
+16 -3
View File
@@ -57,8 +57,9 @@ The settings stored under their own on-disk key names, in their stored form,
e.g. `"theme_mode": "DARK"`, `"multi_day_count": 4`,
`"default_reminder_minutes": "10,60"`. Only keys that have been set are
written. `SettingsBackupSchema.plain` lists them: appearance, fonts,
views, the agenda screen, the event form, reminders, automatic backup
(switch and interval) and contact special dates (switch, types, title
views, the agenda screen, the event form and its default calendar, reminders
and the daily summary, automatic backup (switch, interval, layout, retention,
backup on change) and contact special dates (switch, types, title
templates, year).
Three are taken only where this device can honour them, otherwise the current
@@ -86,6 +87,12 @@ identity instead:
| `event_duration_minutes` | Int? | Per-calendar default length, clamped to 1–1440 like the setting. |
| `reminders` | Int[]? | Timed reminder override; `[]` is "no reminder". |
| `all_day_reminders` | Int[]? | All-day reminder override. |
| `hidden_in_views` | String[]? | Views it is hidden in only (`Month`, `Week`, …); unknown names are dropped. |
| `notifications` | Boolean? | `false`: Calendula posts none of its reminders. |
| `alert_style` | String? | `full_screen` or `full_screen_ring`; absent or unknown is a plain notification. |
| `fixed_time_reminder` | String? | Set-time reminder override: `off`, or `days@minuteOfDay` (e.g. `1@1200`). Absent or unreadable inherits the default. |
| `in_daily_digest` | Boolean? | `false`: left out of the daily summary. |
| `auto_backup` | Boolean? | The automatic backup's choice for it; absent follows the default for its kind. |
On import an entry matches the one calendar here with the same account name,
account type and display name. No match, or more than one, skips it and the
@@ -118,7 +125,13 @@ bookkeeping, or meaningless on another device.
- Font stamps (`*_font_stamp`): cache keys for the local font file.
- `agenda_widget_range`, `widget_size`: legacy defaults for unconfigured
widgets. Widget configuration is not part of the backup at all.
- `recent_time_zones`: usage history.
- `recent_time_zones`, `recent_fixed_time_reminders`: usage history.
- `notification_category_hint_dismissed`, `reminder_channel_versions`:
this install's notification hint and channels.
- `sync_access_dismissed`: sync-request grants of this device's accounts.
- `daily_digest_armed_at`, `reminder_last_digest_day`: daily summary bookkeeping.
- `auto_backup_files`, `auto_backup_fingerprints`: what the automatic backup
wrote to this install's folder.
- Onboarding progress and `visibility_notice_pending`.
- `auto_backup_folder_uri`: a SAF grant does not survive a reinstall or
another device.