30 lines
1.4 KiB
Swift
30 lines
1.4 KiB
Swift
// What the app tells the complication besides the credentials: whether it is in demo mode, and the
|
|
// last summary it saw, which the complication shows when it cannot reach the gateway. Both live in
|
|
// the shared Keychain group next to the device token, the one store the two processes share. `api`
|
|
// is the gateway the complication and its Allow and Deny buttons call.
|
|
|
|
import Foundation
|
|
|
|
enum SharedState {
|
|
private static let demoAccount = "demo"
|
|
private static let summaryAccount = "lastSummary"
|
|
|
|
/// True while the app is in demo mode, so the complication shows the demo counts too.
|
|
static var demo: Bool {
|
|
get { DeviceStore.sharedData(demoAccount) == Data([1]) }
|
|
set { DeviceStore.setSharedData(newValue ? Data([1]) : nil, account: demoAccount) }
|
|
}
|
|
|
|
/// The demo gateway in demo mode, the paired gateway otherwise, and nil when not paired.
|
|
static var api: (any GatewayAPI)? {
|
|
if demo { return DemoGateway.shared }
|
|
return DeviceStore.load().map { GatewayClient(baseURL: $0.gatewayURL, token: $0.token) }
|
|
}
|
|
|
|
/// The last summary the app or the complication saw, or nil after signing out.
|
|
static var lastSummary: Summary? {
|
|
get { DeviceStore.sharedData(summaryAccount).flatMap { try? JSON.decoder().decode(Summary.self, from: $0) } }
|
|
set { DeviceStore.setSharedData(newValue.flatMap { try? JSON.encoder().encode($0) }, account: summaryAccount) }
|
|
}
|
|
}
|