-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
595 lines (525 loc) · 15.7 KB
/
index.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* I'd prefer this file to be called Contacts.ts, as in the expo-contacts package itself, but the nx workspace
* seems to override my "main" field to "index.js" and my "typings" field to "index.d.ts" upon build.
* @see https://github.com/expo/expo/blob/master/packages/expo-contacts/src/Contacts.ts
*/
import { UnavailabilityError } from "@nativescript-community/expo-nativescript-adapter";
import { isIOS } from "@nativescript/core";
import { PermissionResponse, PermissionStatus } from 'unimodules-permissions-interface';
// import uuidv4 from 'uuid/v4';
import ExpoContacts from './ExpoContacts';
// TODO: implement.
const Share = {
share(
args: { url: string, message: string },
shareOptions: object = {},
): Promise<any> {
throw new UnavailabilityError('Contacts', 'shareContactAsync');
}
};
export type CalendarFormatType =
| typeof CalendarFormats.Gregorian
| typeof CalendarFormats.Buddhist
| typeof CalendarFormats.Chinese
| typeof CalendarFormats.Coptic
| typeof CalendarFormats.EthiopicAmeteMihret
| typeof CalendarFormats.EthiopicAmeteAlem
| typeof CalendarFormats.Hebrew
| typeof CalendarFormats.ISO8601
| typeof CalendarFormats.Indian
| typeof CalendarFormats.Islamic
| typeof CalendarFormats.IslamicCivil
| typeof CalendarFormats.Japanese
| typeof CalendarFormats.Persian
| typeof CalendarFormats.RepublicOfChina
| typeof CalendarFormats.IslamicTabular
| typeof CalendarFormats.IslamicUmmAlQura;
export type ContainerType =
| typeof ContainerTypes.Local
| typeof ContainerTypes.Exchange
| typeof ContainerTypes.CardDAV
| typeof ContainerTypes.Unassigned;
export type ContactType = typeof ContactTypes.Person | typeof ContactTypes.Company;
export type FieldType =
| typeof Fields.ID
| typeof Fields.ContactType
| typeof Fields.Name
| typeof Fields.FirstName
| typeof Fields.MiddleName
| typeof Fields.LastName
| typeof Fields.MaidenName
| typeof Fields.NamePrefix
| typeof Fields.NameSuffix
| typeof Fields.Nickname
| typeof Fields.PhoneticFirstName
| typeof Fields.PhoneticMiddleName
| typeof Fields.PhoneticLastName
| typeof Fields.Birthday
| typeof Fields.NonGregorianBirthday
| typeof Fields.Emails
| typeof Fields.PhoneNumbers
| typeof Fields.Addresses
| typeof Fields.SocialProfiles
| typeof Fields.InstantMessageAddresses
| typeof Fields.UrlAddresses
| typeof Fields.Company
| typeof Fields.JobTitle
| typeof Fields.Department
| typeof Fields.ImageAvailable
| typeof Fields.Image
| typeof Fields.RawImage
| typeof Fields.ExtraNames
| typeof Fields.Note
| typeof Fields.Dates
| typeof Fields.Relationships;
export type Date = {
day?: number;
month?: number;
year?: number;
id: string;
label: string;
format?: CalendarFormatType;
};
export type Relationship = {
label: string;
name?: string;
id: string;
};
export type Email = {
email?: string;
isPrimary?: boolean;
label: string;
id: string;
};
export type PhoneNumber = {
number?: string;
isPrimary?: boolean;
digits?: string;
countryCode?: string;
label: string;
id: string;
};
export type Address = {
street?: string;
city?: string;
country?: string;
region?: string;
neighborhood?: string;
postalCode?: string;
poBox?: string;
isoCountryCode?: string;
label: string;
id: string;
};
export type SocialProfile = {
service?: string;
localizedProfile?: string;
url?: string;
username?: string;
userId?: string;
label: string;
id: string;
};
export type InstantMessageAddress = {
service?: string;
username?: string;
localizedService?: string;
label: string;
id: string;
};
export type UrlAddress = {
label: string;
url?: string;
id: string;
};
export type Image = {
uri?: string;
width?: number;
height?: number;
base64?: string;
};
export type Contact = {
id: string;
contactType: ContactType;
name: string;
firstName?: string;
middleName?: string;
lastName?: string;
maidenName?: string;
namePrefix?: string;
nameSuffix?: string;
nickname?: string;
phoneticFirstName?: string;
phoneticMiddleName?: string;
phoneticLastName?: string;
company?: string;
jobTitle?: string;
department?: string;
note?: string;
imageAvailable?: boolean;
image?: Image;
rawImage?: Image;
birthday?: Date;
dates?: Date[];
relationships?: Relationship[];
emails?: Email[];
phoneNumbers?: PhoneNumber[];
addresses?: Address[];
instantMessageAddresses?: InstantMessageAddress[];
urlAddresses?: UrlAddress[];
nonGregorianBirthday?: Date;
socialProfiles?: SocialProfile[];
};
export type ContactResponse = {
data: Contact[];
hasNextPage: boolean;
hasPreviousPage: boolean;
};
export type ContactSort =
| typeof SortTypes.UserDefault
| typeof SortTypes.FirstName
| typeof SortTypes.LastName
| typeof SortTypes.None;
export type ContactQuery = {
pageSize?: number;
pageOffset?: number;
fields?: FieldType[];
sort?: ContactSort;
name?: string;
id?: string | string[];
groupId?: string;
containerId?: string;
rawContacts?: boolean;
};
export type FormOptions = {
displayedPropertyKeys?: FieldType[];
message?: string;
alternateName?: string;
allowsEditing?: boolean;
allowsActions?: boolean;
shouldShowLinkedContacts?: boolean;
isNew?: boolean;
cancelButtonTitle?: string;
preventAnimation?: boolean;
groupId?: string;
};
export type GroupQuery = {
groupId?: string;
groupName?: string;
containerId?: string;
};
export type Group = {
name?: string;
id?: string;
};
export type ContainerQuery = {
contactId?: string;
groupId?: string;
containerId?: string | string[];
};
export type Container = {
name: string;
id: string;
type: ContainerType;
};
export { PermissionStatus, PermissionResponse };
/**
* Returns whether the Contacts API is enabled on the current device. This does not check the app permissions.
*
* @returns Async `boolean`, indicating whether the Contacts API is available on the current device. Currently this resolves to `true` on iOS and Android only.
*/
export async function isAvailableAsync(): Promise<boolean> {
return !!ExpoContacts.getContactsAsync;
}
export async function shareContactAsync(
contactId: string,
message: string,
shareOptions: object = {}
): Promise<any> {
if (isIOS) {
const url = await writeContactToFileAsync({
id: contactId,
});
return await Share.share(
{
url,
message,
},
shareOptions
);
} else if (!ExpoContacts.shareContactAsync) {
throw new UnavailabilityError('Contacts', 'shareContactAsync');
}
return await ExpoContacts.shareContactAsync(contactId, message);
}
/**
* Of note: This is the one method of expo-contacts that the Flutter plugin implemented.
* @see expo-contacts/ios/EXContacts/EXContacts.m
* @see expo-contacts-flutter-plugin/lib/expo_contacts.dart
*/
export async function getContactsAsync(contactQuery: ContactQuery = {}): Promise<ContactResponse> {
if (!ExpoContacts.getContactsAsync) {
throw new UnavailabilityError('Contacts', 'getContactsAsync');
}
return await ExpoContacts.getContactsAsync(contactQuery);
}
export async function getPagedContactsAsync(
contactQuery: ContactQuery = {}
): Promise<ContactResponse> {
const { pageSize, ...nOptions } = contactQuery;
if (pageSize && pageSize <= 0) {
throw new Error('Error: Contacts.getPagedContactsAsync: `pageSize` must be greater than 0');
}
return await getContactsAsync({
...nOptions,
pageSize,
});
}
export async function getContactByIdAsync(
id: string,
fields?: FieldType[]
): Promise<Contact | undefined> {
if (!ExpoContacts.getContactsAsync) {
throw new UnavailabilityError('Contacts', 'getContactsAsync');
}
if (id == null) {
throw new Error('Error: Contacts.getContactByIdAsync: Please pass an ID as a parameter');
} else {
const results = await ExpoContacts.getContactsAsync({
pageSize: 1,
pageOffset: 0,
fields,
id,
});
if (results && results.data && results.data.length > 0) {
return results.data[0];
}
}
return undefined;
}
export async function addContactAsync(contact: Contact, containerId?: string): Promise<string> {
if (!ExpoContacts.addContactAsync) {
throw new UnavailabilityError('Contacts', 'addContactAsync');
}
return await ExpoContacts.addContactAsync(contact, containerId);
}
export async function updateContactAsync(contact: Contact): Promise<string> {
if (!ExpoContacts.updateContactAsync) {
throw new UnavailabilityError('Contacts', 'updateContactAsync');
}
return await ExpoContacts.updateContactAsync(contact);
}
export async function removeContactAsync(contactId: string): Promise<any> {
if (!ExpoContacts.removeContactAsync) {
throw new UnavailabilityError('Contacts', 'removeContactAsync');
}
return await ExpoContacts.removeContactAsync(contactId);
}
export async function writeContactToFileAsync(
contactQuery: ContactQuery = {}
): Promise<string | undefined> {
if (!ExpoContacts.writeContactToFileAsync) {
throw new UnavailabilityError('Contacts', 'writeContactToFileAsync');
}
return await ExpoContacts.writeContactToFileAsync(contactQuery);
}
export async function presentFormAsync(
contactId?: string | null,
contact?: Contact | null,
formOptions: FormOptions = {}
): Promise<any> {
if (!ExpoContacts.presentFormAsync) {
throw new UnavailabilityError('Contacts', 'presentFormAsync');
}
if (isIOS) {
const adjustedOptions = formOptions;
if (contactId) {
if (contact) {
contact = undefined;
console.log(
'Expo.Contacts.presentFormAsync: You should define either a `contact` or a `contactId` but not both.'
);
}
if (adjustedOptions.isNew !== undefined) {
console.log(
'Expo.Contacts.presentFormAsync: formOptions.isNew is not supported with `contactId`'
);
}
}
return await ExpoContacts.presentFormAsync(contactId, contact, adjustedOptions);
} else {
return await ExpoContacts.presentFormAsync(contactId, contact, formOptions);
}
}
// iOS Only
export async function addExistingGroupToContainerAsync(
groupId: string,
containerId: string
): Promise<any> {
if (!ExpoContacts.addExistingGroupToContainerAsync) {
throw new UnavailabilityError('Contacts', 'addExistingGroupToContainerAsync');
}
return await ExpoContacts.addExistingGroupToContainerAsync(groupId, containerId);
}
/**
*
* @param name The name for the group. A UUID v4 string would work, for example.
* @param containerId
*/
export async function createGroupAsync(name: string, containerId?: string): Promise<string> {
if (!ExpoContacts.createGroupAsync) {
throw new UnavailabilityError('Contacts', 'createGroupAsync');
}
// name = name || uuidv4();
if (!containerId) {
containerId = await getDefaultContainerIdAsync();
}
return await ExpoContacts.createGroupAsync(name, containerId);
}
export async function updateGroupNameAsync(groupName: string, groupId: string): Promise<any> {
if (!ExpoContacts.updateGroupNameAsync) {
throw new UnavailabilityError('Contacts', 'updateGroupNameAsync');
}
return await ExpoContacts.updateGroupNameAsync(groupName, groupId);
}
export async function removeGroupAsync(groupId: string): Promise<any> {
if (!ExpoContacts.removeGroupAsync) {
throw new UnavailabilityError('Contacts', 'removeGroupAsync');
}
return await ExpoContacts.removeGroupAsync(groupId);
}
export async function addExistingContactToGroupAsync(
contactId: string,
groupId: string
): Promise<any> {
if (!ExpoContacts.addExistingContactToGroupAsync) {
throw new UnavailabilityError('Contacts', 'addExistingContactToGroupAsync');
}
return await ExpoContacts.addExistingContactToGroupAsync(contactId, groupId);
}
export async function removeContactFromGroupAsync(
contactId: string,
groupId: string
): Promise<any> {
if (!ExpoContacts.removeContactFromGroupAsync) {
throw new UnavailabilityError('Contacts', 'removeContactFromGroupAsync');
}
return await ExpoContacts.removeContactFromGroupAsync(contactId, groupId);
}
export async function getGroupsAsync(groupQuery: GroupQuery): Promise<Group[]> {
if (!ExpoContacts.getGroupsAsync) {
throw new UnavailabilityError('Contacts', 'getGroupsAsync');
}
return await ExpoContacts.getGroupsAsync(groupQuery);
}
export async function getDefaultContainerIdAsync(): Promise<string> {
if (!ExpoContacts.getDefaultContainerIdentifierAsync) {
throw new UnavailabilityError('Contacts', 'getDefaultContainerIdentifierAsync');
}
return await ExpoContacts.getDefaultContainerIdentifierAsync();
}
export async function getContainersAsync(containerQuery: ContainerQuery): Promise<Container[]> {
if (!ExpoContacts.getContainersAsync) {
throw new UnavailabilityError('Contacts', 'getContainersAsync');
}
return await ExpoContacts.getContainersAsync(containerQuery);
}
export async function getPermissionsAsync(): Promise<PermissionResponse> {
if (!ExpoContacts.getPermissionsAsync) {
throw new UnavailabilityError('Contacts', 'getPermissionsAsync');
}
return await ExpoContacts.getPermissionsAsync();
}
export async function requestPermissionsAsync(): Promise<PermissionResponse> {
if (!ExpoContacts.requestPermissionsAsync) {
throw new UnavailabilityError('Contacts', 'requestPermissionsAsync');
}
return await ExpoContacts.requestPermissionsAsync();
}
// Legacy
export const PHONE_NUMBERS = 'phoneNumbers';
export const EMAILS = 'emails';
export const ADDRESSES = 'addresses';
export const IMAGE = 'image';
export const RAW_IMAGE = 'rawImage';
export const NOTE = 'note';
export const BIRTHDAY = 'birthday';
export const NON_GREGORIAN_BIRTHDAY = 'nonGregorianBirthday';
export const NAME_PREFIX = 'namePrefix';
export const NAME_SUFFIX = 'nameSuffix';
export const PHONETIC_FIRST_NAME = 'phoneticFirstName';
export const PHONETIC_MIDDLE_NAME = 'phoneticMiddleName';
export const PHONETIC_LAST_NAME = 'phoneticLastName';
export const SOCIAL_PROFILES = 'socialProfiles';
export const IM_ADDRESSES = 'instantMessageAddresses';
export const URLS = 'urlAddresses';
export const DATES = 'dates';
export const RAW_DATES = 'rawDates';
export const RELATIONSHIPS = 'relationships';
export const Fields = {
ID: 'id',
ContactType: 'contactType',
Name: 'name',
FirstName: 'firstName',
MiddleName: 'middleName',
LastName: 'lastName',
MaidenName: 'maidenName',
NamePrefix: 'namePrefix',
NameSuffix: 'nameSuffix',
Nickname: 'nickname',
PhoneticFirstName: 'phoneticFirstName',
PhoneticMiddleName: 'phoneticMiddleName',
PhoneticLastName: 'phoneticLastName',
Birthday: 'birthday',
NonGregorianBirthday: 'nonGregorianBirthday',
Emails: 'emails',
PhoneNumbers: 'phoneNumbers',
Addresses: 'addresses',
SocialProfiles: 'socialProfiles',
InstantMessageAddresses: 'instantMessageAddresses',
UrlAddresses: 'urlAddresses',
Company: 'company',
JobTitle: 'jobTitle',
Department: 'department',
ImageAvailable: 'imageAvailable',
Image: 'image',
RawImage: 'rawImage',
ExtraNames: 'extraNames',
Note: 'note',
Dates: 'dates',
Relationships: 'relationships',
};
export const CalendarFormats = {
Gregorian: 'gregorian',
Buddhist: 'buddhist',
Chinese: 'chinese',
Coptic: 'coptic',
EthiopicAmeteMihret: 'ethiopicAmeteMihret',
EthiopicAmeteAlem: 'ethiopicAmeteAlem',
Hebrew: 'hebrew',
ISO8601: 'iso8601',
Indian: 'indian',
Islamic: 'islamic',
IslamicCivil: 'islamicCivil',
Japanese: 'japanese',
Persian: 'persian',
RepublicOfChina: 'republicOfChina',
IslamicTabular: 'islamicTabular',
IslamicUmmAlQura: 'islamicUmmAlQura',
};
export const ContainerTypes = {
Local: 'local',
Exchange: 'exchange',
CardDAV: 'cardDAV',
Unassigned: 'unassigned',
};
export const SortTypes = {
UserDefault: 'userDefault',
FirstName: 'firstName',
LastName: 'lastName',
None: 'none',
};
export const ContactTypes = {
Person: 'person',
Company: 'company',
};