// // WatchBridgeMessage.swift // Keging // // 아이폰에서 시작한 세트의 워치 심박 측정 — 미러링 세션(HKWorkoutSession, watchOS 10+/iOS 17+)의 // 데이터 채널로 오가는 메시지 (iOS·워치 공용). // 폰 → 워치: finish(완주·저장) / discard(중단·폐기). 워치 → 폰: heartRate. // 저장 결과(saved/failed)는 세션이 끝난 뒤라 미러링 채널이 닫히므로 // WatchConnectivity(WatchSync → PhoneSync)로 따로 보낸다. // import Foundation nonisolated enum WatchBridgeMessage: Codable, Equatable { /// 폰 → 워치: 세트 완주 — 워치가 운동을 저장하고(운동 강도 포함) 결과를 WatchConnectivity로 알린다 case finish(effortScore: Int) /// 폰 → 워치: 세트 중단 — 워크아웃 폐기 (중단한 세트는 없던 것, §3.1) case discard /// 워치 → 폰: 최신 심박(bpm) case heartRate(bpm: Double) func encoded() -> Data? { try? JSONEncoder().encode(self) } static func decode(_ data: Data) -> WatchBridgeMessage? { try? JSONDecoder().decode(WatchBridgeMessage.self, from: data) } } /// WatchConnectivity로 보내는 저장 결과 (워치 → 폰) / 폐기 명령 (폰 → 워치, 미러링이 안 붙었을 때의 안전장치) nonisolated enum WatchBridgeResult { static let key = "mirrorResult" static let saved = "saved" static let failed = "failed" /// 폰 → 워치: 미러링 세션이 30초 안에 안 붙어 폰이 단독으로 넘어갔을 때 워치 세션을 정리하라는 명령 static let commandKey = "mirrorCommand" static let discard = "discard" }