mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-15 03:01:39 +00:00
109 lines
3.6 KiB
Swift
109 lines
3.6 KiB
Swift
import Foundation
|
|
import Observation
|
|
|
|
/// Theme choice (web `ThemeMode` twin; Android
|
|
/// `feature/settings/ThemePrefs.kt`). ``oled`` is the pure-black variant — it
|
|
/// implies dark and maps to the HapiUI `.oled` palette at the app's root.
|
|
public enum ThemeMode: String, CaseIterable, Sendable {
|
|
case system
|
|
case light
|
|
case dark
|
|
case oled
|
|
|
|
/// Unknown/corrupt stored values degrade to ``system``.
|
|
public init(storageKey: String?) {
|
|
self = storageKey.flatMap(ThemeMode.init(rawValue:)) ?? .system
|
|
}
|
|
|
|
public var storageKey: String { rawValue }
|
|
|
|
/// The palette this mode resolves to: ``system`` follows the OS
|
|
/// appearance, explicit modes override it.
|
|
public func resolvedAppearance(systemIsDark: Bool) -> ResolvedAppearance {
|
|
switch self {
|
|
case .system: return systemIsDark ? .dark : .light
|
|
case .light: return .light
|
|
case .dark: return .dark
|
|
case .oled: return .oled
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The appearance a ``ThemeMode`` resolves to for a given system scheme.
|
|
/// The app maps this to the HapiUI palette (`.light` / `.dark` / `.oled`)
|
|
/// and to `preferredColorScheme`; kept SwiftUI-free here so it is testable.
|
|
public enum ResolvedAppearance: Equatable, Sendable {
|
|
case light
|
|
case dark
|
|
case oled
|
|
}
|
|
|
|
/// App language choice (web `Locale` twin: `'en' | 'zh-Hans'`).
|
|
///
|
|
/// A-M4e persists the selection only — the zh-CN string catalog and locale
|
|
/// application land with the M5 i18n pass, so changing this has no visible
|
|
/// effect yet.
|
|
public enum AppLanguage: String, CaseIterable, Sendable {
|
|
case english = "en"
|
|
case simplifiedChinese = "zh-Hans"
|
|
|
|
/// Unknown/corrupt stored values degrade to ``english``.
|
|
public init(storageKey: String?) {
|
|
self = storageKey.flatMap(AppLanguage.init(rawValue:)) ?? .english
|
|
}
|
|
|
|
public var storageKey: String { rawValue }
|
|
|
|
/// Language names are shown in their own language (standard picker
|
|
/// convention) — nothing to translate in M5 either.
|
|
public var displayName: String {
|
|
switch self {
|
|
case .english: return "English"
|
|
case .simplifiedChinese: return "简体中文"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Appearance persistence over `UserDefaults` (app-wide, like the Android
|
|
/// `hapi_prefs` DataStore). Read at the app root to drive the HapiUI palette,
|
|
/// written by the settings screen; unreadable values degrade to defaults.
|
|
@MainActor @Observable
|
|
public final class ThemePrefs {
|
|
public private(set) var mode: ThemeMode
|
|
|
|
// `let` storage is inert under @Observable (no annotation needed).
|
|
private let defaults: UserDefaults
|
|
|
|
public static let modeKey = "settings.themeMode"
|
|
|
|
public init(defaults: UserDefaults = .standard) {
|
|
self.defaults = defaults
|
|
self.mode = ThemeMode(storageKey: defaults.string(forKey: Self.modeKey))
|
|
}
|
|
|
|
public func setMode(_ mode: ThemeMode) {
|
|
self.mode = mode
|
|
defaults.set(mode.storageKey, forKey: Self.modeKey)
|
|
}
|
|
}
|
|
|
|
/// Language persistence over `UserDefaults` (persist-only until M5).
|
|
@MainActor @Observable
|
|
public final class LanguagePrefs {
|
|
public private(set) var language: AppLanguage
|
|
|
|
private let defaults: UserDefaults
|
|
|
|
public static let languageKey = "settings.appLanguage"
|
|
|
|
public init(defaults: UserDefaults = .standard) {
|
|
self.defaults = defaults
|
|
self.language = AppLanguage(storageKey: defaults.string(forKey: Self.languageKey))
|
|
}
|
|
|
|
public func setLanguage(_ language: AppLanguage) {
|
|
self.language = language
|
|
defaults.set(language.storageKey, forKey: Self.languageKey)
|
|
}
|
|
}
|