fix sorting in case of same first name

This commit is contained in:
tibbi 2018-04-08 13:59:19 +02:00
parent 4fde91ae47
commit 2c7f651191
4 changed files with 43 additions and 28 deletions

View File

@ -236,7 +236,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
private fun setupView(view: View, contact: Contact) { private fun setupView(view: View, contact: Contact) {
view.apply { view.apply {
contact_name.text = contact.getFullName(startNameWithSurname) contact_name.text = contact.getFullName()
contact_name.setTextColor(textColor) contact_name.setTextColor(textColor)
contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0) contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0)

View File

@ -30,7 +30,6 @@ class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Con
private val config = activity.config private val config = activity.config
private val textColor = config.textColor private val textColor = config.textColor
private val contactDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_person, textColor) private val contactDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_person, textColor)
private val startNameWithSurname = config.startNameWithSurname
private val showContactThumbnails = config.showContactThumbnails private val showContactThumbnails = config.showContactThumbnails
private val itemLayout = if (config.showPhoneNumbers) R.layout.item_add_favorite_with_number else R.layout.item_add_favorite_without_number private val itemLayout = if (config.showPhoneNumbers) R.layout.item_add_favorite_with_number else R.layout.item_add_favorite_without_number
@ -80,7 +79,7 @@ class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Con
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val eventType = contacts[position] val eventType = contacts[position]
itemViews.put(position, holder.bindView(eventType, startNameWithSurname, contactDrawable, config, showContactThumbnails, smallPadding, bigPadding)) itemViews.put(position, holder.bindView(eventType, contactDrawable, config, showContactThumbnails, smallPadding, bigPadding))
toggleItemSelection(selectedPositions.contains(position), position) toggleItemSelection(selectedPositions.contains(position), position)
} }
@ -88,14 +87,14 @@ class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Con
class ViewHolder(view: View, private val adapterListener: MyAdapterListener, val activity: SimpleActivity, private val showCheckbox: Boolean, class ViewHolder(view: View, private val adapterListener: MyAdapterListener, val activity: SimpleActivity, private val showCheckbox: Boolean,
private val itemClick: ((Contact) -> Unit)?) : RecyclerView.ViewHolder(view) { private val itemClick: ((Contact) -> 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 { smallPadding: Int, bigPadding: Int): View {
itemView.apply { itemView.apply {
contact_checkbox.beVisibleIf(showCheckbox) contact_checkbox.beVisibleIf(showCheckbox)
contact_checkbox.setColors(config.textColor, context.getAdjustedPrimaryColor(), config.backgroundColor) contact_checkbox.setColors(config.textColor, context.getAdjustedPrimaryColor(), config.backgroundColor)
val textColor = config.textColor val textColor = config.textColor
contact_name.text = contact.getFullName(startNameWithSurname) contact_name.text = contact.getFullName()
contact_name.setTextColor(textColor) contact_name.setTextColor(textColor)
contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0) contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0)

View File

@ -233,7 +233,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fun onSearchQueryChanged(text: String) { fun onSearchQueryChanged(text: String) {
(fragment_list.adapter as? ContactsAdapter)?.apply { (fragment_list.adapter as? ContactsAdapter)?.apply {
val filtered = contactsIgnoringSearch.filter { val filtered = contactsIgnoringSearch.filter {
it.getFullName(startNameWithSurname).contains(text, true) || it.getFullName().contains(text, true) ||
it.phoneNumbers.any { it.value.contains(text, true) } || it.phoneNumbers.any { it.value.contains(text, true) } ||
it.emails.any { it.value.contains(text, true) } || it.emails.any { it.value.contains(text, true) } ||
it.addresses.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 } as ArrayList
Contact.sorting = config.sorting Contact.sorting = config.sorting
Contact.startWithSurname = config.startNameWithSurname
filtered.sort() filtered.sort()
filtered.sortBy { !it.getFullName(startNameWithSurname).startsWith(text, true) } filtered.sortBy { !it.getFullName().startsWith(text, true) }
if (filtered.isEmpty() && this@MyViewPagerFragment is FavoritesFragment) { if (filtered.isEmpty() && this@MyViewPagerFragment is FavoritesFragment) {
fragment_placeholder.text = activity.getString(R.string.no_items_found) fragment_placeholder.text = activity.getString(R.string.no_items_found)

View File

@ -11,13 +11,44 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
var groups: ArrayList<Group>, var organization: Organization) : Comparable<Contact> { var groups: ArrayList<Group>, var organization: Organization) : Comparable<Contact> {
companion object { companion object {
var sorting = 0 var sorting = 0
var startWithSurname = false
} }
override fun compareTo(other: Contact): Int { override fun compareTo(other: Contact): Int {
var result = when { val firstString: String
sorting and SORT_BY_FIRST_NAME != 0 -> compareStrings(firstName, other.firstName) val secondString: String
sorting and SORT_BY_MIDDLE_NAME != 0 -> compareStrings(middleName, other.middleName)
else -> compareStrings(surname, other.surname) 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) { 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 else -> surname
} }
fun getFullName(startWithSurname: Boolean): String { fun getFullName(): String {
var firstPart = if (startWithSurname) surname else firstName var firstPart = if (startWithSurname) surname else firstName
if (middleName.isNotEmpty()) { if (middleName.isNotEmpty()) {
firstPart += " $middleName" 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" val suffixComma = if (suffix.isEmpty()) "" else ", $suffix"
return "$prefix $firstPart $lastPart$suffixComma".trim() 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())
}
}
}
} }