Download this file
| 1 | import SwiftUI |
| 2 | |
| 3 | /// The member's Appearance, from `/api/v1/me` (Settings on the web): it |
| 4 | /// overrides the system's, as `data-theme` on the web's `<html>` does. |
| 5 | public enum BaseAppearance: String, Sendable, Codable, CaseIterable { |
| 6 | case system, light, dark |
| 7 | |
| 8 | /// What to pass `preferredColorScheme`: nil follows the system. |
| 9 | public var colorScheme: ColorScheme? { |
| 10 | switch self { |
| 11 | case .system: nil |
| 12 | case .light: .light |
| 13 | case .dark: .dark |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | /// Anything else the server says (a value from a newer Base) is System. |
| 18 | public init(server value: String?) { self = value.flatMap(BaseAppearance.init(rawValue:)) ?? .system } |
| 19 | } |
| 20 | |
| 21 | public extension View { |
| 22 | /// The member's Appearance over the system's. |
| 23 | func baseAppearance(_ appearance: BaseAppearance) -> some View { preferredColorScheme(appearance.colorScheme) } |
| 24 | } |
| 25 | |