Neues Feld allCategories in /api/state (nach dem Einschalten false), in der App ein zweiter Schalter unter „Automatisch wechseln“. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
3.3 KiB
Swift
115 lines
3.3 KiB
Swift
import Foundation
|
|
import Observation
|
|
|
|
/// Hält den Zustand des Würfels und gleicht ihn regelmäßig mit der API ab.
|
|
@MainActor @Observable
|
|
final class CubeModel {
|
|
/// Name oder IP-Adresse des Würfels, wird gespeichert.
|
|
var host: String {
|
|
didSet {
|
|
UserDefaults.standard.set(host, forKey: "host")
|
|
state = nil
|
|
categories = []
|
|
effects = []
|
|
shownCategory = nil
|
|
error = nil
|
|
}
|
|
}
|
|
|
|
private(set) var state: CubeState?
|
|
private(set) var categories: [CubeCategory] = []
|
|
private(set) var effects: [CubeEffect] = []
|
|
private(set) var error: String?
|
|
|
|
/// In der App gewählte Kategorie; anfangs die des laufenden Effekts.
|
|
var shownCategory: String?
|
|
|
|
/// Kategorie des Effekts, der gerade auf dem Würfel läuft
|
|
var runningCategory: String? {
|
|
guard let state else { return nil }
|
|
return effects.first { $0.id == state.effect }?.category
|
|
}
|
|
|
|
/// Wert des Helligkeitsreglers; folgt dem Würfel, solange nicht gezogen wird.
|
|
var brightness: Double = 32
|
|
var isDraggingBrightness = false
|
|
|
|
var isConnected: Bool { state != nil && error == nil }
|
|
|
|
private var api: CubeAPI { CubeAPI(host: host) }
|
|
|
|
init() {
|
|
host = UserDefaults.standard.string(forKey: "host") ?? "cube.local"
|
|
}
|
|
|
|
/// Fragt den Zustand alle 2 Sekunden ab, bis die Aufgabe abgebrochen wird.
|
|
func poll() async {
|
|
while !Task.isCancelled {
|
|
await refresh()
|
|
try? await Task.sleep(for: .seconds(2))
|
|
}
|
|
}
|
|
|
|
func refresh() async {
|
|
do {
|
|
if effects.isEmpty {
|
|
categories = try await api.categories()
|
|
effects = try await api.effects()
|
|
}
|
|
apply(try await api.state())
|
|
} catch {
|
|
self.error = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func select(_ effect: CubeEffect) async {
|
|
await update(CubeStatePatch(effect: effect.id))
|
|
}
|
|
|
|
func setAuto(_ on: Bool) async {
|
|
await update(CubeStatePatch(auto: on))
|
|
}
|
|
|
|
func setAllCategories(_ on: Bool) async {
|
|
await update(CubeStatePatch(allCategories: on))
|
|
}
|
|
|
|
func commitBrightness() async {
|
|
await update(CubeStatePatch(brightness: Int(brightness)))
|
|
}
|
|
|
|
func special(_ id: String) async {
|
|
do {
|
|
try await api.special(id)
|
|
error = nil
|
|
} catch {
|
|
self.error = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
private func update(_ patch: CubeStatePatch) async {
|
|
do {
|
|
apply(try await api.update(patch))
|
|
} catch {
|
|
self.error = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
private func apply(_ newState: CubeState) {
|
|
state = newState
|
|
error = nil
|
|
if !isDraggingBrightness { brightness = Double(newState.brightness) }
|
|
if shownCategory == nil { shownCategory = runningCategory }
|
|
}
|
|
|
|
/// Macht aus Eingaben wie "http://10.0.1.8/" ein "10.0.1.8".
|
|
static func cleanHost(_ input: String) -> String {
|
|
var host = input.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
for prefix in ["http://", "https://"] where host.lowercased().hasPrefix(prefix) {
|
|
host.removeFirst(prefix.count)
|
|
}
|
|
while host.hasSuffix("/") { host.removeLast() }
|
|
return host.isEmpty ? "cube.local" : host
|
|
}
|
|
}
|