Skip to content

Commit 2c6532b

Browse files
authored
Merge pull request #434 from pitbot/master
Add contacts count feature
2 parents fcd6544 + b317836 commit 2c6532b

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ protected Void doInBackground(final Void ... params) {
107107
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
108108
}
109109

110+
@ReactMethod
111+
public void getCount(final Callback callback) {
112+
AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
113+
@Override
114+
protected Void doInBackground(final Void ... params) {
115+
Context context = getReactApplicationContext();
116+
ContentResolver cr = context.getContentResolver();
117+
118+
ContactsProvider contactsProvider = new ContactsProvider(cr);
119+
Integer contacts = contactsProvider.getContactsCount();
120+
121+
callback.invoke(contacts);
122+
return null;
123+
}
124+
};
125+
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
126+
}
127+
110128
/**
111129
* Retrieves contacts matching String.
112130
* Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy otherwise.

android/src/main/java/com/rt2zz/reactnativecontacts/ContactsProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ public WritableMap getContactById(String contactId) {
199199
return null;
200200
}
201201

202+
public Integer getContactsCount() {
203+
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
204+
int count = cursor.getCount();
205+
206+
return count;
207+
}
208+
202209
public WritableArray getContacts() {
203210
Map<String, Contact> justMe;
204211
{

example/App.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export default class App extends Component<Props> {
3131
this.search = this.search.bind(this);
3232

3333
this.state = {
34-
contacts: []
34+
contacts: [],
35+
searchPlaceholder: "Search"
3536
};
3637
}
3738

@@ -56,6 +57,10 @@ export default class App extends Component<Props> {
5657
this.setState({ contacts });
5758
}
5859
});
60+
61+
Contacts.getCount(count => {
62+
this.setState({ searchPlaceholder: `Search ${count} contacts` });
63+
});
5964
}
6065

6166
search(text) {
@@ -92,7 +97,10 @@ export default class App extends Component<Props> {
9297
}}
9398
/>
9499
</View>
95-
<SearchBar onChangeText={this.search} />
100+
<SearchBar
101+
searchPlaceholder={this.state.searchPlaceholder}
102+
onChangeText={this.search}
103+
/>
96104
<ScrollView style={{ flex: 1 }}>
97105
{this.state.contacts.map(contact => {
98106
return (

ios/RCTContacts/RCTContacts.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,46 @@ -(void) getAllContacts:(RCTResponseSenderBlock) callback
148148
[self retrieveContactsFromAddressBook:contactStore withThumbnails:withThumbnails withCallback:callback];
149149
}
150150

151+
-(void) getAllContactsCount:(RCTResponseSenderBlock) callback
152+
{
153+
CNContactStore* contactStore = [self contactsStore:callback];
154+
if(!contactStore)
155+
return;
156+
157+
NSMutableArray *contacts = [[NSMutableArray alloc] init];
158+
159+
NSError* contactError;
160+
[contactStore containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[contactStore.defaultContainerIdentifier]] error:&contactError];
161+
162+
163+
NSMutableArray *keysToFetch = [[NSMutableArray alloc]init];
164+
[keysToFetch addObjectsFromArray:@[
165+
CNContactEmailAddressesKey,
166+
CNContactPhoneNumbersKey,
167+
CNContactFamilyNameKey,
168+
CNContactGivenNameKey,
169+
CNContactMiddleNameKey,
170+
CNContactPostalAddressesKey,
171+
CNContactOrganizationNameKey,
172+
CNContactJobTitleKey,
173+
CNContactImageDataAvailableKey,
174+
CNContactUrlAddressesKey,
175+
CNContactBirthdayKey
176+
]];
177+
178+
CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
179+
BOOL success = [contactStore enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){
180+
NSDictionary *contactDict = [self contactToDictionary: contact withThumbnails:false];
181+
[contacts addObject:contactDict];
182+
}];
183+
184+
int contactsCount = [contacts count];
185+
186+
NSNumber *count = [NSNumber numberWithInt:contactsCount];
187+
188+
callback(@[count]);
189+
}
190+
151191
RCT_EXPORT_METHOD(getAll:(RCTResponseSenderBlock) callback)
152192
{
153193
[self getAllContacts:callback withThumbnails:true];
@@ -158,6 +198,11 @@ -(void) getAllContacts:(RCTResponseSenderBlock) callback
158198
[self getAllContacts:callback withThumbnails:false];
159199
}
160200

201+
RCT_EXPORT_METHOD(getCount:(RCTResponseSenderBlock) callback)
202+
{
203+
[self getAllContactsCount:callback];
204+
}
205+
161206
-(void) retrieveContactsFromAddressBook:(CNContactStore*)contactStore
162207
withThumbnails:(BOOL) withThumbnails
163208
withCallback:(RCTResponseSenderBlock) callback

0 commit comments

Comments
 (0)