Files
FamilyMealPlanner/MealMoodWatch/MealMoodWatchApp.swift
T
alexandrev-tibco c0b485387d 2.1.0: app de Apple Watch, complicacion y widget semanal en iOS
- Target MealMoodWatch (watchOS 10, SwiftUI): vista de hoy y de la
  semana en TabView vertical; recibe el snapshot del iPhone por
  WatchConnectivity (updateApplicationContext) y lo guarda en su app
  group para la complicacion
- Target MealMoodWatchWidget: complicacion accessoryRectangular/
  Circular/Inline con las comidas de hoy (heuristica comida/cena por
  hora); icono del watch generado desde el logo
- WatchWeekPayload compartido entre iOS app, widget iOS, watch app y
  complicacion — llega ya localizado desde el iPhone
- Widget iOS nuevo "Semana" (systemMedium/Large) con los 7 dias;
  el widget de hoy pasa a WidgetBundle
- Targets creados via gema xcodeproj (sin abrir Xcode); embed del watch
  antes del script de Crashlytics para evitar ciclo de build

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013H6bXqGX1ygwib1Dm3n3UG
2026-09-08 16:38:58 +02:00

58 lines
1.7 KiB
Swift

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)
}
}
}