4ffa15b06a
Foundation for 2.1 family sharing (CKShare needs these models + container). SwiftData cannot share across Apple IDs today, so 2.0 ships true multi-device sync for the same account instead: - Models made CloudKit-compatible: dropped @Attribute(.unique) on all 5, inline defaults on every attribute, WeekPlan.slots stored as optional relationship (name preserved → lightweight migration) with non-optional slotList facade; ~73 call sites renamed. - Container: cloudKitDatabase .automatic, falling back to the local-only store when CloudKit is unavailable; failures recorded to Crashlytics. - Entitlements: iCloud CloudKit service (container already existed); remote-notification background mode for push-driven sync. - Legacy KV snapshot sync (ICloudSyncService) stays inert when CloudKit is active — kept only for 1.x devices. - DeduplicationService collapses cross-device duplicates deterministically on launch (settings singleton, default tags with tagId remapping, same-week plans). - Note: AppSettings.isPremium now syncs across same-Apple-ID devices; StoreKit (PremiumSyncService + Transaction.updates) remains the source of truth and reconciles on every launch/foreground. All 21 unit tests pass, including ICloudSyncPremiumIsolationTests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BkanrydYtrme8wipTzWssG
64 lines
2.4 KiB
Swift
64 lines
2.4 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 dishId = plan?.slotList.first {
|
|
$0.dayOfWeek == appDayOfWeek && $0.mealType == mealType.rawValue
|
|
}?.dishId
|
|
let name = dishId.flatMap { id in dishes.first { $0.id == id }?.name }
|
|
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)
|
|
}
|
|
}
|