104 lines
3.6 KiB
Swift
104 lines
3.6 KiB
Swift
import SwiftUI
|
|
import LocalData
|
|
|
|
@MainActor
|
|
struct AggregatingMigrationDemo: View {
|
|
@State private var notificationsEnabled = false
|
|
@State private var theme = ""
|
|
@State private var modernValue = ""
|
|
@State private var statusMessage = ""
|
|
@State private var isLoading = false
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section("The Scenario") {
|
|
Text("Two legacy settings are aggregated into a single unified settings model during migration.")
|
|
.font(.caption)
|
|
.foregroundStyle(Color.Text.secondary)
|
|
}
|
|
|
|
Section("Step 1: Setup Legacy Data") {
|
|
Toggle("Notifications Enabled", isOn: $notificationsEnabled)
|
|
|
|
TextField("Theme", text: $theme)
|
|
.textFieldStyle(.roundedBorder)
|
|
|
|
Button(action: saveToLegacy) {
|
|
Label("Save Legacy Settings", systemImage: "tray.and.arrow.down")
|
|
}
|
|
.disabled(theme.isEmpty || isLoading)
|
|
}
|
|
|
|
Section("Step 2: Trigger Migration") {
|
|
Text("Load the modern key to aggregate legacy values into unified settings.")
|
|
.font(.caption)
|
|
.foregroundStyle(Color.Text.secondary)
|
|
|
|
Button(action: loadFromModern) {
|
|
Label("Load Unified Settings", systemImage: "sparkles")
|
|
}
|
|
.disabled(isLoading)
|
|
|
|
if !modernValue.isEmpty {
|
|
LabeledContent("Unified Settings", value: modernValue)
|
|
.foregroundStyle(Color.Status.success)
|
|
.bold()
|
|
}
|
|
}
|
|
|
|
if !statusMessage.isEmpty {
|
|
Section {
|
|
Text(statusMessage)
|
|
.font(.caption)
|
|
.foregroundStyle(statusMessage.contains("Error") ? Color.Status.error : Color.Status.info)
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Aggregating Migration")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
}
|
|
|
|
private func saveToLegacy() {
|
|
isLoading = true
|
|
Task {
|
|
do {
|
|
try await StorageRouter.shared.set(
|
|
notificationsEnabled,
|
|
for: StorageKeys.LegacyNotificationSettingKey()
|
|
)
|
|
try await StorageRouter.shared.set(
|
|
theme,
|
|
for: StorageKeys.LegacyThemeSettingKey()
|
|
)
|
|
statusMessage = "✓ Saved legacy settings"
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
|
|
private func loadFromModern() {
|
|
isLoading = true
|
|
statusMessage = "Loading unified settings..."
|
|
Task {
|
|
do {
|
|
let value = try await StorageRouter.shared.get(StorageKeys.ModernUnifiedSettingsKey())
|
|
modernValue = format(value)
|
|
statusMessage = "✓ Migration complete."
|
|
} catch StorageError.notFound {
|
|
statusMessage = "Modern key is empty (and no legacy data found)."
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
|
|
private func format(_ value: UnifiedSettings) -> String {
|
|
let notificationsText = value.notificationsEnabled ? "On" : "Off"
|
|
let themeText = value.theme.isEmpty ? "system" : value.theme
|
|
return "Notifications: \(notificationsText), Theme: \(themeText)"
|
|
}
|
|
}
|