Skip to content

Commit

Permalink
Merge pull request #434 from pitbot/master
Browse files Browse the repository at this point in the history
Add contacts count feature
  • Loading branch information
morenoh149 authored Sep 26, 2019
2 parents fcd6544 + b317836 commit 2c6532b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ protected Void doInBackground(final Void ... params) {
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

@ReactMethod
public void getCount(final Callback callback) {
AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(final Void ... params) {
Context context = getReactApplicationContext();
ContentResolver cr = context.getContentResolver();

ContactsProvider contactsProvider = new ContactsProvider(cr);
Integer contacts = contactsProvider.getContactsCount();

callback.invoke(contacts);
return null;
}
};
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

/**
* Retrieves contacts matching String.
* Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ public WritableMap getContactById(String contactId) {
return null;
}

public Integer getContactsCount() {
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int count = cursor.getCount();

return count;
}

public WritableArray getContacts() {
Map<String, Contact> justMe;
{
Expand Down
12 changes: 10 additions & 2 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default class App extends Component<Props> {
this.search = this.search.bind(this);

this.state = {
contacts: []
contacts: [],
searchPlaceholder: "Search"
};
}

Expand All @@ -56,6 +57,10 @@ export default class App extends Component<Props> {
this.setState({ contacts });
}
});

Contacts.getCount(count => {
this.setState({ searchPlaceholder: `Search ${count} contacts` });
});
}

search(text) {
Expand Down Expand Up @@ -92,7 +97,10 @@ export default class App extends Component<Props> {
}}
/>
</View>
<SearchBar onChangeText={this.search} />
<SearchBar
searchPlaceholder={this.state.searchPlaceholder}
onChangeText={this.search}
/>
<ScrollView style={{ flex: 1 }}>
{this.state.contacts.map(contact => {
return (
Expand Down
45 changes: 45 additions & 0 deletions ios/RCTContacts/RCTContacts.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,46 @@ -(void) getAllContacts:(RCTResponseSenderBlock) callback
[self retrieveContactsFromAddressBook:contactStore withThumbnails:withThumbnails withCallback:callback];
}

-(void) getAllContactsCount:(RCTResponseSenderBlock) callback
{
CNContactStore* contactStore = [self contactsStore:callback];
if(!contactStore)
return;

NSMutableArray *contacts = [[NSMutableArray alloc] init];

NSError* contactError;
[contactStore containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[contactStore.defaultContainerIdentifier]] error:&contactError];


NSMutableArray *keysToFetch = [[NSMutableArray alloc]init];
[keysToFetch addObjectsFromArray:@[
CNContactEmailAddressesKey,
CNContactPhoneNumbersKey,
CNContactFamilyNameKey,
CNContactGivenNameKey,
CNContactMiddleNameKey,
CNContactPostalAddressesKey,
CNContactOrganizationNameKey,
CNContactJobTitleKey,
CNContactImageDataAvailableKey,
CNContactUrlAddressesKey,
CNContactBirthdayKey
]];

CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
BOOL success = [contactStore enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){
NSDictionary *contactDict = [self contactToDictionary: contact withThumbnails:false];
[contacts addObject:contactDict];
}];

int contactsCount = [contacts count];

NSNumber *count = [NSNumber numberWithInt:contactsCount];

callback(@[count]);
}

RCT_EXPORT_METHOD(getAll:(RCTResponseSenderBlock) callback)
{
[self getAllContacts:callback withThumbnails:true];
Expand All @@ -158,6 +198,11 @@ -(void) getAllContacts:(RCTResponseSenderBlock) callback
[self getAllContacts:callback withThumbnails:false];
}

RCT_EXPORT_METHOD(getCount:(RCTResponseSenderBlock) callback)
{
[self getAllContactsCount:callback];
}

-(void) retrieveContactsFromAddressBook:(CNContactStore*)contactStore
withThumbnails:(BOOL) withThumbnails
withCallback:(RCTResponseSenderBlock) callback
Expand Down

0 comments on commit 2c6532b

Please sign in to comment.