1 | import { UnavailabilityError } from '@unimodules/core';
|
2 | import { PermissionStatus } from 'expo-modules-core';
|
3 | import { Platform, Share } from 'react-native';
|
4 | import { v4 as uuidv4 } from 'uuid';
|
5 | import ExpoContacts from './ExpoContacts';
|
6 | export { PermissionStatus };
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export async function isAvailableAsync() {
|
13 | return !!ExpoContacts.getContactsAsync;
|
14 | }
|
15 | export async function shareContactAsync(contactId, message, shareOptions = {}) {
|
16 | if (Platform.OS === 'ios') {
|
17 | const url = await writeContactToFileAsync({
|
18 | id: contactId,
|
19 | });
|
20 | return await Share.share({
|
21 | url,
|
22 | message,
|
23 | }, shareOptions);
|
24 | }
|
25 | else if (!ExpoContacts.shareContactAsync) {
|
26 | throw new UnavailabilityError('Contacts', 'shareContactAsync');
|
27 | }
|
28 | return await ExpoContacts.shareContactAsync(contactId, message);
|
29 | }
|
30 | export async function getContactsAsync(contactQuery = {}) {
|
31 | if (!ExpoContacts.getContactsAsync) {
|
32 | throw new UnavailabilityError('Contacts', 'getContactsAsync');
|
33 | }
|
34 | return await ExpoContacts.getContactsAsync(contactQuery);
|
35 | }
|
36 | export async function getPagedContactsAsync(contactQuery = {}) {
|
37 | const { pageSize, ...nOptions } = contactQuery;
|
38 | if (pageSize && pageSize <= 0) {
|
39 | throw new Error('Error: Contacts.getPagedContactsAsync: `pageSize` must be greater than 0');
|
40 | }
|
41 | return await getContactsAsync({
|
42 | ...nOptions,
|
43 | pageSize,
|
44 | });
|
45 | }
|
46 | export async function getContactByIdAsync(id, fields) {
|
47 | if (!ExpoContacts.getContactsAsync) {
|
48 | throw new UnavailabilityError('Contacts', 'getContactsAsync');
|
49 | }
|
50 | if (id == null) {
|
51 | throw new Error('Error: Contacts.getContactByIdAsync: Please pass an ID as a parameter');
|
52 | }
|
53 | else {
|
54 | const results = await ExpoContacts.getContactsAsync({
|
55 | pageSize: 1,
|
56 | pageOffset: 0,
|
57 | fields,
|
58 | id,
|
59 | });
|
60 | if (results && results.data && results.data.length > 0) {
|
61 | return results.data[0];
|
62 | }
|
63 | }
|
64 | return undefined;
|
65 | }
|
66 | export async function addContactAsync(contact, containerId) {
|
67 | if (!ExpoContacts.addContactAsync) {
|
68 | throw new UnavailabilityError('Contacts', 'addContactAsync');
|
69 | }
|
70 | return await ExpoContacts.addContactAsync(contact, containerId);
|
71 | }
|
72 | export async function updateContactAsync(contact) {
|
73 | if (!ExpoContacts.updateContactAsync) {
|
74 | throw new UnavailabilityError('Contacts', 'updateContactAsync');
|
75 | }
|
76 | return await ExpoContacts.updateContactAsync(contact);
|
77 | }
|
78 | export async function removeContactAsync(contactId) {
|
79 | if (!ExpoContacts.removeContactAsync) {
|
80 | throw new UnavailabilityError('Contacts', 'removeContactAsync');
|
81 | }
|
82 | return await ExpoContacts.removeContactAsync(contactId);
|
83 | }
|
84 | export async function writeContactToFileAsync(contactQuery = {}) {
|
85 | if (!ExpoContacts.writeContactToFileAsync) {
|
86 | throw new UnavailabilityError('Contacts', 'writeContactToFileAsync');
|
87 | }
|
88 | return await ExpoContacts.writeContactToFileAsync(contactQuery);
|
89 | }
|
90 | export async function presentFormAsync(contactId, contact, formOptions = {}) {
|
91 | if (!ExpoContacts.presentFormAsync) {
|
92 | throw new UnavailabilityError('Contacts', 'presentFormAsync');
|
93 | }
|
94 | if (Platform.OS === 'ios') {
|
95 | const adjustedOptions = formOptions;
|
96 | if (contactId) {
|
97 | if (contact) {
|
98 | contact = undefined;
|
99 | console.log('Expo.Contacts.presentFormAsync: You should define either a `contact` or a `contactId` but not both.');
|
100 | }
|
101 | if (adjustedOptions.isNew !== undefined) {
|
102 | console.log('Expo.Contacts.presentFormAsync: formOptions.isNew is not supported with `contactId`');
|
103 | }
|
104 | }
|
105 | return await ExpoContacts.presentFormAsync(contactId, contact, adjustedOptions);
|
106 | }
|
107 | else {
|
108 | return await ExpoContacts.presentFormAsync(contactId, contact, formOptions);
|
109 | }
|
110 | }
|
111 |
|
112 | export async function addExistingGroupToContainerAsync(groupId, containerId) {
|
113 | if (!ExpoContacts.addExistingGroupToContainerAsync) {
|
114 | throw new UnavailabilityError('Contacts', 'addExistingGroupToContainerAsync');
|
115 | }
|
116 | return await ExpoContacts.addExistingGroupToContainerAsync(groupId, containerId);
|
117 | }
|
118 | export async function createGroupAsync(name, containerId) {
|
119 | if (!ExpoContacts.createGroupAsync) {
|
120 | throw new UnavailabilityError('Contacts', 'createGroupAsync');
|
121 | }
|
122 | name = name || uuidv4();
|
123 | if (!containerId) {
|
124 | containerId = await getDefaultContainerIdAsync();
|
125 | }
|
126 | return await ExpoContacts.createGroupAsync(name, containerId);
|
127 | }
|
128 | export async function updateGroupNameAsync(groupName, groupId) {
|
129 | if (!ExpoContacts.updateGroupNameAsync) {
|
130 | throw new UnavailabilityError('Contacts', 'updateGroupNameAsync');
|
131 | }
|
132 | return await ExpoContacts.updateGroupNameAsync(groupName, groupId);
|
133 | }
|
134 | export async function removeGroupAsync(groupId) {
|
135 | if (!ExpoContacts.removeGroupAsync) {
|
136 | throw new UnavailabilityError('Contacts', 'removeGroupAsync');
|
137 | }
|
138 | return await ExpoContacts.removeGroupAsync(groupId);
|
139 | }
|
140 | export async function addExistingContactToGroupAsync(contactId, groupId) {
|
141 | if (!ExpoContacts.addExistingContactToGroupAsync) {
|
142 | throw new UnavailabilityError('Contacts', 'addExistingContactToGroupAsync');
|
143 | }
|
144 | return await ExpoContacts.addExistingContactToGroupAsync(contactId, groupId);
|
145 | }
|
146 | export async function removeContactFromGroupAsync(contactId, groupId) {
|
147 | if (!ExpoContacts.removeContactFromGroupAsync) {
|
148 | throw new UnavailabilityError('Contacts', 'removeContactFromGroupAsync');
|
149 | }
|
150 | return await ExpoContacts.removeContactFromGroupAsync(contactId, groupId);
|
151 | }
|
152 | export async function getGroupsAsync(groupQuery) {
|
153 | if (!ExpoContacts.getGroupsAsync) {
|
154 | throw new UnavailabilityError('Contacts', 'getGroupsAsync');
|
155 | }
|
156 | return await ExpoContacts.getGroupsAsync(groupQuery);
|
157 | }
|
158 | export async function getDefaultContainerIdAsync() {
|
159 | if (!ExpoContacts.getDefaultContainerIdentifierAsync) {
|
160 | throw new UnavailabilityError('Contacts', 'getDefaultContainerIdentifierAsync');
|
161 | }
|
162 | return await ExpoContacts.getDefaultContainerIdentifierAsync();
|
163 | }
|
164 | export async function getContainersAsync(containerQuery) {
|
165 | if (!ExpoContacts.getContainersAsync) {
|
166 | throw new UnavailabilityError('Contacts', 'getContainersAsync');
|
167 | }
|
168 | return await ExpoContacts.getContainersAsync(containerQuery);
|
169 | }
|
170 | export async function getPermissionsAsync() {
|
171 | if (!ExpoContacts.getPermissionsAsync) {
|
172 | throw new UnavailabilityError('Contacts', 'getPermissionsAsync');
|
173 | }
|
174 | return await ExpoContacts.getPermissionsAsync();
|
175 | }
|
176 | export async function requestPermissionsAsync() {
|
177 | if (!ExpoContacts.requestPermissionsAsync) {
|
178 | throw new UnavailabilityError('Contacts', 'requestPermissionsAsync');
|
179 | }
|
180 | return await ExpoContacts.requestPermissionsAsync();
|
181 | }
|
182 |
|
183 | export const PHONE_NUMBERS = 'phoneNumbers';
|
184 | export const EMAILS = 'emails';
|
185 | export const ADDRESSES = 'addresses';
|
186 | export const IMAGE = 'image';
|
187 | export const RAW_IMAGE = 'rawImage';
|
188 | export const NOTE = 'note';
|
189 | export const BIRTHDAY = 'birthday';
|
190 | export const NON_GREGORIAN_BIRTHDAY = 'nonGregorianBirthday';
|
191 | export const NAME_PREFIX = 'namePrefix';
|
192 | export const NAME_SUFFIX = 'nameSuffix';
|
193 | export const PHONETIC_FIRST_NAME = 'phoneticFirstName';
|
194 | export const PHONETIC_MIDDLE_NAME = 'phoneticMiddleName';
|
195 | export const PHONETIC_LAST_NAME = 'phoneticLastName';
|
196 | export const SOCIAL_PROFILES = 'socialProfiles';
|
197 | export const IM_ADDRESSES = 'instantMessageAddresses';
|
198 | export const URLS = 'urlAddresses';
|
199 | export const DATES = 'dates';
|
200 | export const RAW_DATES = 'rawDates';
|
201 | export const RELATIONSHIPS = 'relationships';
|
202 | export const Fields = {
|
203 | ID: 'id',
|
204 | ContactType: 'contactType',
|
205 | Name: 'name',
|
206 | FirstName: 'firstName',
|
207 | MiddleName: 'middleName',
|
208 | LastName: 'lastName',
|
209 | MaidenName: 'maidenName',
|
210 | NamePrefix: 'namePrefix',
|
211 | NameSuffix: 'nameSuffix',
|
212 | Nickname: 'nickname',
|
213 | PhoneticFirstName: 'phoneticFirstName',
|
214 | PhoneticMiddleName: 'phoneticMiddleName',
|
215 | PhoneticLastName: 'phoneticLastName',
|
216 | Birthday: 'birthday',
|
217 | NonGregorianBirthday: 'nonGregorianBirthday',
|
218 | Emails: 'emails',
|
219 | PhoneNumbers: 'phoneNumbers',
|
220 | Addresses: 'addresses',
|
221 | SocialProfiles: 'socialProfiles',
|
222 | InstantMessageAddresses: 'instantMessageAddresses',
|
223 | UrlAddresses: 'urlAddresses',
|
224 | Company: 'company',
|
225 | JobTitle: 'jobTitle',
|
226 | Department: 'department',
|
227 | ImageAvailable: 'imageAvailable',
|
228 | Image: 'image',
|
229 | RawImage: 'rawImage',
|
230 | ExtraNames: 'extraNames',
|
231 | Note: 'note',
|
232 | Dates: 'dates',
|
233 | Relationships: 'relationships',
|
234 | };
|
235 | export const CalendarFormats = {
|
236 | Gregorian: 'gregorian',
|
237 | Buddhist: 'buddhist',
|
238 | Chinese: 'chinese',
|
239 | Coptic: 'coptic',
|
240 | EthiopicAmeteMihret: 'ethiopicAmeteMihret',
|
241 | EthiopicAmeteAlem: 'ethiopicAmeteAlem',
|
242 | Hebrew: 'hebrew',
|
243 | ISO8601: 'iso8601',
|
244 | Indian: 'indian',
|
245 | Islamic: 'islamic',
|
246 | IslamicCivil: 'islamicCivil',
|
247 | Japanese: 'japanese',
|
248 | Persian: 'persian',
|
249 | RepublicOfChina: 'republicOfChina',
|
250 | IslamicTabular: 'islamicTabular',
|
251 | IslamicUmmAlQura: 'islamicUmmAlQura',
|
252 | };
|
253 | export const ContainerTypes = {
|
254 | Local: 'local',
|
255 | Exchange: 'exchange',
|
256 | CardDAV: 'cardDAV',
|
257 | Unassigned: 'unassigned',
|
258 | };
|
259 | export const SortTypes = {
|
260 | UserDefault: 'userDefault',
|
261 | FirstName: 'firstName',
|
262 | LastName: 'lastName',
|
263 | None: 'none',
|
264 | };
|
265 | export const ContactTypes = {
|
266 | Person: 'person',
|
267 | Company: 'company',
|
268 | };
|
269 |
|
\ | No newline at end of file |