Some checks failed
CI / test (push) Has been cancelled
Move the app payload under swift/ while keeping git, package.json, and .smartconfig.json at the repo root. This standardizes the Swift app setup so build, test, run, and watch workflows match the other repos.
47 lines
1.2 KiB
Swift
47 lines
1.2 KiB
Swift
import Foundation
|
|
|
|
struct PersistedAppState: Codable, Equatable {
|
|
let session: AuthSession
|
|
let profile: MemberProfile
|
|
let requests: [ApprovalRequest]
|
|
let notifications: [AppNotification]
|
|
}
|
|
|
|
protocol AppStateStoring {
|
|
func load() -> PersistedAppState?
|
|
func save(_ state: PersistedAppState)
|
|
func clear()
|
|
}
|
|
|
|
final class UserDefaultsAppStateStore: AppStateStoring {
|
|
private let defaults: UserDefaults
|
|
private let storageKey: String
|
|
private let encoder = JSONEncoder()
|
|
private let decoder = JSONDecoder()
|
|
|
|
init(defaults: UserDefaults = .standard, storageKey: String = "persisted-app-state") {
|
|
self.defaults = defaults
|
|
self.storageKey = storageKey
|
|
}
|
|
|
|
func load() -> PersistedAppState? {
|
|
guard let data = defaults.data(forKey: storageKey) else {
|
|
return nil
|
|
}
|
|
|
|
return try? decoder.decode(PersistedAppState.self, from: data)
|
|
}
|
|
|
|
func save(_ state: PersistedAppState) {
|
|
guard let data = try? encoder.encode(state) else {
|
|
return
|
|
}
|
|
|
|
defaults.set(data, forKey: storageKey)
|
|
}
|
|
|
|
func clear() {
|
|
defaults.removeObject(forKey: storageKey)
|
|
}
|
|
}
|