mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
properly handle toggling Favorite items
This commit is contained in:
@ -41,7 +41,7 @@ ext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:3.5.5'
|
implementation 'com.simplemobiletools:commons:3.5.6'
|
||||||
implementation 'joda-time:joda-time:2.9.9'
|
implementation 'joda-time:joda-time:2.9.9'
|
||||||
|
|
||||||
//debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
|
//debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
|
||||||
|
@ -234,6 +234,7 @@ class ContactActivity : SimpleActivity() {
|
|||||||
contact_source.text = contact!!.source
|
contact_source.text = contact!!.source
|
||||||
|
|
||||||
contact_toggle_favorite.apply {
|
contact_toggle_favorite.apply {
|
||||||
|
beVisible()
|
||||||
setImageDrawable(getStarDrawable(contact!!.starred == 1))
|
setImageDrawable(getStarDrawable(contact!!.starred == 1))
|
||||||
tag = contact!!.starred
|
tag = contact!!.starred
|
||||||
applyColorFilter(config.textColor)
|
applyColorFilter(config.textColor)
|
||||||
@ -306,14 +307,9 @@ class ContactActivity : SimpleActivity() {
|
|||||||
private fun setupNewContact() {
|
private fun setupNewContact() {
|
||||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
|
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
|
||||||
supportActionBar?.title = resources.getString(R.string.new_contact)
|
supportActionBar?.title = resources.getString(R.string.new_contact)
|
||||||
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), "", 0)
|
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), "", 0, 0)
|
||||||
contact_source.text = config.lastUsedContactSource
|
contact_source.text = config.lastUsedContactSource
|
||||||
contact_source.setOnClickListener { showAccountSourcePicker() }
|
contact_source.setOnClickListener { showAccountSourcePicker() }
|
||||||
|
|
||||||
contact_toggle_favorite.apply {
|
|
||||||
tag = 0
|
|
||||||
applyColorFilter(config.textColor)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showPhotoPlaceholder() {
|
private fun showPhotoPlaceholder() {
|
||||||
@ -678,10 +674,7 @@ class ContactActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private fun isContactStarred() = contact_toggle_favorite.tag == 1
|
private fun isContactStarred() = contact_toggle_favorite.tag == 1
|
||||||
|
|
||||||
private fun getStarDrawable(on: Boolean): Drawable {
|
private fun getStarDrawable(on: Boolean) = resources.getDrawable(if (on) R.drawable.ic_star_on_big else R.drawable.ic_star_off_big)
|
||||||
val newDrawable = resources.getDrawable(if (on) R.drawable.ic_star_on else R.drawable.ic_star_off)
|
|
||||||
return newDrawable
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun trySetPhoto() {
|
private fun trySetPhoto() {
|
||||||
val items = arrayListOf(
|
val items = arrayListOf(
|
||||||
|
@ -137,8 +137,9 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
|
|||||||
}
|
}
|
||||||
contactItems.removeAll(favoritesToRemove)
|
contactItems.removeAll(favoritesToRemove)
|
||||||
|
|
||||||
val favoriteIDsToRemove = HashSet<String>()
|
val favoriteIDsToRemove = ArrayList<String>()
|
||||||
favoritesToRemove.mapTo(favoriteIDsToRemove, { it.id.toString() })
|
favoritesToRemove.mapTo(favoriteIDsToRemove, { it.contactId.toString() })
|
||||||
|
ContactsHelper(activity).removeFavorites(favoriteIDsToRemove)
|
||||||
if (contactItems.isEmpty()) {
|
if (contactItems.isEmpty()) {
|
||||||
listener?.refreshFavorites()
|
listener?.refreshFavorites()
|
||||||
finishActMode()
|
finishActMode()
|
||||||
@ -152,8 +153,9 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val newFavorites = HashSet<String>()
|
val newFavorites = ArrayList<String>()
|
||||||
selectedPositions.forEach { newFavorites.add(contactItems[it].id.toString()) }
|
selectedPositions.forEach { newFavorites.add(contactItems[it].contactId.toString()) }
|
||||||
|
ContactsHelper(activity).addFavorites(newFavorites)
|
||||||
listener?.refreshFavorites()
|
listener?.refreshFavorites()
|
||||||
finishActMode()
|
finishActMode()
|
||||||
}
|
}
|
||||||
|
@ -61,9 +61,9 @@ class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Con
|
|||||||
override fun itemLongClicked(position: Int) {}
|
override fun itemLongClicked(position: Int) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSelectedItemsSet(): HashSet<String> {
|
fun getSelectedItemsSet(): HashSet<Contact> {
|
||||||
val selectedItemsSet = HashSet<String>(selectedPositions.size)
|
val selectedItemsSet = HashSet<Contact>(selectedPositions.size)
|
||||||
selectedPositions.forEach { selectedItemsSet.add(contacts[it].id.toString()) }
|
selectedPositions.forEach { selectedItemsSet.add(contacts[it]) }
|
||||||
return selectedItemsSet
|
return selectedItemsSet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,8 +10,6 @@ import com.simplemobiletools.contacts.extensions.config
|
|||||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||||
import com.simplemobiletools.contacts.models.Contact
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
import kotlinx.android.synthetic.main.layout_select_contact.view.*
|
import kotlinx.android.synthetic.main.layout_select_contact.view.*
|
||||||
import java.util.HashSet
|
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
|
|
||||||
class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit) {
|
class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit) {
|
||||||
private var dialog: AlertDialog? = null
|
private var dialog: AlertDialog? = null
|
||||||
@ -45,22 +43,31 @@ class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
dialog = AlertDialog.Builder(activity)
|
dialog = AlertDialog.Builder(activity)
|
||||||
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
|
.setPositiveButton(R.string.ok, null)
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.create().apply {
|
.create().apply {
|
||||||
activity.setupDialogStuff(view, this)
|
activity.setupDialogStuff(view, this)
|
||||||
|
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||||
|
dialogConfirmed()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dialogConfirmed() {
|
private fun dialogConfirmed() {
|
||||||
val allDisplayedIDs = ArrayList<String>()
|
Thread {
|
||||||
allContacts.mapTo(allDisplayedIDs, { it.id.toString() })
|
val contactsHelper = ContactsHelper(activity)
|
||||||
val selectedItems = (view.select_contact_list.adapter as SelectContactsAdapter).getSelectedItemsSet()
|
val allDisplayedContacts = ArrayList<Contact>()
|
||||||
allDisplayedIDs.removeAll(selectedItems)
|
allContacts.mapTo(allDisplayedContacts, { it })
|
||||||
|
val selectedContacts = (view.select_contact_list.adapter as SelectContactsAdapter).getSelectedItemsSet()
|
||||||
|
val contactIDsToAdd = selectedContacts.map { it.contactId.toString() } as ArrayList<String>
|
||||||
|
contactsHelper.addFavorites(contactIDsToAdd)
|
||||||
|
|
||||||
val favoriteIDsToRemove = HashSet<String>()
|
allDisplayedContacts.removeAll(selectedContacts)
|
||||||
allDisplayedIDs.mapTo(favoriteIDsToRemove, { it })
|
val contactIDsToRemove = allDisplayedContacts.map { it.contactId.toString() } as ArrayList<String>
|
||||||
callback()
|
contactsHelper.removeFavorites(contactIDsToRemove)
|
||||||
dialog?.dismiss()
|
|
||||||
|
callback()
|
||||||
|
dialog?.dismiss()
|
||||||
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.helpers
|
|||||||
import android.content.ContentProviderOperation
|
import android.content.ContentProviderOperation
|
||||||
import android.content.ContentProviderResult
|
import android.content.ContentProviderResult
|
||||||
import android.content.ContentUris
|
import android.content.ContentUris
|
||||||
|
import android.content.ContentValues
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
@ -52,7 +53,8 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
val events = ArrayList<Event>()
|
val events = ArrayList<Event>()
|
||||||
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
|
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
|
||||||
val starred = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.STARRED)
|
val starred = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.STARRED)
|
||||||
val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName, starred)
|
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||||
|
val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName, starred, contactId)
|
||||||
contacts.put(id, contact)
|
contacts.put(id, contact)
|
||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
@ -77,8 +79,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val contactsSize = contacts.size()
|
val contactsSize = contacts.size()
|
||||||
val resultContacts = ArrayList<Contact>(contactsSize)
|
var resultContacts = ArrayList<Contact>(contactsSize)
|
||||||
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
|
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
|
||||||
|
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact>
|
||||||
callback(resultContacts)
|
callback(resultContacts)
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
@ -207,7 +210,8 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
val events = getEvents(id)[id] ?: ArrayList()
|
val events = getEvents(id)[id] ?: ArrayList()
|
||||||
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
|
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
|
||||||
val starred = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.STARRED)
|
val starred = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.STARRED)
|
||||||
return Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName, starred)
|
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||||
|
return Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName, starred, contactId)
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
@ -239,7 +243,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getContactSourceType(accountName: String): String {
|
private fun getContactSourceType(accountName: String): String {
|
||||||
val uri = ContactsContract.RawContacts.CONTENT_URI
|
val uri = ContactsContract.RawContacts.CONTENT_URI
|
||||||
val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_TYPE)
|
val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_TYPE)
|
||||||
val selection = "${ContactsContract.RawContacts.ACCOUNT_NAME} = ?"
|
val selection = "${ContactsContract.RawContacts.ACCOUNT_NAME} = ?"
|
||||||
@ -258,6 +262,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getContactProjection() = arrayOf(
|
private fun getContactProjection() = arrayOf(
|
||||||
|
ContactsContract.Data.CONTACT_ID,
|
||||||
ContactsContract.Data.RAW_CONTACT_ID,
|
ContactsContract.Data.RAW_CONTACT_ID,
|
||||||
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
|
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
|
||||||
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
|
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
|
||||||
@ -354,6 +359,16 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// favorite
|
||||||
|
try {
|
||||||
|
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contact.contactId.toString())
|
||||||
|
val contentValues = ContentValues(1)
|
||||||
|
contentValues.put(ContactsContract.Contacts.STARRED, contact.starred)
|
||||||
|
activity.contentResolver.update(uri, contentValues, null, null)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
activity.showErrorToast(e)
|
||||||
|
}
|
||||||
|
|
||||||
// photo
|
// photo
|
||||||
when (photoUpdateStatus) {
|
when (photoUpdateStatus) {
|
||||||
PHOTO_ADDED, PHOTO_CHANGED -> addPhoto(contact, operations)
|
PHOTO_ADDED, PHOTO_CHANGED -> addPhoto(contact, operations)
|
||||||
@ -540,6 +555,30 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addFavorites(ids: ArrayList<String>) {
|
||||||
|
toggleFavorites(ids, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeFavorites(ids: ArrayList<String>) {
|
||||||
|
toggleFavorites(ids, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toggleFavorites(ids: ArrayList<String>, areFavorites: Boolean) {
|
||||||
|
try {
|
||||||
|
val operations = ArrayList<ContentProviderOperation>()
|
||||||
|
ids.forEach {
|
||||||
|
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, it)
|
||||||
|
ContentProviderOperation.newUpdate(uri).apply {
|
||||||
|
withValue(ContactsContract.Contacts.STARRED, if (areFavorites) 1 else 0)
|
||||||
|
operations.add(build())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
activity.showErrorToast(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
|
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
|
||||||
|
|
||||||
fun deleteContacts(contacts: ArrayList<Contact>) {
|
fun deleteContacts(contacts: ArrayList<Contact>) {
|
||||||
|
@ -6,7 +6,7 @@ import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
|||||||
|
|
||||||
data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String,
|
data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String,
|
||||||
var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var events: ArrayList<Event>, var source: String,
|
var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var events: ArrayList<Event>, var source: String,
|
||||||
var starred: Int) : Comparable<Contact> {
|
var starred: Int, val contactId: Int) : Comparable<Contact> {
|
||||||
companion object {
|
companion object {
|
||||||
var sorting: Int = 0
|
var sorting: Int = 0
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,8 @@
|
|||||||
android:layout_toRightOf="@+id/contact_photo"
|
android:layout_toRightOf="@+id/contact_photo"
|
||||||
android:adjustViewBounds="true"
|
android:adjustViewBounds="true"
|
||||||
android:padding="@dimen/tiny_margin"
|
android:padding="@dimen/tiny_margin"
|
||||||
android:src="@drawable/ic_star_off"/>
|
android:src="@drawable/ic_star_off_big"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/contact_actions_holder"
|
android:id="@+id/contact_actions_holder"
|
||||||
|
Reference in New Issue
Block a user