@octane-xplat/contacts 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -25
- package/package.json +87 -94
- package/src/contacts.macos.ts +377 -13
- package/src/contacts.ts +235 -201
- package/src/contacts.web.ts +41 -33
- package/src/contacts.windows.ts +12 -9
- package/src/types.ts +116 -66
- package/types/generated/.tsrx-typegen-manifest.json +0 -6
- package/types/generated/contacts.d.ts +1 -0
- package/types/generated/types.d.ts +50 -0
- package/LICENSE +0 -21
- package/types/generated/contacts.linux.d.ts +0 -1
- package/types/generated/contacts.macos.d.ts +0 -2
- package/types/generated/contacts.web.d.ts +0 -2
- package/types/generated/contacts.windows.d.ts +0 -2
- package/types/generated/index.macos.d.ts +0 -2
- package/types/generated/index.web.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,27 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @octane-xplat/contacts
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
pnpm add @octane-xplat/contacts
|
|
5
|
-
```
|
|
3
|
+
[Documentation](https://octane-xplat.goddardai.org/package-contacts).
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
`@nativescript/contacts` on iOS/Android, the Contact Picker API
|
|
9
|
-
(`navigator.contacts.select`) on web where it exists.
|
|
10
|
-
|
|
11
|
-
```ts
|
|
12
|
-
import { contacts } from '@octane-xplat/contacts'
|
|
13
|
-
|
|
14
|
-
if ((await contacts.ensure()) === 'granted') {
|
|
15
|
-
const people = await contacts.getAll({ fields: ['name', 'phoneNumbers'], sort: 'lastName' })
|
|
16
|
-
const jane = await contacts.getByName('Jane')
|
|
17
|
-
await contacts.add({ firstName: 'Ada', emails: [{ label: 'work', email: 'ada@example.com' }] })
|
|
18
|
-
}
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Notes:
|
|
22
|
-
|
|
23
|
-
- `getById` is a direct lookup on iOS; on Android the plugin ships no id
|
|
24
|
-
query, so it fetches the store and filters — call sparingly.
|
|
25
|
-
- Web is read-only and picker-shaped: the user selects contacts per call;
|
|
26
|
-
writes and id/name lookups report `unsupported`.
|
|
27
|
-
- macOS and Windows report `supported: false` (bridging not wired).
|
|
5
|
+
[API reference](https://octane-xplat.goddardai.org/api-contacts).
|
package/package.json
CHANGED
|
@@ -1,95 +1,88 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
},
|
|
90
|
-
"scripts": {
|
|
91
|
-
"typegen": "tsrx-typegen --target octane",
|
|
92
|
-
"pack:check": "tsrx-typegen --pack-check",
|
|
93
|
-
"typecheck": "node ../../scripts/typecheck.ts"
|
|
94
|
-
}
|
|
95
|
-
}
|
|
2
|
+
"name": "@octane-xplat/contacts",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Device address book for Octane xplat — contact reads and writes on iOS/Android, Contact Picker API on web",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"cross-platform",
|
|
8
|
+
"nativescript",
|
|
9
|
+
"octane",
|
|
10
|
+
"xplat",
|
|
11
|
+
"contacts",
|
|
12
|
+
"address-book"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/contacts",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
19
|
+
"directory": "packages/contacts"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"platforms",
|
|
24
|
+
"types",
|
|
25
|
+
"!src/**/*.test.*",
|
|
26
|
+
"!src/**/*.spec.*"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./types/generated/index.d.ts",
|
|
32
|
+
"macos": "./src/index.macos.ts",
|
|
33
|
+
"web": "./src/index.web.ts",
|
|
34
|
+
"native": "./src/index.ts",
|
|
35
|
+
"default": "./src/index.ts"
|
|
36
|
+
},
|
|
37
|
+
"./*": {
|
|
38
|
+
"types": "./types/generated/*.d.ts",
|
|
39
|
+
"default": "./src/*"
|
|
40
|
+
},
|
|
41
|
+
"./*.ts": {
|
|
42
|
+
"types": "./types/generated/*.d.ts",
|
|
43
|
+
"default": "./src/*.ts"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"typegen": "tsrx-typegen --target octane",
|
|
51
|
+
"prepack": "bun run typegen && tsrx-typegen --pack-check",
|
|
52
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
53
|
+
"typecheck": "node ../../scripts/typecheck.ts",
|
|
54
|
+
"test": "node --test tests/*.test.ts"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@nativescript-community/octane": "0.2.5",
|
|
58
|
+
"@nativescript/core": "9.1.3",
|
|
59
|
+
"@nativescript/types": "9.1.4",
|
|
60
|
+
"octane": "0.16.3",
|
|
61
|
+
"tsrx-typegen": "0.6.1",
|
|
62
|
+
"typescript": "7.1.0-beta",
|
|
63
|
+
"@tsrx/content-mapper": "0.1.2"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
67
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
68
|
+
"octane": ">=0.6.3 <1"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"@nativescript-community/octane": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
"@nativescript/core": {
|
|
75
|
+
"optional": true
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"nativescript": {
|
|
79
|
+
"platforms": {
|
|
80
|
+
"android": "9.1.0",
|
|
81
|
+
"ios": "9.1.0"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"dependencies": {
|
|
85
|
+
"@nativescript/contacts": "3.0.0",
|
|
86
|
+
"@octane-xplat/platform": "0.20.0"
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/contacts.macos.ts
CHANGED
|
@@ -1,19 +1,383 @@
|
|
|
1
|
-
// Contacts —
|
|
2
|
-
//
|
|
3
|
-
|
|
1
|
+
// Contacts — AppKit leaf over CNContactStore. The Contacts framework is
|
|
2
|
+
// bridgeable through the JSC host; semantics mirror the iOS leaf.
|
|
3
|
+
// CNContactNoteKey requires a contacts-notes entitlement even on macOS —
|
|
4
|
+
// it is excluded from fetch keys and `note` stays absent unless the store
|
|
5
|
+
// reports the key available.
|
|
6
|
+
import {
|
|
7
|
+
CNContact,
|
|
8
|
+
CNContactFetchRequest,
|
|
9
|
+
CNContactIdentifierKey,
|
|
10
|
+
CNContactEmailAddressesKey,
|
|
11
|
+
CNContactFamilyNameKey,
|
|
12
|
+
CNContactGivenNameKey,
|
|
13
|
+
CNContactImageDataKey,
|
|
14
|
+
CNContactJobTitleKey,
|
|
15
|
+
CNContactMiddleNameKey,
|
|
16
|
+
CNContactNicknameKey,
|
|
17
|
+
CNContactNoteKey,
|
|
18
|
+
CNContactOrganizationNameKey,
|
|
19
|
+
CNContactPhoneNumbersKey,
|
|
20
|
+
CNContactPostalAddressesKey,
|
|
21
|
+
CNContactStore,
|
|
22
|
+
CNContactUrlAddressesKey,
|
|
23
|
+
CNEntityType,
|
|
24
|
+
CNLabeledValue,
|
|
25
|
+
CNMutableContact,
|
|
26
|
+
CNPhoneNumber,
|
|
27
|
+
CNSaveRequest,
|
|
28
|
+
} from '@octane-xplat/platform/macos'
|
|
4
29
|
|
|
5
|
-
|
|
30
|
+
import type {
|
|
31
|
+
Contact,
|
|
32
|
+
ContactAddress,
|
|
33
|
+
ContactField,
|
|
34
|
+
Contacts,
|
|
35
|
+
GetContactsOptions,
|
|
36
|
+
NewContact,
|
|
37
|
+
PermissionResult,
|
|
38
|
+
} from './types'
|
|
6
39
|
|
|
40
|
+
let store: any = null
|
|
41
|
+
const contactStore = () => {
|
|
42
|
+
if (store) {
|
|
43
|
+
return store
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
if (typeof CNContactStore?.authorizationStatusForEntityType !== 'function') {
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (store = CNContactStore.new())
|
|
52
|
+
} catch {
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// CNAuthorizationStatus: 0 notDetermined 1 restricted 2 denied 3 authorized
|
|
58
|
+
const rawStatus = () =>
|
|
59
|
+
Number(CNContactStore.authorizationStatusForEntityType(CNEntityType.CNEntityTypeContacts ?? 0))
|
|
60
|
+
|
|
61
|
+
function ensureAccess(): Promise<PermissionResult> {
|
|
62
|
+
const s = contactStore()
|
|
63
|
+
|
|
64
|
+
if (!s) {
|
|
65
|
+
return Promise.resolve('unsupported')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const status = rawStatus()
|
|
69
|
+
|
|
70
|
+
if (status === 3) {
|
|
71
|
+
return Promise.resolve('granted')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (status === 1 || status === 2) {
|
|
75
|
+
return Promise.resolve('denied')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return new Promise((resolve) => {
|
|
79
|
+
s.requestAccessForEntityTypeCompletionHandler(
|
|
80
|
+
CNEntityType.CNEntityTypeContacts ?? 0,
|
|
81
|
+
(granted: boolean) => resolve(granted ? 'granted' : 'denied'),
|
|
82
|
+
)
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const BASE_KEYS = [
|
|
87
|
+
CNContactIdentifierKey,
|
|
88
|
+
CNContactGivenNameKey,
|
|
89
|
+
CNContactMiddleNameKey,
|
|
90
|
+
CNContactFamilyNameKey,
|
|
91
|
+
CNContactNicknameKey,
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
const FIELD_KEYS: Record<Exclude<ContactField, 'name'>, string[]> = {
|
|
95
|
+
emails: [CNContactEmailAddressesKey],
|
|
96
|
+
phoneNumbers: [CNContactPhoneNumbersKey],
|
|
97
|
+
addresses: [CNContactPostalAddressesKey],
|
|
98
|
+
urls: [CNContactUrlAddressesKey],
|
|
99
|
+
company: [CNContactOrganizationNameKey, CNContactJobTitleKey],
|
|
100
|
+
note: [], // entitlement-gated on macOS too — read only when the key is available
|
|
101
|
+
image: [CNContactImageDataKey],
|
|
102
|
+
nickname: [],
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function fetchKeys(fields?: ContactField[]): string[] {
|
|
106
|
+
const keys = [...BASE_KEYS]
|
|
107
|
+
|
|
108
|
+
for (const field of fields ?? (Object.keys(FIELD_KEYS) as ContactField[])) {
|
|
109
|
+
for (const key of FIELD_KEYS[field as keyof typeof FIELD_KEYS] ?? []) {
|
|
110
|
+
if (!keys.includes(key)) {
|
|
111
|
+
keys.push(key)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return keys
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const nsarray = (list: any): any[] => {
|
|
120
|
+
if (!list) {
|
|
121
|
+
return []
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (Array.isArray(list)) {
|
|
125
|
+
return list
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const out: any[] = []
|
|
129
|
+
|
|
130
|
+
for (let i = 0, count = Number(list.count ?? 0); i < count; i++) {
|
|
131
|
+
out.push(list.objectAtIndex(i))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const labeled = (list: any, value: (entry: any) => unknown) =>
|
|
138
|
+
nsarray(list).map((entry) => ({
|
|
139
|
+
label: entry?.label ? String(entry.label) : undefined,
|
|
140
|
+
value: value(entry),
|
|
141
|
+
}))
|
|
142
|
+
|
|
143
|
+
function asContact(native: any): Contact {
|
|
144
|
+
const firstName = native?.givenName ? String(native.givenName) : undefined
|
|
145
|
+
const lastName = native?.familyName ? String(native.familyName) : undefined
|
|
146
|
+
const name =
|
|
147
|
+
native?.name && typeof native.name === 'string'
|
|
148
|
+
? native.name
|
|
149
|
+
: [firstName, lastName].filter(Boolean).join(' ') ||
|
|
150
|
+
(native?.organizationName ? String(native.organizationName) : '')
|
|
151
|
+
|
|
152
|
+
const contact: Contact = {
|
|
153
|
+
id: String(native?.identifier ?? ''),
|
|
154
|
+
name,
|
|
155
|
+
firstName,
|
|
156
|
+
middleName: native?.middleName ? String(native.middleName) : undefined,
|
|
157
|
+
lastName,
|
|
158
|
+
nickname: native?.nickname ? String(native.nickname) : undefined,
|
|
159
|
+
emails: labeled(native?.emailAddresses, (e) => String(e?.value ?? '')).map(
|
|
160
|
+
({ label, value }) => ({ label, email: value as string }),
|
|
161
|
+
),
|
|
162
|
+
phoneNumbers: labeled(native?.phoneNumbers, (e) => String(e?.value?.stringValue ?? '')).map(
|
|
163
|
+
({ label, value }) => ({ label, number: value as string }),
|
|
164
|
+
),
|
|
165
|
+
addresses: labeled(native?.postalAddresses, (e): ContactAddress => ({
|
|
166
|
+
street: e?.value?.street ? String(e.value.street) : undefined,
|
|
167
|
+
city: e?.value?.city ? String(e.value.city) : undefined,
|
|
168
|
+
region: e?.value?.state ? String(e.value.state) : undefined,
|
|
169
|
+
postalCode: e?.value?.postalCode ? String(e.value.postalCode) : undefined,
|
|
170
|
+
country: e?.value?.country ? String(e.value.country) : undefined,
|
|
171
|
+
})).map(({ label, value }) => ({ label, ...(value as ContactAddress) })),
|
|
172
|
+
urls: labeled(native?.urlAddresses, (e) => String(e?.value ?? '')).map(({ label, value }) => ({
|
|
173
|
+
label,
|
|
174
|
+
url: value as string,
|
|
175
|
+
})),
|
|
176
|
+
company: native?.organizationName ? String(native.organizationName) : undefined,
|
|
177
|
+
jobTitle: native?.jobTitle ? String(native.jobTitle) : undefined,
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// The notes entitlement is a distribution choice; read it only when the
|
|
181
|
+
// fetched keys already carry it.
|
|
182
|
+
try {
|
|
183
|
+
if (native?.isKeyAvailable?.(CNContactNoteKey) && native?.note) {
|
|
184
|
+
contact.note = String(native.note)
|
|
185
|
+
}
|
|
186
|
+
} catch {
|
|
187
|
+
// A store without the notes entitlement leaves the field absent.
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
if (native?.imageDataAvailable) {
|
|
192
|
+
const data = native?.imageData
|
|
193
|
+
const base64 = data?.base64EncodedStringWithOptions?.(0)
|
|
194
|
+
|
|
195
|
+
if (base64) {
|
|
196
|
+
contact.image = { data: String(base64) }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} catch {
|
|
200
|
+
// Unfetched or unreadable image data is omitted, never fatal.
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return contact
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function applySort(list: Contact[], sort?: GetContactsOptions['sort']): Contact[] {
|
|
207
|
+
if (!sort || sort === 'none') {
|
|
208
|
+
return list
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const key = sort === 'firstName' ? 'firstName' : 'lastName'
|
|
212
|
+
|
|
213
|
+
return [...list].sort((a, b) => (a[key] ?? a.name).localeCompare(b[key] ?? b.name))
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function applyPage(list: Contact[], options?: GetContactsOptions): Contact[] {
|
|
217
|
+
const start = options?.pageOffset ?? 0
|
|
218
|
+
const size = options?.pageSize
|
|
219
|
+
|
|
220
|
+
return size === undefined ? list.slice(start) : list.slice(start, start + size)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function requireStore(): any {
|
|
224
|
+
const s = contactStore()
|
|
225
|
+
|
|
226
|
+
if (!s) {
|
|
227
|
+
throw new Error('contacts: the Contacts store is unavailable on this host')
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return s
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function applyNewContact(target: any, source: NewContact): void {
|
|
234
|
+
target.givenName = source.firstName ?? ''
|
|
235
|
+
target.middleName = source.middleName ?? ''
|
|
236
|
+
target.familyName = source.lastName ?? ''
|
|
237
|
+
target.nickname = source.nickname ?? ''
|
|
238
|
+
target.organizationName = source.company ?? ''
|
|
239
|
+
target.jobTitle = source.jobTitle ?? ''
|
|
240
|
+
target.emailAddresses = (source.emails ?? []).map((e) =>
|
|
241
|
+
CNLabeledValue.labeledValueWithLabelValue(e.label ?? null, e.email),
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
target.phoneNumbers = (source.phoneNumbers ?? []).map((p) =>
|
|
245
|
+
CNLabeledValue.labeledValueWithLabelValue(
|
|
246
|
+
p.label ?? null,
|
|
247
|
+
CNPhoneNumber.phoneNumberWithStringValue(p.number),
|
|
248
|
+
),
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Contacts service — system address book. */
|
|
7
253
|
export const contacts: Contacts = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
254
|
+
get supported() {
|
|
255
|
+
return contactStore() !== null
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
ensure(): Promise<PermissionResult> {
|
|
259
|
+
return ensureAccess()
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
async getAll(options?: GetContactsOptions): Promise<Contact[]> {
|
|
263
|
+
const s = requireStore()
|
|
264
|
+
const request = CNContactFetchRequest.alloc().initWithKeysToFetch(fetchKeys(options?.fields))
|
|
265
|
+
const out: Contact[] = []
|
|
266
|
+
|
|
267
|
+
if (
|
|
268
|
+
!s.enumerateContactsWithFetchRequestErrorUsingBlock(request, null, (contact: any) => {
|
|
269
|
+
out.push(asContact(contact))
|
|
270
|
+
})
|
|
271
|
+
) {
|
|
272
|
+
throw new Error('contacts.getAll: the store refused the fetch request')
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return applyPage(applySort(out, options?.sort), options)
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
async getByName(predicate: string, fields?: ContactField[]): Promise<Contact[]> {
|
|
279
|
+
const s = requireStore()
|
|
280
|
+
const list = s.unifiedContactsMatchingPredicateKeysToFetchError(
|
|
281
|
+
CNContact.predicateForContactsMatchingName(predicate),
|
|
282
|
+
fetchKeys(fields),
|
|
283
|
+
null,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
return nsarray(list).map(asContact)
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
async getById(id: string, fields?: ContactField[]): Promise<Contact | null> {
|
|
290
|
+
const s = requireStore()
|
|
291
|
+
const native = s.unifiedContactWithIdentifierKeysToFetchError(id, fetchKeys(fields), null)
|
|
292
|
+
|
|
293
|
+
return native ? asContact(native) : null
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
async add(contact: NewContact): Promise<string> {
|
|
297
|
+
const s = requireStore()
|
|
298
|
+
const native = CNMutableContact.new()
|
|
299
|
+
|
|
300
|
+
applyNewContact(native, contact)
|
|
301
|
+
|
|
302
|
+
const request = CNSaveRequest.new()
|
|
303
|
+
|
|
304
|
+
request.addContactToContainerWithIdentifier(native, null)
|
|
305
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
306
|
+
throw new Error('contacts.add: the store refused the save request')
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return String(native.identifier ?? '')
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
async update(id: string, contact: NewContact): Promise<string> {
|
|
313
|
+
const s = requireStore()
|
|
314
|
+
const existing = s.unifiedContactWithIdentifierKeysToFetchError(id, fetchKeys(), null)
|
|
315
|
+
|
|
316
|
+
if (!existing) {
|
|
317
|
+
throw new Error(`contacts.update: no contact with id ${id}`)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const native = existing.mutableCopy()
|
|
321
|
+
|
|
322
|
+
applyNewContact(native, contact)
|
|
323
|
+
|
|
324
|
+
const request = CNSaveRequest.new()
|
|
325
|
+
|
|
326
|
+
request.updateContact(native)
|
|
327
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
328
|
+
throw new Error('contacts.update: the store refused the save request')
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return id
|
|
332
|
+
},
|
|
333
|
+
|
|
334
|
+
async remove(id: string): Promise<void> {
|
|
335
|
+
const s = requireStore()
|
|
336
|
+
const existing = s.unifiedContactWithIdentifierKeysToFetchError(
|
|
337
|
+
id,
|
|
338
|
+
[CNContactIdentifierKey],
|
|
339
|
+
null,
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
if (!existing) {
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const request = CNSaveRequest.new()
|
|
347
|
+
|
|
348
|
+
request.deleteContact(existing.mutableCopy())
|
|
349
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
350
|
+
throw new Error('contacts.remove: the store refused the save request')
|
|
351
|
+
}
|
|
352
|
+
},
|
|
16
353
|
}
|
|
17
354
|
|
|
18
355
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
19
|
-
|
|
356
|
+
|
|
357
|
+
permissionOwners.contacts = {
|
|
358
|
+
getPermissions: async () => {
|
|
359
|
+
const s = contactStore()
|
|
360
|
+
|
|
361
|
+
if (!s) {
|
|
362
|
+
return { status: 'unsupported' as const, granted: false, canAskAgain: false }
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const status = rawStatus()
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
status:
|
|
369
|
+
status === 3
|
|
370
|
+
? ('granted' as const)
|
|
371
|
+
: status === 0
|
|
372
|
+
? ('undetermined' as const)
|
|
373
|
+
: ('denied' as const),
|
|
374
|
+
granted: status === 3,
|
|
375
|
+
canAskAgain: status === 0 || status === 3,
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
requestPermissions: async () => {
|
|
379
|
+
const result = await contacts.ensure()
|
|
380
|
+
|
|
381
|
+
return { status: result, granted: result === 'granted', canAskAgain: result === 'granted' }
|
|
382
|
+
},
|
|
383
|
+
}
|