try being smarter at showing distinct contacts, dont check just contact id

This commit is contained in:
tibbi 2018-04-29 22:07:41 +02:00
parent e7ee92b8c7
commit 763b04faa3
8 changed files with 24 additions and 22 deletions

View file

@ -99,9 +99,7 @@ class SelectContactActivity : SimpleActivity() {
} as ArrayList<Contact>
val contactSources = config.displayContactSources
if (!config.showAllContacts()) {
contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
Contact.sorting = config.sorting
Contact.startWithSurname = config.startNameWithSurname

View file

@ -21,7 +21,7 @@ class FilterContactSourcesAdapter(val activity: SimpleActivity, private val cont
init {
contactSources.forEachIndexed { index, contactSource ->
if (activity.config.showAllContacts() || displayContactSources.contains(contactSource.name)) {
if (displayContactSources.contains(contactSource.name)) {
selectedPositions.add(index)
}

View file

@ -19,9 +19,7 @@ class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayL
var allContacts = initialContacts
if (selectContacts == null) {
val contactSources = activity.config.displayContactSources
if (!activity.config.showAllContacts()) {
allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
initiallySelectedContacts = allContacts.filter { it.starred == 1 } as ArrayList<Contact>
} else {

View file

@ -99,15 +99,11 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
contacts.sort()
allContacts = contacts
val filtered = if (this is GroupsFragment) {
contacts
} else if (this is FavoritesFragment) {
contacts.filter { it.starred == 1 } as ArrayList<Contact>
} else {
val contactSources = config.displayContactSources
if (config.showAllContacts()) {
contacts
} else {
val filtered = when {
this is GroupsFragment -> contacts
this is FavoritesFragment -> contacts.filter { it.starred == 1 } as ArrayList<Contact>
else -> {
val contactSources = config.displayContactSources
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
}

View file

@ -12,8 +12,6 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getStringSet(DISPLAY_CONTACT_SOURCES, hashSetOf("-1"))
set(displayContactSources) = prefs.edit().remove(DISPLAY_CONTACT_SOURCES).putStringSet(DISPLAY_CONTACT_SOURCES, displayContactSources).apply()
fun showAllContacts() = displayContactSources.size == 1 && displayContactSources.first() == "-1"
var showContactThumbnails: Boolean
get() = prefs.getBoolean(SHOW_CONTACT_THUMBNAILS, true)
set(showContactThumbnails) = prefs.edit().putBoolean(SHOW_CONTACT_THUMBNAILS, showContactThumbnails).apply()

View file

@ -35,7 +35,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val contactsSize = contacts.size()
var resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact>
resultContacts = resultContacts.distinctBy {
it.getHashToCompare()
} as ArrayList<Contact>
// groups are obtained with contactID, not rawID, so assign them to proper contacts like this
val groups = getContactGroups(getStoredGroups())
@ -88,6 +90,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val websites = ArrayList<String>()
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, photoUri, number, emails, addresses, events,
accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites)
contacts.put(id, contact)
} while (cursor.moveToNext())
}
@ -1214,16 +1217,14 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
activity.dbHelper.deleteContacts(localContacts)
try {
val contactIDs = HashSet<String>()
val operations = ArrayList<ContentProviderOperation>()
val selection = "${ContactsContract.Data.CONTACT_ID} = ?"
contacts.filter { it.source != SMT_PRIVATE }.forEach {
ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply {
val selectionArgs = arrayOf(it.contactId.toString())
withSelection(selection, selectionArgs)
operations.add(this.build())
operations.add(build())
}
contactIDs.add(it.id.toString())
}
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)

View file

@ -119,6 +119,10 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun deleteContact(id: Int) = deleteContacts(arrayOf(id.toString()))
fun deleteContacts(ids: Array<String>) {
if (ids.isEmpty()) {
return
}
val args = TextUtils.join(", ", ids)
val selection = "$CONTACTS_TABLE_NAME.$COL_ID IN ($args)"
mDb.delete(CONTACTS_TABLE_NAME, selection, null)

View file

@ -12,6 +12,7 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
companion object {
var sorting = 0
var startWithSurname = false
val pattern = "\\D+".toRegex()
}
override fun compareTo(other: Contact): Int {
@ -81,4 +82,10 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
fullName
}
}
fun getHashToCompare(): Int {
val newPhoneNumbers = ArrayList<PhoneNumber>()
phoneNumbers.mapTo(newPhoneNumbers, { PhoneNumber(it.value.replace(pattern, ""), 0) })
return copy(id = 0, phoneNumbers = newPhoneNumbers).hashCode()
}
}