38 lines
1.5 KiB
Swift
38 lines
1.5 KiB
Swift
import Foundation
|
|
import Observation
|
|
import CoreFoundation
|
|
|
|
nonisolated enum AppAppearance: String, CaseIterable, Identifiable {
|
|
case system, light, dark
|
|
var id: String { rawValue }
|
|
}
|
|
|
|
@MainActor @Observable
|
|
final class AppearancePreferences {
|
|
@ObservationIgnored private let defaults: UserDefaults
|
|
var appearance: AppAppearance { didSet { defaults.set(appearance.rawValue, forKey: "appearance") } }
|
|
|
|
var reduceMotion: Bool {
|
|
didSet { if reduceMotion != oldValue { defaults.set(reduceMotion, forKey: "appearance.reduceMotion") } }
|
|
}
|
|
|
|
init(defaults: UserDefaults = .standard) {
|
|
self.defaults = defaults
|
|
let value = defaults.object(forKey: "appearance.reduceMotion") as? NSNumber
|
|
if defaults.volatileDomain(forName: UserDefaults.argumentDomain)["appearance.reduceMotion"] != nil {
|
|
// Foundation represents launch overrides as strings; do not persist them.
|
|
reduceMotion = defaults.bool(forKey: "appearance.reduceMotion")
|
|
} else {
|
|
reduceMotion = value.map { CFGetTypeID($0) == CFBooleanGetTypeID() && $0.boolValue } ?? false
|
|
}
|
|
appearance = AppAppearance(rawValue: defaults.string(forKey: "appearance") ?? "") ?? .system
|
|
}
|
|
}
|
|
|
|
/// Motion changes presentation only; sampling preferences are deliberately absent.
|
|
nonisolated enum MetricMotionPolicy {
|
|
static func enabled(reduced: Bool, systemReduced: Bool, visible: Bool, paused: Bool, sleeping: Bool) -> Bool {
|
|
!reduced && !systemReduced && visible && !paused && !sleeping
|
|
}
|
|
}
|