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

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

TodoReminderSink.swift86 lines · 4.7 KiBRaw
1// EverySync Hub — todos -> Reminders sink (no Todo app).
2//
3// `EKEventStore` + `requestAccess(to: .reminder)`; synced tasks live in a
4// dedicated hub-managed `EKCalendar` (or the default list) and appear in the
5// system Reminders app. Field mapping (verbatim from `Hub/README.md`):
6//
7// | `RemoteTodo` | `EKReminder` |
8// | `uid` | `calendarItemIdentifier` via the hub's syncId<->identifier map (identifiers are hub-local state, NOT IPC columns) |
9// | `title` | `title` |
10// | `notes` | `notes` (MUST persist — merge-hash input) |
11// | `done` | `completed` (+ `completionDate`) |
12// | `dueAt` (epoch ms, nullable) | `dueDateComponents` (via `Calendar`; nil clears) |
13// | `position` | not mapped — sort client-side (excluded from the merge hash, so reorder is never an edit) |
14// | `createdAt` | read-only reference (part of the adoption content key; never written back) |
15// | `deleted=true` envelope | remove the `EKReminder` |
16//
17// Tombstone mapping: `EKReminder` has no tombstone — a user-deleted reminder
18// simply vanishes from the store. The hub therefore persists the set of known
19// syncIds per vault (`SyncIdMap.swift`); after a HEALTHY fetch, any known
20// syncId with no matching `EKReminder` is a genuine local delete and folds
21// into the next merge as a tombstone envelope. Merge-driven deletes always
22// arrive as tombstone envelopes, never raw deletes. Same healthy-read
23// discipline as `SyncSink.listLocalOk`: an unhealthy fetch aborts the cycle
24// instead of reading absence as deletion.
25//
26// MAC-CHECK (needs xcodebuild on a Mac):
27// - `EKEventStore(requestAccessToEntityType:completion:)` vs iOS 17+
28// `requestFullAccessToReminders` — confirm the right call per deployment target.
29// - `EKReminder` due-date bridging (`dueDateComponents` <-> epoch ms) with
30// explicit `Calendar(identifier: .gregorian)` + UTC to avoid DST drift.
31
32import EventKit
33import EverysyncCore
34
35/// Applies XCFramework driver output for one vault's todos to Reminders.
36/// Driver output in, `EKReminder` rows out — no merge/hash/crypto here.
37final class TodoReminderSink {
38 private let eventStore: EKEventStore
39 private let syncIdMap: SyncIdMap
40 private let calendar = Calendar(identifier: .gregorian)
41
42 init(eventStore: EKEventStore, syncIdMap: SyncIdMap) {
43 self.eventStore = eventStore
44 self.syncIdMap = syncIdMap
45 }
46
47 // TODO(Mac): implement against the generated XCFramework `RemoteTodo` type:
48 //
49 // func apply(vaultId: String, envelopes: [RemoteTodo], targetCalendar: EKCalendar) throws:
50 // - for each envelope with deleted == true: look up the EKReminder via
51 // syncIdMap; `eventStore.remove(reminder, commit: false)`; drop the
52 // syncId from the known set (tombstone acknowledged).
53 // - else: fetch-or-create EKReminder in targetCalendar; set
54 // title/notes/completed+completionDate/dueDateComponents
55 // (dueAt ms -> DateComponents via `calendar.dateComponents(from: Date(...))`;
56 // nil clears); `position` ignored; `createdAt` never written back.
57 // Record syncId -> calendarItemIdentifier in syncIdMap.
58 // - commit once at the end (`eventStore.commit()`); a commit failure =
59 // unhealthy write — report up so the cycle does NOT advance cursors.
60 //
61 // func detectLocalDeletes(vaultId: String, targetCalendar: EKCalendar,
62 // knownSyncIds: Set<String>) throws -> Set<String>:
63 // - HEALTHY fetch of reminders in targetCalendar first; on ANY fetch
64 // failure THROW (caller aborts — never read absence as deletion).
65 // - return knownSyncIds minus fetched identifiers: genuine local
66 // deletes, which the driver folds into tombstone envelopes.
67 // - `notes` + `createdAt` are never compared here (they ride the
68 // envelope); only presence/absence after a healthy read matters.
69
70 /// Epoch-ms <-> DateComponents conversion. UTC Gregorian; nil clears.
71 func dueDateComponents(fromDueAt dueAt: Int64?) -> DateComponents? {
72 guard let dueAt else { return nil }
73 var utc = calendar
74 utc.timeZone = TimeZone(identifier: "UTC") ?? .current
75 return utc.dateComponents(
76 [.year, .month, .day, .hour, .minute, .second],
77 from: Date(timeIntervalSince1970: TimeInterval(dueAt) / 1000.0)
78 )
79 }
80
81 func dueAt(from components: DateComponents?) -> Int64? {
82 guard let components,
83 let date = calendar.date(from: components) else { return nil }
84 return Int64((date.timeIntervalSince1970 * 1000.0).rounded())
85 }
86}