swiftui-multiplatform/Shared/Preferences/PreferencesModel.swift

55 lines
1.5 KiB
Swift
Raw Permalink Normal View History

import SwiftUI
2023-10-23 21:15:41 +00:00
enum Appearance: Int {
case system = 0
case light = 1
case dark = 2
}
class PreferencesModel: ObservableObject {
2021-11-05 18:18:36 +00:00
private let defaults = UserDefaults.shared
2023-10-23 21:15:41 +00:00
@Published var selectedColorScheme: ColorScheme?
@Published var appearance: Int = 0
@Published var font: Int = 0 {
didSet {
defaults.set(font, forKey: WFDefaults.defaultFontIntegerKey)
}
}
2021-11-19 20:41:54 +00:00
@available(iOSApplicationExtension, unavailable)
2023-10-23 21:15:41 +00:00
func updateAppearance(to appearance: Appearance) {
#if os(iOS)
var window: UIWindow? {
guard let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window else {
return nil
}
2023-10-23 21:15:41 +00:00
return window
}
2023-10-23 21:15:41 +00:00
#endif
switch appearance {
case .light:
#if os(macOS)
NSApp.appearance = NSAppearance(named: .aqua)
#else
window?.overrideUserInterfaceStyle = .light
#endif
case .dark:
#if os(macOS)
NSApp.appearance = NSAppearance(named: .darkAqua)
#else
window?.overrideUserInterfaceStyle = .dark
#endif
default:
#if os(macOS)
NSApp.appearance = nil
#else
window?.overrideUserInterfaceStyle = .unspecified
#endif
}
}
}