Skip to content

Commit

Permalink
updateContact update phone numbers and emails instead of insert [Andr…
Browse files Browse the repository at this point in the history
…oid] (#348)

* update email instead of insert
* removes all numbers of contact and then adds the passed ones
* emails now added but removed and then added, so removes duplicates
* remove from buglist
  • Loading branch information
luucv authored and morenoh149 committed Jan 18, 2019
1 parent 96aef2b commit 0431082
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ Update reference contacts by their recordID (as returned by the OS in getContact
There are issues with updating contacts on Android:
1. custom labels get overwritten to "Other",
1. postal address update code doesn't exist. (it exists for addContact)
1. phoneNumbers and emails insert instead of update.
See https://github.com/rt2zz/react-native-contacts/issues/332#issuecomment-455675041 for current discussions.
## Delete Contacts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public byte[] toByteArray(Bitmap bitmap) {
*/
@ReactMethod
public void updateContact(ReadableMap contact, Callback callback) {

String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null;
String rawContactId = contact.hasKey("rawContactId") ? contact.getString("rawContactId") : null;

Expand Down Expand Up @@ -573,19 +573,21 @@ public void updateContact(ReadableMap contact, Callback callback) {

op.withYieldAllowed(true);

// remove existing phoneNumbers first
op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.CONTACT_ID + " = ?",
new String[]{String.valueOf(CommonDataKinds.Phone.CONTENT_ITEM_TYPE), String.valueOf(recordID)}
);
ops.add(op.build());

// add passed phonenumbers
for (int i = 0; i < numOfPhones; i++) {
if (phoneIds[i] == null) {
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Phone.NUMBER, phones[i])
.withValue(CommonDataKinds.Phone.TYPE, phonesLabels[i]);
} else {
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(phoneIds[i])})
.withValue(CommonDataKinds.Phone.NUMBER, phones[i])
.withValue(CommonDataKinds.Phone.TYPE, phonesLabels[i]);
}
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Phone.NUMBER, phones[i])
.withValue(CommonDataKinds.Phone.TYPE, phonesLabels[i]);
ops.add(op.build());
}

Expand All @@ -603,19 +605,21 @@ public void updateContact(ReadableMap contact, Callback callback) {
ops.add(op.build());
}

// remove existing emails first
op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.CONTACT_ID + " = ?",
new String[]{String.valueOf(CommonDataKinds.Email.CONTENT_ITEM_TYPE), String.valueOf(recordID)}
);
ops.add(op.build());

// add passed email addresses
for (int i = 0; i < numOfEmails; i++) {
if (emailIds[i] == null) {
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Email.ADDRESS, emails[i])
.withValue(CommonDataKinds.Email.TYPE, emailsLabels[i]);
} else {
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(emailIds[i])})
.withValue(CommonDataKinds.Email.ADDRESS, emails[i])
.withValue(CommonDataKinds.Email.TYPE, emailsLabels[i]);
}
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Email.ADDRESS, emails[i])
.withValue(CommonDataKinds.Email.TYPE, emailsLabels[i]);
ops.add(op.build());
}

Expand Down

0 comments on commit 0431082

Please sign in to comment.