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
66 lines
2.5 KiB
Swift
66 lines
2.5 KiB
Swift
import Foundation
|
|
import SwiftData
|
|
|
|
struct DefaultDataService {
|
|
|
|
static func createDefaultTags(context: ModelContext, saveImmediately: Bool = true) {
|
|
let defaults: [(name: String, nameEN: String, color: String, maxPerWeek: Int?, noConsecutive: Bool, noDuplicateInDay: Bool, mealTypeRestriction: String?, sortOrder: Int)] = [
|
|
("Carne", "Meat", "#E74C3C", 3, true, true, nil, 0),
|
|
("Pescado", "Fish", "#3498DB", 2, true, true, nil, 1),
|
|
("Legumbres", "Legumes", "#95A5A6", 2, false, true, nil, 2),
|
|
("Verduras", "Vegetables", "#2ECC71", nil, false, false, nil, 3),
|
|
("Huevos", "Eggs", "#F1C40F", 2, false, true, nil, 4),
|
|
("Pasta/Arroz", "Pasta/Rice", "#D4AC6E", 3, true, false, nil, 5),
|
|
("Solo Cena", "Dinner Only", "#9B59B6", nil, false, false, "dinner", 6),
|
|
("Solo Comida", "Lunch Only", "#E67E22", nil, false, false, "lunch", 7),
|
|
("Con Gluten", "Contains Gluten", "#8B4513", nil, false, false, nil, 8),
|
|
("Sin Gluten", "Gluten Free", "#1ABC9C", nil, false, false, nil, 9)
|
|
]
|
|
|
|
for d in defaults {
|
|
let tag = Tag(
|
|
name: d.name,
|
|
nameEN: d.nameEN,
|
|
color: d.color,
|
|
maxPerWeek: d.maxPerWeek,
|
|
noConsecutive: d.noConsecutive,
|
|
noDuplicateInDay: d.noDuplicateInDay,
|
|
mealTypeRestriction: d.mealTypeRestriction,
|
|
isDefault: true,
|
|
sortOrder: d.sortOrder
|
|
)
|
|
context.insert(tag)
|
|
}
|
|
|
|
if saveImmediately {
|
|
try? context.save()
|
|
}
|
|
}
|
|
|
|
static func createDefaultSettings(context: ModelContext) {
|
|
let settings = AppSettings()
|
|
context.insert(settings)
|
|
try? context.save()
|
|
}
|
|
|
|
static func createWeekPlan(for weekStart: Date, settings: AppSettings, context: ModelContext) -> WeekPlan {
|
|
let plan = WeekPlan(weekStartDate: weekStart)
|
|
context.insert(plan)
|
|
|
|
let maxDay = settings.includeWeekends ? 6 : 4
|
|
let mealTypes: [String] = settings.activeMealTypes.map(\.rawValue)
|
|
|
|
for day in 0...maxDay {
|
|
for mealType in mealTypes {
|
|
let slot = MealSlot(dayOfWeek: day, mealType: mealType)
|
|
slot.weekPlan = plan
|
|
plan.slotList.append(slot)
|
|
context.insert(slot)
|
|
}
|
|
}
|
|
|
|
try? context.save()
|
|
return plan
|
|
}
|
|
}
|