94 lines
3.2 KiB
Swift
94 lines
3.2 KiB
Swift
import SwiftUI
|
|
import LocalData
|
|
|
|
@MainActor
|
|
struct ConditionalMigrationDemo: View {
|
|
@State private var legacyValue = ""
|
|
@State private var modernValue = ""
|
|
@State private var statusMessage = ""
|
|
@State private var isLoading = false
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section("The Scenario") {
|
|
Text("A conditional migration only runs when the app version is below a threshold.")
|
|
.font(.caption)
|
|
.foregroundStyle(Color.Text.secondary)
|
|
}
|
|
|
|
Section("Step 1: Setup Legacy Data") {
|
|
Text("Save a legacy app mode string that will migrate only if the version condition is met.")
|
|
.font(.caption)
|
|
.foregroundStyle(Color.Text.secondary)
|
|
|
|
TextField("Legacy App Mode", text: $legacyValue)
|
|
.textFieldStyle(.roundedBorder)
|
|
|
|
Button(action: saveToLegacy) {
|
|
Label("Save Legacy Mode", systemImage: "square.and.arrow.down")
|
|
}
|
|
.disabled(legacyValue.isEmpty || isLoading)
|
|
}
|
|
|
|
Section("Step 2: Trigger Conditional Migration") {
|
|
Text("Load the modern key to run the conditional migration.")
|
|
.font(.caption)
|
|
.foregroundStyle(Color.Text.secondary)
|
|
|
|
Button(action: loadFromModern) {
|
|
Label("Load Modern Mode", systemImage: "sparkles")
|
|
}
|
|
.disabled(isLoading)
|
|
|
|
if !modernValue.isEmpty {
|
|
LabeledContent("Migrated Mode", 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("Conditional Migration")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
}
|
|
|
|
private func saveToLegacy() {
|
|
isLoading = true
|
|
Task {
|
|
do {
|
|
let key = StorageKey.legacyAppMode
|
|
try await StorageRouter.shared.set(legacyValue, for: key)
|
|
statusMessage = "✓ Saved legacy app mode"
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
|
|
private func loadFromModern() {
|
|
isLoading = true
|
|
statusMessage = "Loading modern mode..."
|
|
Task {
|
|
do {
|
|
let key = StorageKey.modernAppMode
|
|
let value = try await StorageRouter.shared.get(key)
|
|
modernValue = value
|
|
statusMessage = "✓ Conditional migration complete."
|
|
} catch StorageError.notFound {
|
|
statusMessage = "Modern key is empty (and no legacy data found)."
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
}
|