Initial commit — RoadCode import
This commit is contained in:
77
BlackRoadWatch/Sources/WatchApp/BlackRoadWatchApp.swift
Normal file
77
BlackRoadWatch/Sources/WatchApp/BlackRoadWatchApp.swift
Normal file
@@ -0,0 +1,77 @@
|
||||
import SwiftUI
|
||||
import WatchConnectivity
|
||||
|
||||
// MARK: - Watch App Entry Point
|
||||
|
||||
@main
|
||||
struct BlackRoadWatchApp: App {
|
||||
@StateObject private var dataStore = WatchDataStore.shared
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
WatchContentView()
|
||||
.environmentObject(dataStore)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Watch Data Store (receives from iPhone)
|
||||
|
||||
class WatchDataStore: NSObject, ObservableObject {
|
||||
static let shared = WatchDataStore()
|
||||
|
||||
@Published var sensor: SensorData?
|
||||
@Published var ai: AIStatus?
|
||||
@Published var health: SystemHealth?
|
||||
@Published var lastUpdate: Date?
|
||||
@Published var isConnected = false
|
||||
|
||||
private var wcSession: WCSession?
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
guard WCSession.isSupported() else { return }
|
||||
wcSession = WCSession.default
|
||||
wcSession?.delegate = self
|
||||
wcSession?.activate()
|
||||
}
|
||||
|
||||
private func processContext(_ context: [String: Any]) {
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
guard let self = self else { return }
|
||||
|
||||
if let sensorDict = context["sensor"],
|
||||
let sensorData = try? JSONSerialization.data(withJSONObject: sensorDict),
|
||||
let sensor = try? JSONDecoder().decode(SensorData.self, from: sensorData) {
|
||||
self.sensor = sensor
|
||||
}
|
||||
|
||||
if let aiDict = context["ai"],
|
||||
let aiData = try? JSONSerialization.data(withJSONObject: aiDict),
|
||||
let ai = try? JSONDecoder().decode(AIStatus.self, from: aiData) {
|
||||
self.ai = ai
|
||||
}
|
||||
|
||||
if let healthDict = context["health"],
|
||||
let healthData = try? JSONSerialization.data(withJSONObject: healthDict),
|
||||
let health = try? JSONDecoder().decode(SystemHealth.self, from: healthData) {
|
||||
self.health = health
|
||||
}
|
||||
|
||||
self.lastUpdate = Date()
|
||||
self.isConnected = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension WatchDataStore: WCSessionDelegate {
|
||||
func session(_ session: WCSession, activationDidCompleteWith state: WCSessionActivationState, error: Error?) {}
|
||||
|
||||
func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
|
||||
processContext(message)
|
||||
}
|
||||
|
||||
func session(_ session: WCSession, didReceiveApplicationContext context: [String: Any]) {
|
||||
processContext(context)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user