improve contact sorting at New Conversation, prefer names starting with letters

This commit is contained in:
tibbi 2020-05-03 23:43:09 +02:00
parent 96e78e3ddc
commit d98c1abb96
3 changed files with 24 additions and 7 deletions

View File

@ -163,11 +163,7 @@ class NewConversationActivity : SimpleActivity() {
contacts_letter_fastscroller.setupWithRecyclerView(contacts_list, { position ->
try {
val name = contacts[position].name
var character = if (name.isNotEmpty()) name.substring(0, 1) else ""
if (!character.areLettersOnly()) {
character = "#"
}
val character = if (name.isNotEmpty()) name.substring(0, 1) else ""
FastScrollItemIndicator.Text(character.toUpperCase(Locale.getDefault()))
} catch (e: Exception) {
FastScrollItemIndicator.Text("")

View File

@ -403,7 +403,7 @@ fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
it.phoneNumber.substring(startIndex)
}.toMutableList() as ArrayList<Contact>
allContacts.sortBy { it.name.normalizeString().toLowerCase(Locale.getDefault()) }
allContacts.sort()
callback(allContacts)
}
}

View File

@ -1,3 +1,24 @@
package com.simplemobiletools.smsmessenger.models
data class Contact(val id: Int, var name: String, var photoUri: String, var phoneNumber: String)
import com.simplemobiletools.commons.extensions.normalizeString
data class Contact(val id: Int, var name: String, var photoUri: String, var phoneNumber: String) : Comparable<Contact> {
override fun compareTo(other: Contact): Int {
val firstString = name.normalizeString()
val secondString = other.name.normalizeString()
return 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 {
firstString.compareTo(secondString, true)
}
}
}
}