UNPKG

9.97 kBJavaScriptView Raw
1import { UnavailabilityError } from '@unimodules/core';
2import { PermissionStatus } from 'expo-modules-core';
3import { Platform, Share } from 'react-native';
4import { v4 as uuidv4 } from 'uuid';
5import ExpoContacts from './ExpoContacts';
6export { PermissionStatus };
7/**
8 * Returns whether the Contacts API is enabled on the current device. This does not check the app permissions.
9 *
10 * @returns Async `boolean`, indicating whether the Contacts API is available on the current device. Currently this resolves to `true` on iOS and Android only.
11 */
12export async function isAvailableAsync() {
13 return !!ExpoContacts.getContactsAsync;
14}
15export 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}
30export async function getContactsAsync(contactQuery = {}) {
31 if (!ExpoContacts.getContactsAsync) {
32 throw new UnavailabilityError('Contacts', 'getContactsAsync');
33 }
34 return await ExpoContacts.getContactsAsync(contactQuery);
35}
36export 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}
46export 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}
66export async function addContactAsync(contact, containerId) {
67 if (!ExpoContacts.addContactAsync) {
68 throw new UnavailabilityError('Contacts', 'addContactAsync');
69 }
70 return await ExpoContacts.addContactAsync(contact, containerId);
71}
72export async function updateContactAsync(contact) {
73 if (!ExpoContacts.updateContactAsync) {
74 throw new UnavailabilityError('Contacts', 'updateContactAsync');
75 }
76 return await ExpoContacts.updateContactAsync(contact);
77}
78export async function removeContactAsync(contactId) {
79 if (!ExpoContacts.removeContactAsync) {
80 throw new UnavailabilityError('Contacts', 'removeContactAsync');
81 }
82 return await ExpoContacts.removeContactAsync(contactId);
83}
84export async function writeContactToFileAsync(contactQuery = {}) {
85 if (!ExpoContacts.writeContactToFileAsync) {
86 throw new UnavailabilityError('Contacts', 'writeContactToFileAsync');
87 }
88 return await ExpoContacts.writeContactToFileAsync(contactQuery);
89}
90export 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// iOS Only
112export async function addExistingGroupToContainerAsync(groupId, containerId) {
113 if (!ExpoContacts.addExistingGroupToContainerAsync) {
114 throw new UnavailabilityError('Contacts', 'addExistingGroupToContainerAsync');
115 }
116 return await ExpoContacts.addExistingGroupToContainerAsync(groupId, containerId);
117}
118export 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}
128export async function updateGroupNameAsync(groupName, groupId) {
129 if (!ExpoContacts.updateGroupNameAsync) {
130 throw new UnavailabilityError('Contacts', 'updateGroupNameAsync');
131 }
132 return await ExpoContacts.updateGroupNameAsync(groupName, groupId);
133}
134export async function removeGroupAsync(groupId) {
135 if (!ExpoContacts.removeGroupAsync) {
136 throw new UnavailabilityError('Contacts', 'removeGroupAsync');
137 }
138 return await ExpoContacts.removeGroupAsync(groupId);
139}
140export async function addExistingContactToGroupAsync(contactId, groupId) {
141 if (!ExpoContacts.addExistingContactToGroupAsync) {
142 throw new UnavailabilityError('Contacts', 'addExistingContactToGroupAsync');
143 }
144 return await ExpoContacts.addExistingContactToGroupAsync(contactId, groupId);
145}
146export async function removeContactFromGroupAsync(contactId, groupId) {
147 if (!ExpoContacts.removeContactFromGroupAsync) {
148 throw new UnavailabilityError('Contacts', 'removeContactFromGroupAsync');
149 }
150 return await ExpoContacts.removeContactFromGroupAsync(contactId, groupId);
151}
152export async function getGroupsAsync(groupQuery) {
153 if (!ExpoContacts.getGroupsAsync) {
154 throw new UnavailabilityError('Contacts', 'getGroupsAsync');
155 }
156 return await ExpoContacts.getGroupsAsync(groupQuery);
157}
158export async function getDefaultContainerIdAsync() {
159 if (!ExpoContacts.getDefaultContainerIdentifierAsync) {
160 throw new UnavailabilityError('Contacts', 'getDefaultContainerIdentifierAsync');
161 }
162 return await ExpoContacts.getDefaultContainerIdentifierAsync();
163}
164export async function getContainersAsync(containerQuery) {
165 if (!ExpoContacts.getContainersAsync) {
166 throw new UnavailabilityError('Contacts', 'getContainersAsync');
167 }
168 return await ExpoContacts.getContainersAsync(containerQuery);
169}
170export async function getPermissionsAsync() {
171 if (!ExpoContacts.getPermissionsAsync) {
172 throw new UnavailabilityError('Contacts', 'getPermissionsAsync');
173 }
174 return await ExpoContacts.getPermissionsAsync();
175}
176export async function requestPermissionsAsync() {
177 if (!ExpoContacts.requestPermissionsAsync) {
178 throw new UnavailabilityError('Contacts', 'requestPermissionsAsync');
179 }
180 return await ExpoContacts.requestPermissionsAsync();
181}
182// Legacy
183export const PHONE_NUMBERS = 'phoneNumbers';
184export const EMAILS = 'emails';
185export const ADDRESSES = 'addresses';
186export const IMAGE = 'image';
187export const RAW_IMAGE = 'rawImage';
188export const NOTE = 'note';
189export const BIRTHDAY = 'birthday';
190export const NON_GREGORIAN_BIRTHDAY = 'nonGregorianBirthday';
191export const NAME_PREFIX = 'namePrefix';
192export const NAME_SUFFIX = 'nameSuffix';
193export const PHONETIC_FIRST_NAME = 'phoneticFirstName';
194export const PHONETIC_MIDDLE_NAME = 'phoneticMiddleName';
195export const PHONETIC_LAST_NAME = 'phoneticLastName';
196export const SOCIAL_PROFILES = 'socialProfiles';
197export const IM_ADDRESSES = 'instantMessageAddresses';
198export const URLS = 'urlAddresses';
199export const DATES = 'dates';
200export const RAW_DATES = 'rawDates';
201export const RELATIONSHIPS = 'relationships';
202export 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};
235export 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};
253export const ContainerTypes = {
254 Local: 'local',
255 Exchange: 'exchange',
256 CardDAV: 'cardDAV',
257 Unassigned: 'unassigned',
258};
259export const SortTypes = {
260 UserDefault: 'userDefault',
261 FirstName: 'firstName',
262 LastName: 'lastName',
263 None: 'none',
264};
265export const ContactTypes = {
266 Person: 'person',
267 Company: 'company',
268};
269//# sourceMappingURL=Contacts.js.map
\No newline at end of file