// // WatchConnectivityManager.swift // Feels Watch App // // Watch-side connectivity - sends mood to iPhone for centralized logging. // import Foundation import WatchConnectivity import WidgetKit import os.log /// Watch-side connectivity manager /// Sends mood votes to iPhone for centralized logging final class WatchConnectivityManager: NSObject, ObservableObject { static let shared = WatchConnectivityManager() private static let logger = Logger(subsystem: "com.tt.ifeel.watchkitapp", category: "WatchConnectivity") private var session: WCSession? private override init() { super.init() if WCSession.isSupported() { session = WCSession.default session?.delegate = self session?.activate() Self.logger.info("WCSession activated") } else { Self.logger.warning("WCSession not supported") } } // MARK: - Watch → iOS /// Send mood to iOS app for centralized logging func sendMoodToPhone(mood: Int, date: Date) -> Bool { guard let session = session, session.activationState == .activated else { Self.logger.warning("WCSession not ready") return false } let message: [String: Any] = [ "action": "logMood", "mood": mood, "date": date.timeIntervalSince1970 ] // Use transferUserInfo for guaranteed delivery session.transferUserInfo(message) Self.logger.info("Sent mood \(mood) to iPhone") return true } } // MARK: - WCSessionDelegate extension WatchConnectivityManager: WCSessionDelegate { func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { if let error = error { Self.logger.error("WCSession activation failed: \(error.localizedDescription)") } else { Self.logger.info("WCSession activated: \(activationState.rawValue)") } } // Receive reload notification from iOS func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) { if userInfo["action"] as? String == "reloadWidgets" { Self.logger.info("Received reload notification from iPhone") Task { @MainActor in WidgetCenter.shared.reloadAllTimelines() } } } func session(_ session: WCSession, didReceiveMessage message: [String: Any]) { if message["action"] as? String == "reloadWidgets" { Task { @MainActor in WidgetCenter.shared.reloadAllTimelines() } } } }