From 8f890f4f5410f764c5a1ae9118b401ac158913bd Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 15 Apr 2020 12:47:52 +0200 Subject: [PATCH] catch and show exceptions thrown at modifying groups --- .../contacts/pro/helpers/ContactsHelper.kt | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 5aa11b2b..b3f95038 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -1217,22 +1217,22 @@ class ContactsHelper(val context: Context) { } fun addContactsToGroup(contacts: ArrayList, groupId: Long) { - val operations = ArrayList() - contacts.forEach { - ContentProviderOperation.newInsert(Data.CONTENT_URI).apply { - withValue(Data.RAW_CONTACT_ID, it.id) - withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE) - withValue(GroupMembership.GROUP_ROW_ID, groupId) - operations.add(build()) - } - - if (operations.size % BATCH_SIZE == 0) { - context.contentResolver.applyBatch(AUTHORITY, operations) - operations.clear() - } - } - try { + val operations = ArrayList() + contacts.forEach { + ContentProviderOperation.newInsert(Data.CONTENT_URI).apply { + withValue(Data.RAW_CONTACT_ID, it.id) + withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE) + withValue(GroupMembership.GROUP_ROW_ID, groupId) + operations.add(build()) + } + + if (operations.size % BATCH_SIZE == 0) { + context.contentResolver.applyBatch(AUTHORITY, operations) + operations.clear() + } + } + context.contentResolver.applyBatch(AUTHORITY, operations) } catch (e: Exception) { context.showErrorToast(e) @@ -1240,21 +1240,25 @@ class ContactsHelper(val context: Context) { } fun removeContactsFromGroup(contacts: ArrayList, groupId: Long) { - val operations = ArrayList() - contacts.forEach { - ContentProviderOperation.newDelete(Data.CONTENT_URI).apply { - val selection = "${Data.CONTACT_ID} = ? AND ${Data.MIMETYPE} = ? AND ${Data.DATA1} = ?" - val selectionArgs = arrayOf(it.contactId.toString(), GroupMembership.CONTENT_ITEM_TYPE, groupId.toString()) - withSelection(selection, selectionArgs) - operations.add(build()) - } + try { + val operations = ArrayList() + contacts.forEach { + ContentProviderOperation.newDelete(Data.CONTENT_URI).apply { + val selection = "${Data.CONTACT_ID} = ? AND ${Data.MIMETYPE} = ? AND ${Data.DATA1} = ?" + val selectionArgs = arrayOf(it.contactId.toString(), GroupMembership.CONTENT_ITEM_TYPE, groupId.toString()) + withSelection(selection, selectionArgs) + operations.add(build()) + } - if (operations.size % BATCH_SIZE == 0) { - context.contentResolver.applyBatch(AUTHORITY, operations) - operations.clear() + if (operations.size % BATCH_SIZE == 0) { + context.contentResolver.applyBatch(AUTHORITY, operations) + operations.clear() + } } + context.contentResolver.applyBatch(AUTHORITY, operations) + } catch (e: Exception) { + context.showErrorToast(e) } - context.contentResolver.applyBatch(AUTHORITY, operations) } fun insertContact(contact: Contact): Boolean {