163fd6026a
CloudKit sincroniza la base privada de un Apple ID: ni llega a Android ni deja que dos cuentas editen el mismo plan (CKShare sigue sin existir en SwiftData). El contenido de un hogar pasa por tanto a Firestore, y un dispositivo que entra en un hogar construye el store local sin CloudKit — dos espejos escribiendo los mismos objetos se pelean, que es justo lo que ya obligó a apagar el sync por iCloud KV. SwiftData sigue siendo el store local y el modo offline; HouseholdSyncService es lo unico que habla con la red. Detecta cambios comparando una huella del contenido de cada documento con la ultima sincronizada (el "shadow"), asi que no hace falta instrumentar con updatedAt las treinta vistas que mutan modelos. Los borrados van como tombstone: un borrado duro volveria desde cualquier miembro que estuviera sin conexion. Semanas y slots usan id derivado del contenido (2026-09-14, 5-dinner) para que dos miembros que abren la misma semana escriban el mismo documento en vez de crear dos, y para que los conflictos se resuelvan por slot y no por semana. Incluye reglas de seguridad (solo miembros; los codigos de invitacion se pueden leer por id pero no listar), pantalla de hogar en Ajustes con Sign in with Apple, invitacion por codigo de 6 caracteres sin vocales ni 0/O/1/I, y la eleccion al unirse entre llevarse los platos propios o adoptar los del hogar. Fuera de esta fase: fotos de platos (necesitan Storage) y el cliente Android. Refs #33 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013su1ttRiMeMYxkZJ1Y3246
41 lines
1.6 KiB
Swift
41 lines
1.6 KiB
Swift
import Foundation
|
|
|
|
/// Where the app's data lives this launch.
|
|
///
|
|
/// A device inside a household syncs through Firestore and must NOT also run
|
|
/// CloudKit: two mirrors writing the same objects fight each other (the same
|
|
/// reason the legacy iCloud KV sync is disabled whenever CloudKit is active).
|
|
/// The store configuration is fixed when the `ModelContainer` is built at
|
|
/// launch, so joining or leaving a household only takes effect after a relaunch
|
|
/// — `needsRelaunch` tells the UI when to say so.
|
|
enum HouseholdRuntime {
|
|
private static let householdIdKey = "household_id"
|
|
private static let householdNameKey = "household_name"
|
|
|
|
/// Household this device belongs to, or nil when planning solo.
|
|
static var householdId: String? {
|
|
get { UserDefaults.standard.string(forKey: householdIdKey) }
|
|
set { UserDefaults.standard.set(newValue, forKey: householdIdKey) }
|
|
}
|
|
|
|
static var householdName: String? {
|
|
get { UserDefaults.standard.string(forKey: householdNameKey) }
|
|
set { UserDefaults.standard.set(newValue, forKey: householdNameKey) }
|
|
}
|
|
|
|
/// Whether the store built at launch is the household (local + Firestore)
|
|
/// one. Written once during app start, read-only afterwards.
|
|
nonisolated(unsafe) static var isHouseholdStore = false
|
|
|
|
/// True when the membership changed since the store was built, so the app
|
|
/// is running on the wrong store until it relaunches.
|
|
static var needsRelaunch: Bool {
|
|
(householdId != nil) != isHouseholdStore
|
|
}
|
|
|
|
static func clear() {
|
|
householdId = nil
|
|
householdName = nil
|
|
}
|
|
}
|