// Where a DEVICE-LOCAL preference is kept — the Swift half of AsyncStorage. // // Two of this lane's screens keep something that is not in the person's // database and never syncs: the notification switches (`@cotton:notifprefs`) // and the photo-library switch (`@cotton:librarySync:enabled:v1:`). // A third — the appearance preference, `@cotton/theme-preference` — went with // dark mode: the app is light-only and there is nothing left to store. // // **The KEYS carry across unchanged.** They are what the previous app wrote, // and a person moving to this build should find their switches where they left // them. // `UserDefaults` is not `AsyncStorage`'s store, so nothing is actually read // across — the keys are kept because inventing new ones would make that // migration impossible later as well as now. // // **This is deliberately NOT `SecretStore`.** That is the Keychain, for // somebody else's credential; this is `UserDefaults`, for a switch. Sharing one // type between them would mean one set of rules for two things that need // different ones — the sentence `SecretStore.swift`'s own header makes about // `DeviceKey`. // // It lives in the Notifications folder because this lane's files are scoped to // its four screen folders and this is the first of the three to need it; // **folding it into `CottonKit` beside `SecretStore` is the orchestrator's**, // and is named in this lane's report. // // A protocol for the reason `SecretStore` is one: a simulator's defaults are // shared between test runs, so a test that writes into the real ones is a test // that passes once. import Foundation /// A tiny string store. Strings only, because every one of the three values is /// either a flag or a JSON document, and a store that knew about types would be /// a second encoder to keep in step with the migrators below. public protocol DevicePreferences: Sendable { func string(_ key: String) -> String? /// `nil` REMOVES the key. `writeLibrarySyncEnabled` turns the switch off by /// removing rather than by writing "0", and the read is /// `=== "1"`, so absence has to be expressible. func setString(_ value: String?, _ key: String) } /// The real one. /// `@unchecked Sendable` because `UserDefaults` is thread-safe by contract and /// carries no annotation saying so; the struct holds nothing else. public struct UserDefaultsPreferences: DevicePreferences, @unchecked Sendable { private let defaults: UserDefaults public init(_ defaults: UserDefaults = .standard) { self.defaults = defaults } public func string(_ key: String) -> String? { defaults.string(forKey: key) } public func setString(_ value: String?, _ key: String) { if let value { defaults.set(value, forKey: key) } else { defaults.removeObject(forKey: key) } } } /// The double every test drives. public final class InMemoryPreferences: DevicePreferences, @unchecked Sendable { private let lock = NSLock() private var values: [String: String] public init(_ values: [String: String] = [:]) { self.values = values } public func string(_ key: String) -> String? { lock.lock() defer { lock.unlock() } return values[key] } public func setString(_ value: String?, _ key: String) { lock.lock() defer { lock.unlock() } values[key] = value } /// A TEST's question: which keys hold something. The real defaults could /// answer it too, but nothing in the app ever asks. public var occupied: [String] { lock.lock() defer { lock.unlock() } return values.keys.sorted() } }