properly handle Favorite toggling of local private contacts

This commit is contained in:
tibbi 2018-02-11 21:45:25 +01:00
parent 21cc46de54
commit 0651a49cd1
4 changed files with 30 additions and 16 deletions

View File

@ -145,9 +145,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
} }
contactItems.removeAll(favoritesToRemove) contactItems.removeAll(favoritesToRemove)
val favoriteIDsToRemove = ArrayList<String>() ContactsHelper(activity).removeFavorites(favoritesToRemove)
favoritesToRemove.mapTo(favoriteIDsToRemove, { it.contactId.toString() })
ContactsHelper(activity).removeFavorites(favoriteIDsToRemove)
if (contactItems.isEmpty()) { if (contactItems.isEmpty()) {
listener?.refreshFavorites() listener?.refreshFavorites()
finishActMode() finishActMode()
@ -161,8 +159,8 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
return return
} }
val newFavorites = ArrayList<String>() val newFavorites = ArrayList<Contact>()
selectedPositions.forEach { newFavorites.add(contactItems[it].contactId.toString()) } selectedPositions.forEach { newFavorites.add(contactItems[it]) }
ContactsHelper(activity).addFavorites(newFavorites) ContactsHelper(activity).addFavorites(newFavorites)
listener?.refreshFavorites() listener?.refreshFavorites()
finishActMode() finishActMode()

View File

@ -59,12 +59,11 @@ class AddFavoritesDialog(val activity: SimpleActivity, private val callback: ()
val allDisplayedContacts = ArrayList<Contact>() val allDisplayedContacts = ArrayList<Contact>()
allContacts.mapTo(allDisplayedContacts, { it }) allContacts.mapTo(allDisplayedContacts, { it })
val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet() val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet()
val contactIDsToAdd = selectedContacts.map { it.contactId.toString() } as ArrayList<String> val contactsToAdd = selectedContacts.map { it } as ArrayList<Contact>
contactsHelper.addFavorites(contactIDsToAdd) contactsHelper.addFavorites(contactsToAdd)
allDisplayedContacts.removeAll(selectedContacts) allDisplayedContacts.removeAll(selectedContacts)
val contactIDsToRemove = allDisplayedContacts.map { it.contactId.toString() } as ArrayList<String> contactsHelper.removeFavorites(allDisplayedContacts)
contactsHelper.removeFavorites(contactIDsToRemove)
callback() callback()
dialog?.dismiss() dialog?.dismiss()

View File

@ -610,24 +610,27 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return "" return ""
} }
fun addFavorites(ids: ArrayList<String>) { fun addFavorites(contacts: ArrayList<Contact>) {
toggleFavorites(ids, true) toggleLocalFavorites(contacts, true)
toggleFavorites(contacts, true)
} }
fun removeFavorites(ids: ArrayList<String>) { fun removeFavorites(contacts: ArrayList<Contact>) {
toggleFavorites(ids, false) toggleLocalFavorites(contacts, false)
toggleFavorites(contacts, false)
} }
private fun toggleFavorites(ids: ArrayList<String>, areFavorites: Boolean) { private fun toggleFavorites(contacts: ArrayList<Contact>, addToFavorites: Boolean) {
val applyBatchSize = 100 val applyBatchSize = 100
try { try {
val operations = ArrayList<ContentProviderOperation>() val operations = ArrayList<ContentProviderOperation>()
ids.forEach { contacts.filter { it.source != SMT_PRIVATE }.map { it.contactId.toString() }.forEach {
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, it) val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, it)
ContentProviderOperation.newUpdate(uri).apply { 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()) operations.add(build())
} }
if (operations.size % applyBatchSize == 0) { if (operations.size % applyBatchSize == 0) {
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
operations.clear() operations.clear()
@ -639,6 +642,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
} }
} }
private fun toggleLocalFavorites(contacts: ArrayList<Contact>, addToFavorites: Boolean) {
val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray()
activity.dbHelper.toggleFavorites(localContacts, addToFavorites)
}
fun deleteContact(contact: Contact) { fun deleteContact(contact: Contact) {
if (contact.source == SMT_PRIVATE) { if (contact.source == SMT_PRIVATE) {
activity.dbHelper.deleteContact(contact.id) activity.dbHelper.deleteContact(contact.id)

View File

@ -89,6 +89,15 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
} }
fun toggleFavorites(ids: Array<String>, 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<String>? = null): ArrayList<Contact> { fun getContacts(selection: String? = null, selectionArgs: Array<String>? = null): ArrayList<Contact> {
val contacts = ArrayList<Contact>() val contacts = ArrayList<Contact>()
val projection = arrayOf(COL_ID, COL_FIRST_NAME, COL_MIDDLE_NAME, COL_SURNAME, COL_PHONE_NUMBERS, COL_EMAILS, COL_EVENTS, COL_STARRED) val projection = arrayOf(COL_ID, COL_FIRST_NAME, COL_MIDDLE_NAME, COL_SURNAME, COL_PHONE_NUMBERS, COL_EMAILS, COL_EVENTS, COL_STARRED)