forked from wix-incubator/react-native-paged-contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·190 lines (174 loc) · 5.82 KB
/
index.js
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
import {
NativeModules,
Platform,
PermissionsAndroid
} from 'react-native';
const PagedContactsModule = NativeModules.ReactNativePagedContacts;
/**
* Generates a globally unique identifier.
*
* @returns {String} A globally unique identifier.
*/
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/**
* The `PagedContacts` class is a class that can fetch native contacts in pages.
*
* @export
* @class PagedContacts
*/
export class PagedContacts {
/**
* Creates an instance of PagedContacts.
*
* @param {String} nameMatch The contact name to be matched
*
* @memberOf PagedContacts
*/
constructor(nameMatch) {
this._uuid = guid();
this.setNameMatch(nameMatch);
}
/**
* Return the authorization status.
*
* @returns {String} The authorization status
*
* @memberOf PagedContacts
*/
async getAuthorizationStatus() {
return PagedContactsModule.getAuthorizationStatus();
}
/**
* Requests contact access from the operating system.
*
* @returns {Boolean} `true` if access was granted or `false` otherwise
*
* @memberOf PagedContacts
*/
async requestAccess() {
return PagedContactsModule.requestAccess(this._uuid);
}
/**
* Set the contact name to be matched.
*
* @param {String} str The contact name to be matched
*
* @memberOf PagedContacts
*/
setNameMatch(str) {
this._nameMatch = str;
PagedContactsModule.setNameMatch(this._uuid, str);
}
/**
* Return the total number of contacts.
*
* @returns {Number} The total number of contacts
*
* @memberOf PagedContacts
*/
async getContactsCount() {
return PagedContactsModule.contactsCount(this._uuid);
}
/**
* Fetches `batchSize` contacts, starting from `offset`, returning the provided keys.
*
* @param {Number} offset The fetch offset
* @param {Number} batchSize The fetch size
* @param {String[]} keysToFetch The keys to fetch
* @returns {Object[]} The fetched contacts
*
* @memberOf PagedContacts
*/
async getContactsWithRange(offset, batchSize, keysToFetch) {
return PagedContactsModule.getContactsWithRange(this._uuid, offset, batchSize, keysToFetch);
}
/**
* Fetches contacts matching the provided identifiers, returning the provided keys.
*
* @param {String[]} identifiers The contact identifiers to match
* @param {String[]} keysToFetch The keys to fetch
* @returns {Object[]} The fetched contacts
*
* @memberOf PagedContacts
*/
async getContactsWithIdentifiers(identifiers, keysToFetch) {
return PagedContactsModule.getContactsWithIdentifiers(this._uuid, identifiers, keysToFetch);
}
/**
* Disposes the underlying native component, freeing resources.
* You must call this when the `PagedContacts` object is no longer needed.
*
* @memberOf PagedContacts
*/
dispose() {
if (Platform.OS === 'ios') {
PagedContactsModule.dispose(this._uuid);
}
}
/**
* Creates a Contact on the device.
* @param {Object} contact the contact to be added to the device
*
* @memberOf PagedContacts
*/
async addContact(contact) {
if (Platform.OS === 'android') {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS,
{
title: 'Contacts',
message: 'This App would like to write contacts',
},
);
}
await PagedContactsModule.addContact(
contact,
this._uuid,
);
}
}
PagedContacts.identifier = PagedContactsModule.identifier;
PagedContacts.displayName = PagedContactsModule.displayName;
PagedContacts.namePrefix = PagedContactsModule.namePrefix;
PagedContacts.givenName = PagedContactsModule.givenName;
PagedContacts.middleName = PagedContactsModule.middleName;
PagedContacts.familyName = PagedContactsModule.familyName;
PagedContacts.nameSuffix = PagedContactsModule.nameSuffix;
PagedContacts.nickname = PagedContactsModule.nickname;
PagedContacts.organizationName = PagedContactsModule.organizationName;
PagedContacts.departmentName = PagedContactsModule.departmentName;
PagedContacts.jobTitle = PagedContactsModule.jobTitle;
PagedContacts.phoneticGivenName = PagedContactsModule.phoneticGivenName;
PagedContacts.phoneticMiddleName = PagedContactsModule.phoneticMiddleName;
PagedContacts.phoneticFamilyName = PagedContactsModule.phoneticFamilyName;
PagedContacts.phoneticOrganizationName = PagedContactsModule.phoneticOrganizationName;
PagedContacts.birthday = PagedContactsModule.birthday;
PagedContacts.note = PagedContactsModule.note;
PagedContacts.imageData = PagedContactsModule.imageData;
PagedContacts.thumbnailImageData = PagedContactsModule.thumbnailImageData;
PagedContacts.phoneNumbers = PagedContactsModule.phoneNumbers;
PagedContacts.emailAddresses = PagedContactsModule.emailAddresses;
PagedContacts.postalAddresses = PagedContactsModule.postalAddresses;
PagedContacts.dates = PagedContactsModule.dates;
PagedContacts.urlAddresses = PagedContactsModule.urlAddresses;
PagedContacts.relations = PagedContactsModule.relations;
PagedContacts.instantMessageAddresses = PagedContactsModule.instantMessageAddresses;
if (Platform.OS === 'ios') {
PagedContacts.previousFamilyName = PagedContactsModule.previousFamilyName;
PagedContacts.nonGregorianBirthday = PagedContactsModule.nonGregorianBirthday;
PagedContacts.socialProfiles = PagedContactsModule.socialProfiles;
}
if (Platform.OS === 'android') {
PagedContacts.identity = PagedContactsModule.identity;
}
PagedContacts.denied = PagedContactsModule.denied;
PagedContacts.authorized = PagedContactsModule.authorized;
if (Platform.OS === 'ios') {
PagedContacts.notDetermined = PagedContactsModule.notDetermined;
PagedContacts.restricted = PagedContactsModule.restricted;
}