Files
FamilyMealPlanner/MealMood/Services/WidgetDataStore.swift
T
alexandrev-tibco a0f9fa63d3 platos: segundo plato por comida (ensalada + salmon)
- MealSlot.secondaryDishId (opcional, aditivo CloudKit); se limpia al
  sustituir/quitar/vaciar/marcar comer fuera y viaja en swaps, copia de
  semana anterior, undo y snapshot KV de iCloud (retrocompatible)
- Picker de hueco lleno: toggle "anadir como segundo plato" + boton para
  quitarlo; HomeViewModel.assignSecondaryDish/removeSecondaryDish
- Se muestra "Plato + Segundo" en calendario, widget y export; la lista
  de la compra incluye los ingredientes del segundo; stats lo cuentan
- Evento analytics secondary_dish_added; strings en 6 idiomas

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013H6bXqGX1ygwib1Dm3n3UG
2026-09-01 11:37:23 +02:00

66 lines
2.6 KiB
Swift

import Foundation
import WidgetKit
/// Snapshot of today's meals for the widget. Generic over meal types (2.0):
/// one entry per active meal type, in chronological order.
struct TodayMealData: Codable {
struct Meal: Codable {
let type: String // MealType raw value (drives icon/color in widget)
let label: String // localized meal name
let name: String? // dish name, nil when the slot is empty
}
let meals: [Meal]
let weekdayName: String
let updatedAt: Date
}
enum WidgetDataStore {
static let appGroupID = "group.com.alexandrevazquez.mealmood"
private static let key = "mealmood.today_meals"
static func update(plan: WeekPlan?, dishes: [Dish], settings: AppSettings) {
let today = Date()
let weekday = Calendar.current.component(.weekday, from: today)
// weekday: 1=Sun,2=Mon..7=Sat appDayOfWeek: 0=Mon..6=Sun
let appDayOfWeek = (weekday + 5) % 7
let meals: [TodayMealData.Meal] = settings.activeMealTypes.map { mealType in
let slot = plan?.slotList.first {
$0.dayOfWeek == appDayOfWeek && $0.mealType == mealType.rawValue
}
let primaryName = slot?.dishId.flatMap { id in dishes.first { $0.id == id }?.name }
let secondaryName = slot?.secondaryDishId.flatMap { id in dishes.first { $0.id == id }?.name }
let name = primaryName.map { p in secondaryName.map { "\(p) + \($0)" } ?? p }
return TodayMealData.Meal(
type: mealType.rawValue,
label: String(localized: String.LocalizationValue(mealType.localizedKey)),
name: name
)
}
let locale = Locale(identifier: settings.languageEnum.resolved().localeIdentifier)
let formatter = DateFormatter()
formatter.locale = locale
formatter.dateFormat = "EEEE"
let weekdayName = formatter.string(from: today).capitalized(with: locale)
let data = TodayMealData(
meals: meals,
weekdayName: weekdayName,
updatedAt: today
)
guard let defaults = UserDefaults(suiteName: appGroupID),
let encoded = try? JSONEncoder().encode(data) else { return }
defaults.set(encoded, forKey: key)
WidgetCenter.shared.reloadAllTimelines()
}
static func read() -> TodayMealData? {
guard let defaults = UserDefaults(suiteName: appGroupID),
let data = defaults.data(forKey: key) else { return nil }
return try? JSONDecoder().decode(TodayMealData.self, from: data)
}
}