Files
FamilyMealPlanner/MealMood/Models/MealSlot.swift
T
alexandrev-tibco 4ffa15b06a 2.0: CloudKit private-database sync replaces KV snapshot sync
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
2026-07-12 18:13:45 +02:00

56 lines
1.6 KiB
Swift

import Foundation
import SwiftData
// CloudKit-compatible (2.0): no unique constraints, inline defaults everywhere.
@Model
final class MealSlot {
var id: UUID = UUID()
var dayOfWeek: Int = 0 // 0=Monday, 6=Sunday
var mealType: String = MealType.dinner.rawValue
var dishId: UUID?
var calendarEventId: String?
var isRuleOverridden: Bool = false
var isEatingOut: Bool = false
var weekPlan: WeekPlan?
init(
id: UUID = UUID(),
dayOfWeek: Int,
mealType: String,
dishId: UUID? = nil,
calendarEventId: String? = nil,
isRuleOverridden: Bool = false,
isEatingOut: Bool = false
) {
self.id = id
self.dayOfWeek = dayOfWeek
self.mealType = mealType
self.dishId = dishId
self.calendarEventId = calendarEventId
self.isRuleOverridden = isRuleOverridden
self.isEatingOut = isEatingOut
}
var mealTypeEnum: MealType {
get { MealType(rawValue: mealType) ?? .dinner }
set { mealType = newValue.rawValue }
}
}
extension MealSlot {
static func preferredForDuplicateResolution(_ slots: [MealSlot]) -> MealSlot {
slots.sorted { lhs, rhs in
let lhsHasDish = lhs.dishId != nil
let rhsHasDish = rhs.dishId != nil
if lhsHasDish != rhsHasDish { return lhsHasDish }
let lhsHasEvent = lhs.calendarEventId != nil
let rhsHasEvent = rhs.calendarEventId != nil
if lhsHasEvent != rhsHasEvent { return lhsHasEvent }
return lhs.id.uuidString < rhs.id.uuidString
}.first ?? slots[0]
}
}