diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index bc3699c0..9fcf2bb1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -42,12 +42,22 @@ class ContactsHelper(val activity: Activity) { } val contactsSize = contacts.size() - var resultContacts = ArrayList(contactsSize) - (0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) } + var tempContacts = ArrayList(contactsSize) + val resultContacts = ArrayList(contactsSize) + (0 until contactsSize).mapTo(tempContacts) { contacts.valueAt(it) } if (activity.config.filterDuplicates) { - resultContacts = resultContacts.distinctBy { + tempContacts = tempContacts.distinctBy { it.getHashToCompare() } as ArrayList + + 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 diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt index ccf31bca..fdbcda8e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -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() 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() }