mirror of
				https://github.com/SimpleMobileTools/Simple-Contacts.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	properly handle Favorite toggling of local private contacts
This commit is contained in:
		| @@ -145,9 +145,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co | ||||
|         } | ||||
|         contactItems.removeAll(favoritesToRemove) | ||||
|  | ||||
|         val favoriteIDsToRemove = ArrayList<String>() | ||||
|         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<Co | ||||
|             return | ||||
|         } | ||||
|  | ||||
|         val newFavorites = ArrayList<String>() | ||||
|         selectedPositions.forEach { newFavorites.add(contactItems[it].contactId.toString()) } | ||||
|         val newFavorites = ArrayList<Contact>() | ||||
|         selectedPositions.forEach { newFavorites.add(contactItems[it]) } | ||||
|         ContactsHelper(activity).addFavorites(newFavorites) | ||||
|         listener?.refreshFavorites() | ||||
|         finishActMode() | ||||
|   | ||||
| @@ -59,12 +59,11 @@ class AddFavoritesDialog(val activity: SimpleActivity, private val callback: () | ||||
|             val allDisplayedContacts = ArrayList<Contact>() | ||||
|             allContacts.mapTo(allDisplayedContacts, { it }) | ||||
|             val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet() | ||||
|             val contactIDsToAdd = selectedContacts.map { it.contactId.toString() } as ArrayList<String> | ||||
|             contactsHelper.addFavorites(contactIDsToAdd) | ||||
|             val contactsToAdd = selectedContacts.map { it } as ArrayList<Contact> | ||||
|             contactsHelper.addFavorites(contactsToAdd) | ||||
|  | ||||
|             allDisplayedContacts.removeAll(selectedContacts) | ||||
|             val contactIDsToRemove = allDisplayedContacts.map { it.contactId.toString() } as ArrayList<String> | ||||
|             contactsHelper.removeFavorites(contactIDsToRemove) | ||||
|             contactsHelper.removeFavorites(allDisplayedContacts) | ||||
|  | ||||
|             callback() | ||||
|             dialog?.dismiss() | ||||
|   | ||||
| @@ -610,24 +610,27 @@ class ContactsHelper(val activity: BaseSimpleActivity) { | ||||
|         return "" | ||||
|     } | ||||
|  | ||||
|     fun addFavorites(ids: ArrayList<String>) { | ||||
|         toggleFavorites(ids, true) | ||||
|     fun addFavorites(contacts: ArrayList<Contact>) { | ||||
|         toggleLocalFavorites(contacts, true) | ||||
|         toggleFavorites(contacts, true) | ||||
|     } | ||||
|  | ||||
|     fun removeFavorites(ids: ArrayList<String>) { | ||||
|         toggleFavorites(ids, false) | ||||
|     fun removeFavorites(contacts: ArrayList<Contact>) { | ||||
|         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 | ||||
|         try { | ||||
|             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) | ||||
|                 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<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) { | ||||
|         if (contact.source == SMT_PRIVATE) { | ||||
|             activity.dbHelper.deleteContact(contact.id) | ||||
|   | ||||
| @@ -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> { | ||||
|         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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user