From 4fca6ce711e4a7551f9b2131e0995f9dc0641898 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 20 Mar 2018 12:22:40 +0100 Subject: [PATCH] handle batch removing contacts from groups --- .../contacts/activities/GroupContactsActivity.kt | 1 + .../contacts/helpers/ContactsHelper.kt | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt index 0182f77d..9a3dd388 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt @@ -33,6 +33,7 @@ class GroupContactsActivity : SimpleActivity() { SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts -> ContactsHelper(this).apply { addContactsToGroup(addedContacts, group.id) + removeContactsFromGroup(removedContacts, group.id) } refreshContacts() } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 0503f562..8a86c144 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -760,6 +760,19 @@ class ContactsHelper(val activity: BaseSimpleActivity) { activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) } + fun removeContactsFromGroup(contacts: ArrayList, groupId: Long) { + val operations = ArrayList() + contacts.forEach { + ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { + val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.DATA1} = ?" + val selectionArgs = arrayOf(it.id.toString(), CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, groupId.toString()) + withSelection(selection, selectionArgs) + operations.add(build()) + } + } + activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) + } + fun insertContact(contact: Contact): Boolean { return if (contact.source == SMT_PRIVATE) { insertLocalContact(contact)