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
73 lines
2.4 KiB
Swift
73 lines
2.4 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
import FirebaseCore
|
|
import FirebaseCrashlytics
|
|
#if canImport(GoogleMobileAds)
|
|
import GoogleMobileAds
|
|
#endif
|
|
|
|
/// Whether the store is syncing through CloudKit this launch. When true, the
|
|
/// legacy iCloud KV snapshot sync must stay inert (both would fight).
|
|
enum CloudSyncRuntime {
|
|
// Written exactly once during app start (before any concurrency), then
|
|
// read-only for the rest of the launch.
|
|
nonisolated(unsafe) static var isCloudKitActive = false
|
|
}
|
|
|
|
@main
|
|
struct MealMoodApp: App {
|
|
private let modelContainer: ModelContainer = {
|
|
let schema = Schema([
|
|
AppSettings.self,
|
|
Tag.self,
|
|
Dish.self,
|
|
WeekPlan.self,
|
|
MealSlot.self,
|
|
ShoppingItem.self
|
|
])
|
|
|
|
// 2.0: CloudKit private-database sync (multi-device, same Apple ID).
|
|
// Falls back to the local-only store when CloudKit isn't available
|
|
// (signed-out iCloud, missing entitlement in dev builds, etc.).
|
|
let cloudConfiguration = ModelConfiguration(cloudKitDatabase: .automatic)
|
|
do {
|
|
let container = try ModelContainer(for: schema, configurations: [cloudConfiguration])
|
|
CloudSyncRuntime.isCloudKitActive = true
|
|
return container
|
|
} catch {
|
|
CrashlyticsService.record(error, context: "cloudkit_container")
|
|
print("CloudKit container unavailable, using local store: \(error)")
|
|
}
|
|
|
|
let configuration = ModelConfiguration(cloudKitDatabase: .none)
|
|
do {
|
|
return try ModelContainer(for: schema, configurations: [configuration])
|
|
} catch {
|
|
// Migration failed — reset the store to avoid a crash loop
|
|
let storeURL = configuration.url
|
|
for ext in ["", "-shm", "-wal"] {
|
|
try? FileManager.default.removeItem(at: URL(fileURLWithPath: storeURL.path + ext))
|
|
}
|
|
do {
|
|
return try ModelContainer(for: schema, configurations: [configuration])
|
|
} catch {
|
|
fatalError("Cannot create ModelContainer after reset: \(error)")
|
|
}
|
|
}
|
|
}()
|
|
|
|
init() {
|
|
FirebaseApp.configure()
|
|
#if canImport(GoogleMobileAds)
|
|
GADMobileAds.sharedInstance().start(completionHandler: nil)
|
|
#endif
|
|
}
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
ContentView()
|
|
}
|
|
.modelContainer(modelContainer)
|
|
}
|
|
}
|