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

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

ContactSink.swift101 lines · 4.4 KiBRaw
1// EverySync Hub — contacts -> `CNContactStore` sink.
2//
3// `CNContactStore` + `requestAccess(for: .contacts)`. Maps `RemoteContact`
4// name parts / organization / note / typed phones-emails-addresses / birthday
5// onto `CNMutableContact`; syncId<->`CNContact.identifier` lives in the hub
6// map (`SyncIdMap.swift`) — Swift never scribbles syncIds into user-visible
7// contact fields. Delete and conflict semantics follow the XCFramework entity
8// kind (`ContactKind`: soft-delete; canonical vCard strips identity/transport
9// metadata) — Swift holds no opinion beyond faithfully applying `SyncDriver`
10// output.
11//
12// Type mapping (shared `PhoneKind`/`EmailKind`/`PostalKind` ints from
13// `core.format.VCard` — values mirror `ContactsContract`, passed through
14// unchanged):
15// - Phone: HOME=1 -> home, MOBILE=2 -> mobile, WORK=3 -> work, FAX_HOME=4 /
16// FAX_WORK=5 -> fax, OTHER=7 -> other.
17// - Email: HOME=1 -> home, WORK=2 -> work, OTHER=3 -> other.
18// - Postal: HOME=1 -> home, WORK=2 -> work, OTHER=3 -> other.
19// - Birthday: ISO-8601 `yyyy-MM-dd` <-> `DateComponents(calendar, y/m/d)`.
20// A malformed birthday does NOT fail the contact — the field is skipped and
21// the rest applies (foreign/corrupt tolerance, same as `parseRemote` null).
22//
23// MAC-CHECK (needs xcodebuild on a Mac):
24// - `CNContactStore.requestAccess(for:completionHandler:)` deprecation
25// status on the deployment target (iOS 18 `requestAccess(for:)` async
26// variant — confirm on the Mac).
27// - `CNSaveRequest` batching + `contactStore.execute(_:)` error mapping to
28// healthy/unhealthy write reporting (a failed save must NOT advance cursors).
29
30import Contacts
31import EverysyncCore
32
33/// Applies XCFramework driver output for one vault's contacts.
34/// Driver output in, `CNContact` rows out — nothing else.
35final class ContactSink {
36 private let contactStore: CNContactStore
37 private let syncIdMap: SyncIdMap
38
39 init(contactStore: CNContactStore, syncIdMap: SyncIdMap) {
40 self.contactStore = contactStore
41 self.syncIdMap = syncIdMap
42 }
43
44 // TODO(Mac): implement against the generated XCFramework `RemoteContact` /
45 // `TypedValue` types:
46 //
47 // func apply(vaultId: String, envelopes: [RemoteContact]) throws:
48 // - deleted == true -> delete the mapped CNContact (via CNSaveRequest
49 // delete after fetch-by-identifier); drop the syncId from the map.
50 // - else fetch-or-create CNMutableContact; set given/middle/family/
51 // prefix/suffix/organization/note; phones -> CNLabeledValue with
52 // CNLabelPhoneNumber* per the table above; emails/postal likewise;
53 // birthday yyyy-MM-dd -> DateComponents (skip field on parse failure).
54 // - batch in ONE CNSaveRequest; execute once. Failure = unhealthy write.
55 //
56 // func detectLocalDeletes(knownSyncIds: Set<String>) throws -> Set<String>:
57 // - HEALTHY fetch (keys: identifier only) first; throw on ANY failure
58 // (caller aborts — same healthy-read discipline as listLocalOk).
59 // - known minus fetched = genuine local deletes -> tombstone envelopes.
60
61 /// Phone type int -> Contacts label. Mirrors `VCard` codes.
62 static func phoneLabel(for type: Int) -> String {
63 switch type {
64 case 1: return CNLabelHome
65 case 2: return CNLabelPhoneNumberMobile
66 case 3: return CNLabelWork
67 case 4, 5: return CNLabelPhoneNumberMainFax
68 default: return CNLabelOther
69 }
70 }
71
72 static func emailLabel(for type: Int) -> String {
73 switch type {
74 case 1: return CNLabelHome
75 case 2: return CNLabelWork
76 default: return CNLabelOther
77 }
78 }
79
80 static func postalLabel(for type: Int) -> String {
81 switch type {
82 case 1: return CNLabelHome
83 case 2: return CNLabelWork
84 default: return CNLabelOther
85 }
86 }
87
88 /// ISO-8601 `yyyy-MM-dd` -> DateComponents. Nil on malformed input
89 /// (caller skips the field, never fails the contact).
90 static func birthdayComponents(from iso: String?) -> DateComponents? {
91 guard let iso, !iso.isEmpty else { return nil }
92 let parts = iso.split(separator: "-").compactMap { Int($0) }
93 guard parts.count == 3 else { return nil }
94 var components = DateComponents()
95 components.calendar = Calendar(identifier: .gregorian)
96 components.year = parts[0]
97 components.month = parts[1]
98 components.day = parts[2]
99 return components
100 }
101}