From 0651a49cd1b55c286535e7ff0fb8c598af6fa3c2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 11 Feb 2018 21:45:25 +0100 Subject: [PATCH] properly handle Favorite toggling of local private contacts --- .../contacts/adapters/ContactsAdapter.kt | 8 +++---- .../contacts/dialogs/AddFavoritesDialog.kt | 7 +++--- .../contacts/helpers/ContactsHelper.kt | 22 +++++++++++++------ .../contacts/helpers/DBHelper.kt | 9 ++++++++ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt index 9d49925e..681f1d27 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt @@ -145,9 +145,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList() - favoritesToRemove.mapTo(favoriteIDsToRemove, { it.contactId.toString() }) - ContactsHelper(activity).removeFavorites(favoriteIDsToRemove) + ContactsHelper(activity).removeFavorites(favoritesToRemove) if (contactItems.isEmpty()) { listener?.refreshFavorites() finishActMode() @@ -161,8 +159,8 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList() - selectedPositions.forEach { newFavorites.add(contactItems[it].contactId.toString()) } + val newFavorites = ArrayList() + selectedPositions.forEach { newFavorites.add(contactItems[it]) } ContactsHelper(activity).addFavorites(newFavorites) listener?.refreshFavorites() finishActMode() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/AddFavoritesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/AddFavoritesDialog.kt index f31f0c4a..e0992e10 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/AddFavoritesDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/AddFavoritesDialog.kt @@ -59,12 +59,11 @@ class AddFavoritesDialog(val activity: SimpleActivity, private val callback: () val allDisplayedContacts = ArrayList() allContacts.mapTo(allDisplayedContacts, { it }) val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet() - val contactIDsToAdd = selectedContacts.map { it.contactId.toString() } as ArrayList - contactsHelper.addFavorites(contactIDsToAdd) + val contactsToAdd = selectedContacts.map { it } as ArrayList + contactsHelper.addFavorites(contactsToAdd) allDisplayedContacts.removeAll(selectedContacts) - val contactIDsToRemove = allDisplayedContacts.map { it.contactId.toString() } as ArrayList - contactsHelper.removeFavorites(contactIDsToRemove) + contactsHelper.removeFavorites(allDisplayedContacts) callback() dialog?.dismiss() 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 a70ab7aa..cef49fb6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -610,24 +610,27 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return "" } - fun addFavorites(ids: ArrayList) { - toggleFavorites(ids, true) + fun addFavorites(contacts: ArrayList) { + toggleLocalFavorites(contacts, true) + toggleFavorites(contacts, true) } - fun removeFavorites(ids: ArrayList) { - toggleFavorites(ids, false) + fun removeFavorites(contacts: ArrayList) { + toggleLocalFavorites(contacts, false) + toggleFavorites(contacts, false) } - private fun toggleFavorites(ids: ArrayList, areFavorites: Boolean) { + private fun toggleFavorites(contacts: ArrayList, addToFavorites: Boolean) { val applyBatchSize = 100 try { val operations = ArrayList() - ids.forEach { + contacts.filter { it.source != SMT_PRIVATE }.map { it.contactId.toString() }.forEach { val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, it) ContentProviderOperation.newUpdate(uri).apply { - withValue(ContactsContract.Contacts.STARRED, if (areFavorites) 1 else 0) + withValue(ContactsContract.Contacts.STARRED, if (addToFavorites) 1 else 0) operations.add(build()) } + if (operations.size % applyBatchSize == 0) { activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) operations.clear() @@ -639,6 +642,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } + private fun toggleLocalFavorites(contacts: ArrayList, addToFavorites: Boolean) { + val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray() + activity.dbHelper.toggleFavorites(localContacts, addToFavorites) + } + fun deleteContact(contact: Contact) { if (contact.source == SMT_PRIVATE) { activity.dbHelper.deleteContact(contact.id) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt index 59a8148b..10ebc310 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -89,6 +89,15 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } } + fun toggleFavorites(ids: Array, addToFavorites: Boolean) { + val contactValues = ContentValues() + contactValues.put(COL_STARRED, if (addToFavorites) 1 else 0) + + val args = TextUtils.join(", ", ids) + val selection = "$COL_ID IN ($args)" + mDb.update(CONTACTS_TABLE_NAME, contactValues, selection, null) + } + fun getContacts(selection: String? = null, selectionArgs: Array? = null): ArrayList { val contacts = ArrayList() val projection = arrayOf(COL_ID, COL_FIRST_NAME, COL_MIDDLE_NAME, COL_SURNAME, COL_PHONE_NUMBERS, COL_EMAILS, COL_EVENTS, COL_STARRED)