fix #218, improve duplicate contact filtering, show the one with more info

This commit is contained in:
tibbi 2018-09-03 23:41:16 +02:00
parent 03b739f01e
commit de387a938d
2 changed files with 18 additions and 6 deletions

View File

@ -42,12 +42,22 @@ class ContactsHelper(val activity: Activity) {
}
val contactsSize = contacts.size()
var resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
var tempContacts = ArrayList<Contact>(contactsSize)
val resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(tempContacts) { contacts.valueAt(it) }
if (activity.config.filterDuplicates) {
resultContacts = resultContacts.distinctBy {
tempContacts = tempContacts.distinctBy {
it.getHashToCompare()
} as ArrayList<Contact>
tempContacts.groupBy { "${it.getFullName().toLowerCase()}${it.emails}" }.values.forEach {
if (it.size == 1) {
resultContacts.add(it.first())
} else {
val sorted = it.sortedByDescending { it.getStringToCompare().length }
resultContacts.add(sorted.first())
}
}
}
// groups are obtained with contactID, not rawID, so assign them to proper contacts like this

View File

@ -92,7 +92,7 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
}
}
fun getHashToCompare(): Int {
fun getStringToCompare(): String {
val newPhoneNumbers = ArrayList<PhoneNumber>()
phoneNumbers.mapTo(newPhoneNumbers) { PhoneNumber(it.value.replace(PHONE_NUMBER_PATTERN.toRegex(), ""), 0, "") }
@ -100,7 +100,9 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
emails.mapTo(newEmails) { Email(it.value, 0, "") }
return copy(id = 0, prefix = "", firstName = getFullName().toLowerCase(), middleName = "", surname = "", suffix = "", nickname = "", photoUri = "",
phoneNumbers = newPhoneNumbers, events = ArrayList(), addresses = ArrayList(), emails = newEmails, source = "", starred = 0,
contactId = 0, thumbnailUri = "", notes = "", groups = ArrayList(), websites = ArrayList(), organization = Organization("", "")).hashCode()
phoneNumbers = phoneNumbers, events = ArrayList(), addresses = ArrayList(), emails = newEmails, source = "", starred = 0,
contactId = 0, thumbnailUri = "", notes = "", groups = ArrayList(), websites = ArrayList(), organization = Organization("", "")).toString()
}
fun getHashToCompare() = getStringToCompare().hashCode()
}