add filtering by search string at the New Message activity

This commit is contained in:
tibbi 2020-04-06 20:46:26 +02:00
parent 8c765a25c3
commit 3f2b88785a
1 changed files with 20 additions and 5 deletions

View File

@ -25,7 +25,7 @@ import kotlinx.android.synthetic.main.activity_new_message.*
import kotlinx.android.synthetic.main.item_selected_contact.view.* import kotlinx.android.synthetic.main.item_selected_contact.view.*
class NewMessageActivity : SimpleActivity() { class NewMessageActivity : SimpleActivity() {
private var contacts = ArrayList<Contact>() private var allContacts = ArrayList<Contact>()
private var selectedContacts = ArrayList<Contact>() private var selectedContacts = ArrayList<Contact>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -45,8 +45,8 @@ class NewMessageActivity : SimpleActivity() {
private fun initContacts() { private fun initContacts() {
val names = getNames() val names = getNames()
contacts = getPhoneNumbers() allContacts = getPhoneNumbers()
contacts.forEach { allContacts.forEach {
val contactId = it.id val contactId = it.id
val contact = names.firstOrNull { it.id == contactId } val contact = names.firstOrNull { it.id == contactId }
val name = contact?.name val name = contact?.name
@ -62,13 +62,28 @@ class NewMessageActivity : SimpleActivity() {
it.isOrganization = contact?.isOrganization ?: false it.isOrganization = contact?.isOrganization ?: false
} }
contacts = contacts.filter { it.name.isNotEmpty() }.distinctBy { allContacts = allContacts.filter { it.name.isNotEmpty() }.distinctBy {
val startIndex = Math.max(0, it.phoneNumber.length - 9) val startIndex = Math.max(0, it.phoneNumber.length - 9)
it.phoneNumber.substring(startIndex) it.phoneNumber.substring(startIndex)
}.toMutableList() as ArrayList<Contact> }.toMutableList() as ArrayList<Contact>
contacts.sortBy { it.name.normalizeString().toLowerCase() } allContacts.sortBy { it.name.normalizeString().toLowerCase() }
setupAdapter(allContacts)
new_message_to.onTextChangeListener {
val searchString = it
val filteredContacts = ArrayList<Contact>()
allContacts.forEach {
if (it.phoneNumber.contains(searchString, true) || it.name.contains(searchString, true)) {
filteredContacts.add(it)
}
}
setupAdapter(filteredContacts)
}
}
private fun setupAdapter(contacts: ArrayList<Contact>) {
ContactsAdapter(this, contacts, suggestions_list, null) { ContactsAdapter(this, contacts, suggestions_list, null) {
hideKeyboard() hideKeyboard()
Intent(this, ThreadActivity::class.java).apply { Intent(this, ThreadActivity::class.java).apply {