import SwiftUI import WatchConnectivity import WidgetKit @main struct MealMoodWatchApp: App { @StateObject private var store = WatchWeekStore.shared var body: some Scene { WindowGroup { TabView { TodayView() WeekView() } .tabViewStyle(.verticalPage) .environmentObject(store) } } } /// Receives the week snapshot from the iPhone and persists it in the watch's /// app group so the complication can read it too. final class WatchWeekStore: NSObject, ObservableObject, WCSessionDelegate { static let shared = WatchWeekStore() @Published var payload: WatchWeekPayload? private override init() { super.init() payload = WatchWeekPayload.stored() guard WCSession.isSupported() else { return } WCSession.default.delegate = self WCSession.default.activate() } private func apply(_ data: Data) { guard let received = WatchWeekPayload.decode(data) else { return } DispatchQueue.main.async { self.payload = received received.store() WidgetCenter.shared.reloadAllTimelines() } } func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { // Pick up a context delivered while the app wasn't running. if let data = session.receivedApplicationContext[WatchWeekPayload.storageKey] as? Data { apply(data) } } func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String: Any]) { if let data = applicationContext[WatchWeekPayload.storageKey] as? Data { apply(data) } } }