diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ContactActivity.kt index d0067c4e..47a00896 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ContactActivity.kt @@ -77,8 +77,9 @@ abstract class ContactActivity : SimpleActivity() { fun deleteContact() { ConfirmationDialog(this) { if (contact != null) { - ContactsHelper(this).deleteContact(contact!!) - finish() + ContactsHelper(this).deleteContact(contact!!, false) { + finish() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt index 821ffbe9..06a6d7f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt @@ -992,10 +992,14 @@ class EditContactActivity : ContactActivity() { if (ContactsHelper(this@EditContactActivity).insertContact(contact!!)) { if (deleteCurrentContact) { contact!!.source = originalContactSource - ContactsHelper(this).deleteContact(contact!!) + ContactsHelper(this).deleteContact(contact!!, false) { + setResult(Activity.RESULT_OK) + finish() + } + } else { + setResult(Activity.RESULT_OK) + finish() } - setResult(Activity.RESULT_OK) - finish() } else { toast(R.string.unknown_error_occurred) } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt index b8c10d60..0122fdb3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt @@ -519,8 +519,9 @@ class ViewContactActivity : ContactActivity() { val message = "${getString(R.string.proceed_with_deletion)}$addition" ConfirmationDialog(this, message) { if (contact != null) { - ContactsHelper(this).deleteContact(contact!!) - finish() + ContactsHelper(this).deleteContact(contact!!, true) { + finish() + } } } } 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 8085c5ed..c2474b99 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 @@ -1524,17 +1524,27 @@ class ContactsHelper(val context: Context) { LocalContactsHelper(context).toggleFavorites(localContacts, addToFavorites) } - fun deleteContact(originalContact: Contact) { - getDuplicatesOfContact(originalContact, true) { contacts -> - deleteContacts(contacts) + fun deleteContact(originalContact: Contact, deleteClones: Boolean = false, callback: (success: Boolean) -> Unit) { + ensureBackgroundThread { + if (deleteClones) { + getDuplicatesOfContact(originalContact, true) { contacts -> + if (deleteContacts(contacts)) { + callback(true) + } + } + } else { + if (deleteContacts(arrayListOf(originalContact))) { + callback(true) + } + } } } - fun deleteContacts(contacts: ArrayList) { + fun deleteContacts(contacts: ArrayList): Boolean { val localContacts = contacts.filter { it.isPrivate() }.map { it.id.toLong() }.toMutableList() LocalContactsHelper(context).deleteContactIds(localContacts) - try { + return try { val operations = ArrayList() val selection = "${ContactsContract.RawContacts._ID} = ?" contacts.filter { !it.isPrivate() }.forEach { @@ -1553,8 +1563,10 @@ class ContactsHelper(val context: Context) { if (context.hasPermission(PERMISSION_WRITE_CONTACTS)) { context.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) } + true } catch (e: Exception) { context.showErrorToast(e) + false } }