a298b5e421
CI / test (push) Has been cancelled
Switch the app to the real passport enrollment, dashboard, device, alert, and challenge APIs so it can pair with idp.global and act on server-backed state instead of demo data.
87 lines
2.5 KiB
Swift
87 lines
2.5 KiB
Swift
import Foundation
|
|
|
|
enum SharedDefaults {
|
|
static let appGroupIdentifier = "group.global.idp.app"
|
|
|
|
static var userDefaults: UserDefaults {
|
|
UserDefaults(suiteName: appGroupIdentifier) ?? .standard
|
|
}
|
|
}
|
|
|
|
struct PersistedAppState: Codable, Equatable {
|
|
let session: AuthSession
|
|
let profile: MemberProfile
|
|
let requests: [ApprovalRequest]
|
|
let notifications: [AppNotification]
|
|
let devices: [PassportDeviceRecord]
|
|
|
|
init(
|
|
session: AuthSession,
|
|
profile: MemberProfile,
|
|
requests: [ApprovalRequest],
|
|
notifications: [AppNotification],
|
|
devices: [PassportDeviceRecord] = []
|
|
) {
|
|
self.session = session
|
|
self.profile = profile
|
|
self.requests = requests
|
|
self.notifications = notifications
|
|
self.devices = devices
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case session
|
|
case profile
|
|
case requests
|
|
case notifications
|
|
case devices
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
session = try container.decode(AuthSession.self, forKey: .session)
|
|
profile = try container.decode(MemberProfile.self, forKey: .profile)
|
|
requests = try container.decode([ApprovalRequest].self, forKey: .requests)
|
|
notifications = try container.decode([AppNotification].self, forKey: .notifications)
|
|
devices = try container.decodeIfPresent([PassportDeviceRecord].self, forKey: .devices) ?? []
|
|
}
|
|
}
|
|
|
|
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 = SharedDefaults.userDefaults, 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)
|
|
}
|
|
}
|