Skip to content

Commit 6e9ed1a

Browse files
committed
Add searchContacts.
- Pass a string to `searchContacts` and it will search the Display Name for a partial hit of the search text. - For example, if I search “Berg” it will bring up “Tim Bergling”. Also: - Increased padding in ExampleApp. - Added “Search Name in Contacts” to ExampleApp. Fixes joshuapinter#58.
1 parent 0b48735 commit 6e9ed1a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

ExampleApp/App.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
StyleSheet,
1313
Switch,
1414
Text,
15+
TextInput,
1516
View
1617
} from 'react-native';
1718

@@ -68,6 +69,11 @@ export default class App extends Component<{}> {
6869
<Button title="Get Contacts" onPress={ () => this._getContacts() } />
6970
</View>
7071

72+
<View style={ styles.button }>
73+
<TextInput value={ this.state.searchText } onChangeText={ text => this.setState( { searchText: text } ) } />
74+
<Button title="Search Name in Contacts" onPress={ () => this._searchContacts( this.state.searchText ) } />
75+
</View>
76+
7177
<FlatList
7278
style={styles.contacts}
7379
data={this.state.contacts}
@@ -122,7 +128,19 @@ export default class App extends Component<{}> {
122128
console.error(error);
123129
}
124130
else {
125-
// console.log('contacts[0].fullName', contacts[0].fullName);
131+
this.setState( { contacts } );
132+
}
133+
});
134+
}
135+
}
136+
137+
_searchContacts( searchText ) {
138+
if (this.state.canUserAccessContacts) {
139+
Contacts.searchContacts( searchText, (error, contacts) => {
140+
if (error) {
141+
console.error(error);
142+
}
143+
else {
126144
this.setState( { contacts } );
127145
}
128146
});
@@ -134,7 +152,7 @@ const styles = StyleSheet.create({
134152
container: {
135153
flex: 1,
136154
backgroundColor: '#F5FCFF',
137-
paddingTop: 40,
155+
padding: 40,
138156
},
139157
welcome: {
140158
fontSize: 20,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
|------------------------------------|:---:|:-------:|:-------:| ----- |
1414
| `getContact` || 🚫 | 🚫 | |
1515
| `getContacts` ||| 🚫 | |
16-
| `searchContacts` || 🚫 | 🚫 | |
16+
| `searchContacts` || | 🚫 | |
1717
| `addContact` || 🚫 | 🚫 | |
1818
| `updateContact` || 🚫 | 🚫 | |
1919
| `deleteContact` || 🚫 | 🚫 | |

android/src/main/java/com/joshuapinter/RNUnifiedContacts/RNUnifiedContactsModule.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,28 @@ public void getContacts(final Callback callback) {
175175
@ReactMethod
176176
public void searchContacts( String searchText, Callback callback ) {
177177

178-
// ToDo: Use searchText to filter results
179178
WritableArray contacts = Arguments.createArray();
180179

181180
contentResolver = getCurrentActivity().getContentResolver();
182181

182+
String whereString = null;
183+
String[] whereParams = null;
184+
185+
if ( searchText != null && !searchText.equals( "" ) ) {
186+
whereString = "display_name LIKE ?";
187+
whereParams = new String[]{ "%" + searchText + "%" };
188+
}
189+
183190
Cursor contactCursor = contentResolver.query(
184-
ContactsContract.Contacts.CONTENT_URI,
185-
null, null, null, null);
191+
ContactsContract.Data.CONTENT_URI,
192+
null,
193+
whereString,
194+
whereParams,
195+
null );
186196

187197
while( contactCursor.moveToNext() ) {
188198

189-
int contactId = getIntFromCursor( contactCursor, ContactsContract.Contacts._ID );
199+
int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );
190200

191201
WritableMap contact = getContactDetailsFromContactId(contactId);
192202

@@ -198,7 +208,6 @@ public void searchContacts( String searchText, Callback callback ) {
198208
// ToDo: Add check for error and return error callback instead
199209
// i.e. callback.invoke(error, null)
200210

201-
202211
// Success
203212
callback.invoke(null, contacts);
204213
}
@@ -299,8 +308,6 @@ private WritableMap getContactDetailsFromContactId(int contactId) {
299308
String note = getNoteFromContact(contactId);
300309
contactMap.putString( "note", note );
301310

302-
// Log.w("Test13", contactMap.toString());
303-
304311
return contactMap;
305312
}
306313

0 commit comments

Comments
 (0)