From 2c7f65119183f9b6521995ead60656e0a1989750 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 8 Apr 2018 13:59:19 +0200 Subject: [PATCH] fix sorting in case of same first name --- .../contacts/adapters/ContactsAdapter.kt | 2 +- .../adapters/SelectContactsAdapter.kt | 7 +-- .../contacts/fragments/MyViewPagerFragment.kt | 5 +- .../contacts/models/Contact.kt | 57 ++++++++++++------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt index 8273860d..dd783836 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt @@ -236,7 +236,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList Unit)?) : RecyclerView.ViewHolder(view) { - fun bindView(contact: Contact, startNameWithSurname: Boolean, contactDrawable: Drawable, config: Config, showContactThumbnails: Boolean, + fun bindView(contact: Contact, contactDrawable: Drawable, config: Config, showContactThumbnails: Boolean, smallPadding: Int, bigPadding: Int): View { itemView.apply { contact_checkbox.beVisibleIf(showCheckbox) contact_checkbox.setColors(config.textColor, context.getAdjustedPrimaryColor(), config.backgroundColor) val textColor = config.textColor - contact_name.text = contact.getFullName(startNameWithSurname) + contact_name.text = contact.getFullName() contact_name.setTextColor(textColor) contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt index 51803ece..ff5b07aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt @@ -233,7 +233,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) fun onSearchQueryChanged(text: String) { (fragment_list.adapter as? ContactsAdapter)?.apply { val filtered = contactsIgnoringSearch.filter { - it.getFullName(startNameWithSurname).contains(text, true) || + it.getFullName().contains(text, true) || it.phoneNumbers.any { it.value.contains(text, true) } || it.emails.any { it.value.contains(text, true) } || it.addresses.any { it.value.contains(text, true) } || @@ -243,8 +243,9 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } as ArrayList Contact.sorting = config.sorting + Contact.startWithSurname = config.startNameWithSurname filtered.sort() - filtered.sortBy { !it.getFullName(startNameWithSurname).startsWith(text, true) } + filtered.sortBy { !it.getFullName().startsWith(text, true) } if (filtered.isEmpty() && this@MyViewPagerFragment is FavoritesFragment) { fragment_placeholder.text = activity.getString(R.string.no_items_found) 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 58fd2f10..47f8800c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -11,13 +11,44 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m var groups: ArrayList, var organization: Organization) : Comparable { companion object { var sorting = 0 + var startWithSurname = false } override fun compareTo(other: Contact): Int { - var result = when { - sorting and SORT_BY_FIRST_NAME != 0 -> compareStrings(firstName, other.firstName) - sorting and SORT_BY_MIDDLE_NAME != 0 -> compareStrings(middleName, other.middleName) - else -> compareStrings(surname, other.surname) + val firstString: String + val secondString: String + + when { + sorting and SORT_BY_FIRST_NAME != 0 -> { + firstString = firstName + secondString = other.firstName + } + sorting and SORT_BY_MIDDLE_NAME != 0 -> { + firstString = middleName + secondString = other.middleName + } + else -> { + firstString = surname + secondString = other.surname + } + } + + var result = if (firstString.firstOrNull()?.isLetter() == true && secondString.firstOrNull()?.isLetter() == false) { + -1 + } else if (firstString.firstOrNull()?.isLetter() == false && secondString.firstOrNull()?.isLetter() == true) { + 1 + } else { + if (firstString.isEmpty() && secondString.isNotEmpty()) { + 1 + } else if (firstString.isNotEmpty() && secondString.isEmpty()) { + -1 + } else { + if (firstString.toLowerCase() == secondString.toLowerCase()) { + getFullName().compareTo(other.getFullName()) + } else { + firstString.toLowerCase().compareTo(secondString.toLowerCase()) + } + } } if (sorting and SORT_DESCENDING != 0) { @@ -33,7 +64,7 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m else -> surname } - fun getFullName(startWithSurname: Boolean): String { + fun getFullName(): String { var firstPart = if (startWithSurname) surname else firstName if (middleName.isNotEmpty()) { firstPart += " $middleName" @@ -43,20 +74,4 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m val suffixComma = if (suffix.isEmpty()) "" else ", $suffix" return "$prefix $firstPart $lastPart$suffixComma".trim() } - - private fun compareStrings(first: String, second: String): Int { - return if (first.firstOrNull()?.isLetter() == true && second.firstOrNull()?.isLetter() == false) { - -1 - } else if (first.firstOrNull()?.isLetter() == false && second.firstOrNull()?.isLetter() == true) { - 1 - } else { - if (first.isEmpty() && second.isNotEmpty()) { - 1 - } else if (first.isNotEmpty() && second.isEmpty()) { - -1 - } else { - first.toLowerCase().compareTo(second.toLowerCase()) - } - } - } }