75 lines
2.5 KiB
Swift
75 lines
2.5 KiB
Swift
import Foundation
|
|
import LocalData
|
|
|
|
/// Legacy notification toggle stored in UserDefaults.
|
|
extension StorageKey where Value == Bool {
|
|
nonisolated static let legacyNotificationSetting = StorageKey(
|
|
name: "legacy_notification_setting",
|
|
domain: .userDefaults(suite: nil),
|
|
security: .none,
|
|
serializer: .json,
|
|
owner: "MigrationDemo",
|
|
description: "Legacy notification setting stored as Bool.",
|
|
availability: .all,
|
|
syncPolicy: .never
|
|
)
|
|
}
|
|
|
|
/// Legacy theme preference stored separately from notifications.
|
|
extension StorageKey where Value == String {
|
|
nonisolated static let legacyThemeSetting = StorageKey(
|
|
name: "legacy_theme_setting",
|
|
domain: .userDefaults(suite: nil),
|
|
security: .none,
|
|
serializer: .json,
|
|
owner: "MigrationDemo",
|
|
description: "Legacy theme setting stored as a string.",
|
|
availability: .all,
|
|
syncPolicy: .never
|
|
)
|
|
}
|
|
|
|
/// Modern unified settings created by aggregating multiple legacy keys.
|
|
extension StorageKey where Value == UnifiedSettings {
|
|
|
|
nonisolated static let modernUnifiedSettings = StorageKey(
|
|
name: "modern_unified_settings",
|
|
domain: .fileSystem(directory: .documents),
|
|
security: .none,
|
|
serializer: .json,
|
|
owner: "MigrationDemo",
|
|
description: "Modern unified settings aggregated from legacy keys.",
|
|
availability: .all,
|
|
syncPolicy: .never,
|
|
migration: { key in
|
|
let sources: [AnyStorageKey] = [
|
|
.key(.legacyNotificationSetting),
|
|
.key(.legacyThemeSetting)
|
|
]
|
|
return AnyStorageMigration(
|
|
DefaultAggregatingMigration(
|
|
destinationKey: key,
|
|
sourceKeys: sources
|
|
) { sources in
|
|
// Merge legacy values into a single strongly-typed settings model.
|
|
var notificationsEnabled = false
|
|
var theme = "system"
|
|
|
|
for source in sources {
|
|
if let boolValue = source.value as? Bool {
|
|
notificationsEnabled = boolValue
|
|
} else if let stringValue = source.value as? String {
|
|
theme = stringValue
|
|
}
|
|
}
|
|
|
|
return UnifiedSettings(
|
|
notificationsEnabled: notificationsEnabled,
|
|
theme: theme
|
|
)
|
|
}
|
|
)
|
|
}
|
|
)
|
|
}
|