mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-09 00:55:52 -06:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> This PR removes the `options` parameters which use the default value. ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> <img width="1014" alt="Screenshot 2024-03-03 at 21 17 55" src="https://github.com/home-assistant/iOS/assets/35694712/85324caf-c36d-4366-a9f8-9c349192a361"> <img width="1049" alt="Screenshot 2024-03-03 at 21 17 17" src="https://github.com/home-assistant/iOS/assets/35694712/cebddbe6-8ea4-415e-b12a-0e35f86bf5a3"> <img width="1022" alt="Screenshot 2024-03-03 at 21 16 53" src="https://github.com/home-assistant/iOS/assets/35694712/ccccb902-9791-42f6-8868-5900093c40ae"> <img width="991" alt="Screenshot 2024-03-03 at 21 16 28" src="https://github.com/home-assistant/iOS/assets/35694712/f2f33685-a55c-47ed-a555-8cc0fc1561ce"> <img width="1329" alt="Screenshot 2024-03-03 at 21 16 00" src="https://github.com/home-assistant/iOS/assets/35694712/2dc156da-29d6-44e4-a064-4fbdb79139bd"> <img width="1130" alt="Screenshot 2024-03-03 at 21 14 53" src="https://github.com/home-assistant/iOS/assets/35694712/628dd2ff-5bba-4ee7-912b-cdbdf73f18b2">
78 lines
2.3 KiB
Swift
78 lines
2.3 KiB
Swift
import Foundation
|
|
import HAKit
|
|
|
|
public class LocalPushStateSync: UserDefaultsValueSync<LocalPushManager.State> {
|
|
public static let settingsKey = "LocalPushSettingsKey"
|
|
}
|
|
|
|
public class UserDefaultsValueSync<ValueType: Codable>: NSObject {
|
|
public let settingsKey: String
|
|
public let userDefaults: UserDefaults
|
|
|
|
public init(settingsKey: String, userDefaults: UserDefaults? = nil) {
|
|
self.settingsKey = settingsKey
|
|
self.userDefaults = userDefaults ?? Current.settingsStore.prefs
|
|
super.init()
|
|
self.userDefaults.addObserver(
|
|
self,
|
|
forKeyPath: settingsKey,
|
|
context: nil
|
|
)
|
|
}
|
|
|
|
deinit {
|
|
userDefaults.removeObserver(self, forKeyPath: settingsKey)
|
|
}
|
|
|
|
public var value: ValueType? {
|
|
set {
|
|
do {
|
|
if let state = newValue {
|
|
let json = try JSONEncoder().encode(state)
|
|
userDefaults.set(json, forKey: settingsKey)
|
|
} else {
|
|
userDefaults.removeObject(forKey: settingsKey)
|
|
}
|
|
} catch {
|
|
Current.Log.error("failed to encode: \(error)")
|
|
}
|
|
}
|
|
get {
|
|
guard let data = userDefaults.data(forKey: settingsKey) else {
|
|
return nil
|
|
}
|
|
|
|
do {
|
|
let value = try JSONDecoder().decode(ValueType.self, from: data)
|
|
return value
|
|
} catch {
|
|
Current.Log.error("failed to decode: \(error)")
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
private var observers = [(identifier: UUID, handler: (ValueType) -> Void)]()
|
|
public func observe(_ handler: @escaping (ValueType) -> Void) -> HACancellable {
|
|
let identifier = UUID()
|
|
observers.append((identifier: identifier, handler: handler))
|
|
return HABlockCancellable { [weak self] in
|
|
self?.observers.removeAll(where: { $0.identifier == identifier })
|
|
}
|
|
}
|
|
|
|
// swiftlint:disable:next block_based_kvo
|
|
override public func observeValue(
|
|
forKeyPath keyPath: String?,
|
|
of object: Any?,
|
|
change: [NSKeyValueChangeKey: Any]?,
|
|
context: UnsafeMutableRawPointer?
|
|
) {
|
|
guard let value else { return }
|
|
|
|
for observer in observers {
|
|
observer.handler(value)
|
|
}
|
|
}
|
|
}
|