140 lines
4.3 KiB
Swift
140 lines
4.3 KiB
Swift
//
|
|
// UserDefaultsDemo.swift
|
|
// SecureStorgageSample
|
|
//
|
|
// Demonstrates UserDefaults storage with LocalData package.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LocalData
|
|
|
|
struct UserDefaultsDemo: View {
|
|
@State private var inputText = ""
|
|
@State private var storedValue = ""
|
|
@State private var statusMessage = ""
|
|
@State private var isLoading = false
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
Text("UserDefaults stores simple data persistently. Great for preferences and small values.")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
|
|
Section("Store Value") {
|
|
TextField("Enter a value", text: $inputText)
|
|
.textFieldStyle(.roundedBorder)
|
|
|
|
Button(action: saveValue) {
|
|
HStack {
|
|
Image(systemName: "square.and.arrow.down")
|
|
Text("Save to UserDefaults")
|
|
}
|
|
}
|
|
.disabled(inputText.isEmpty || isLoading)
|
|
}
|
|
|
|
Section("Retrieve Value") {
|
|
Button(action: loadValue) {
|
|
HStack {
|
|
Image(systemName: "arrow.down.circle")
|
|
Text("Load from UserDefaults")
|
|
}
|
|
}
|
|
.disabled(isLoading)
|
|
|
|
if !storedValue.isEmpty {
|
|
HStack {
|
|
Text("Stored:")
|
|
Spacer()
|
|
Text(storedValue)
|
|
.foregroundStyle(.blue)
|
|
}
|
|
}
|
|
}
|
|
|
|
Section("Remove Value") {
|
|
Button(action: removeValue) {
|
|
HStack {
|
|
Image(systemName: "trash")
|
|
Text("Remove from UserDefaults")
|
|
}
|
|
}
|
|
.foregroundStyle(.red)
|
|
.disabled(isLoading)
|
|
}
|
|
|
|
if !statusMessage.isEmpty {
|
|
Section {
|
|
Text(statusMessage)
|
|
.font(.caption)
|
|
.foregroundStyle(statusMessage.contains("Error") ? .red : .green)
|
|
}
|
|
}
|
|
|
|
Section("Key Configuration") {
|
|
LabeledContent("Domain", value: "UserDefaults (standard)")
|
|
LabeledContent("Security", value: "None")
|
|
LabeledContent("Serializer", value: "JSON")
|
|
LabeledContent("Platform", value: "All")
|
|
LabeledContent("Sync Policy", value: "Automatic Small")
|
|
}
|
|
}
|
|
.navigationTitle("UserDefaults")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
}
|
|
|
|
private func saveValue() {
|
|
isLoading = true
|
|
Task {
|
|
do {
|
|
let key = StorageKeys.AppVersionKey()
|
|
try await StorageRouter.shared.set(inputText, for: key)
|
|
statusMessage = "✓ Saved successfully"
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
|
|
private func loadValue() {
|
|
isLoading = true
|
|
Task {
|
|
do {
|
|
let key = StorageKeys.AppVersionKey()
|
|
storedValue = try await StorageRouter.shared.get(key)
|
|
statusMessage = "✓ Loaded successfully"
|
|
} catch StorageError.notFound {
|
|
storedValue = ""
|
|
statusMessage = "No value stored yet"
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
|
|
private func removeValue() {
|
|
isLoading = true
|
|
Task {
|
|
do {
|
|
let key = StorageKeys.AppVersionKey()
|
|
try await StorageRouter.shared.remove(key)
|
|
storedValue = ""
|
|
statusMessage = "✓ Removed successfully"
|
|
} catch {
|
|
statusMessage = "Error: \(error.localizedDescription)"
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
NavigationStack {
|
|
UserDefaultsDemo()
|
|
}
|
|
}
|