Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / apple / Hub / HubSync.swift

HubSync.swift112 lines · 5.7 KiBRaw
1// EverySync Hub — Phase 3 sync-hub shell.
2//
3// ZERO sync logic in Swift. All merge (`TodoKind` / `ContactKind` /
4// `CalendarKind`), transport (`IosRelayTransport`), and crypto lives in the
5// shared XCFramework. This target:
6// - runs `SyncDriver` cycles on a schedule (BGTask — see
7// `../Shared/BackgroundSync.swift`),
8// - owns the App-Group envelope store (see `../IPC_CONTRACT.md`),
9// - sinks todos / contacts / calendar into the system stores
10// (Reminders via `EKReminder`, Contacts via `CNContactStore`, Calendar via
11// `EKEvent`).
12// There is NO Todo UI: tasks surface in the system Reminders app.
13//
14// Driver output in, system stores out — nothing else.
15
16import EventKit
17import Contacts
18import BackgroundTasks
19import EverysyncCore
20
21// MARK: - KMP entry points (Phase 3 wiring — call, never re-implement)
22//
23// data: core.data.RemoteTodo (uid / title / notes / done / dueAt epoch
24// ms nullable / position / createdAt / deleted / updatedAt)
25// core.data.RemoteContact (name parts + organization + note +
26// phones/emails/addresses TypedValue(value, type) + ISO-8601
27// birthday + etag/href/deleted/updatedAt)
28// core.data.RemoteEvent (calendarId / summary / description /
29// location / startMillis / endMillis / allDay / timezone /
30// rrule verbatim no-RRULE-prefix / recurrenceId / etag / href /
31// deleted / updatedAt)
32// kinds: core.sync.adapters.TodoKind (syncId=uid, LWW updatedAt,
33// canonical strips identity/position/timestamps — reorder is
34// not an edit; `contentKey = "todo␀" + title + "␀" + createdAt`;
35// soft-delete; `notes`+`createdAt` MUST persist — both are
36// merge-hash inputs, dropping them breaks LWW convergence)
37// core.sync.adapters.ContactKind (syncId=uid; canonical vCard
38// strips identity/transport metadata)
39// core.sync.adapters.CalendarKind (syncId = UID [+ ␀RECURRENCE-ID
40// for overridden instances]; canonical iCal keeps RECURRENCE-ID)
41// driver: core.provider.SyncDriver.drive(kind, store, remote, append, now)
42// — contract: null remote ABORTS (never merges); `remoteOut`
43// appends to the relay BEFORE persisting locally.
44// stores: RelayStore shims over the envelope IPC (see
45// `IPCRelayStore.swift` + `../XCFramework/IOS_ACTUALS_NOTES.md`)
46// cursors: core.platform.RelayCursorStore
47// securestore: core.platform.SecureStore (Keychain actual)
48// transport: core.network.IosRelayTransport
49// channels: recordChannel / membersChannel / keysChannel / ownerChannel /
50// titleChannel / inboxChannel / blobChannel (`notes-blobs:<vault>`)
51// roles: RelayRoles — passwords offer EDITOR only, never VIEWER
52// links: RelayLinks.parseJoinLink / .shareLinkFor
53// pqc: exactly 10 fns per IOS_PQC_NOTES.md (e2eePqc cinterop)
54
55/// Sync hub: runs XCFramework `SyncDriver` cycles, owns the App-Group envelope
56/// store, and sinks todos/contacts/calendar into the system stores.
57/// Scheduling + mapping only.
58final class HubSync {
59 let eventStore = EKEventStore()
60 let contactStore = CNContactStore()
61 private let ipc = EnvelopeIPC()
62 private let syncIdMap = SyncIdMap()
63
64 init() {
65 // MAC-CHECK: identifiers must match Info.plist
66 // `BGTaskSchedulerPermittedIdentifiers`.
67 BackgroundSync.register(handlers: [
68 "com.vayunmathur.everysync.hub.refresh": { task in
69 // TODO(Mac): run SyncDriver cycles for every vault (todos +
70 // contacts + calendar + hub-side passwords/notes relay ops),
71 // then re-schedule + `task.setTaskCompleted(success:)`.
72 task.setTaskCompleted(success: true)
73 },
74 "com.vayunmathur.everysync.hub.upload": { task in
75 // TODO(Mac): one-shot after local system-store changes
76 // (EKEventStore notifications); push deltas, then complete.
77 task.setTaskCompleted(success: true)
78 },
79 ])
80 }
81
82 /// Requests Reminders / Calendar / Contacts access, then starts cycles.
83 /// Each grant is independent: a denied store only disables its sink —
84 /// the hub still syncs the other types (never abort everything on one
85 /// denial; each sink tracks its own `listLocalOk`-equivalent health).
86 func requestSystemAccess() {
87 // TODO(Mac): `eventStore.requestAccess(to: .reminder)`,
88 // `eventStore.requestAccess(to: .event)` (iOS 17+: `requestFullAccess-`
89 // variant — confirm on the Mac), `contactStore.requestAccess(for:
90 // .contacts)`, then start SyncDriver cycles per granted store.
91 }
92
93 /// Drives one todos cycle for a vault: pull `todos:<vaultId>` via the
94 /// XCFramework, apply through `TodoReminderSink`, persist known syncIds.
95 /// See `TodoReminderSink.swift` for the field + tombstone mapping.
96 func syncTodosToReminders(vaultId: String) {
97 // TODO(Mac): call into the XCFramework SyncDriver with the
98 // `IPCRelayStore<RemoteTodo>` shim + `TodoReminderSink` application.
99 }
100
101 /// Drives one contacts cycle for a vault. SyncId<->identifier map stays
102 /// hub-local (see `SyncIdMap.swift`); Swift never scribbles syncIds into
103 /// user-visible contact fields.
104 func syncContacts(vaultId: String) {
105 // TODO(Mac): SyncDriver cycle with the contacts IPC shim + ContactSink.
106 }
107
108 /// Drives one calendar cycle for a vault into the hub-managed calendar.
109 func syncCalendarEvents(vaultId: String) {
110 // TODO(Mac): SyncDriver cycle with the calendar IPC shim + CalendarSink.
111 }
112}