use an AutocompleteTextView at the ThreadActivity

This commit is contained in:
tibbi 2020-04-06 22:18:50 +02:00
parent 97299a0900
commit 900982999f
3 changed files with 119 additions and 4 deletions

View File

@ -9,20 +9,22 @@ import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.adapters.AutoCompleteTextViewAdapter
import com.simplemobiletools.smsmessenger.adapters.ThreadAdapter
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.extensions.getMessages
import com.simplemobiletools.smsmessenger.extensions.getThreadInfo
import com.simplemobiletools.smsmessenger.extensions.markSMSRead
import com.simplemobiletools.smsmessenger.extensions.*
import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.SmsSentReceiver
import kotlinx.android.synthetic.main.activity_new_message.*
import kotlinx.android.synthetic.main.activity_thread.*
import kotlinx.android.synthetic.main.activity_thread.new_message_to
import kotlinx.android.synthetic.main.activity_thread.selected_contacts
import kotlinx.android.synthetic.main.item_selected_contact.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
@ -100,6 +102,19 @@ class ThreadActivity : SimpleActivity() {
val adapter = ThreadAdapter(this, items, thread_messages_list, thread_messages_fastscroller) {}
thread_messages_list.adapter = adapter
}
getAvailableContacts {
runOnUiThread {
val adapter = AutoCompleteTextViewAdapter(this, it)
new_message_to.setAdapter(adapter)
new_message_to.imeOptions = EditorInfo.IME_ACTION_NEXT
new_message_to.setOnItemClickListener { parent, view, position, id ->
val currContacts = (new_message_to.adapter as AutoCompleteTextViewAdapter).resultList
val selectedContact = currContacts[position]
addSelectedContact(selectedContact)
}
}
}
}
private fun setupButtons() {
@ -133,6 +148,8 @@ class ThreadActivity : SimpleActivity() {
private fun addPerson() {
showSelectedContacts()
thread_add_contacts.beVisible()
new_message_to.requestFocus()
showKeyboard(new_message_to)
}
private fun showSelectedContacts() {

View File

@ -0,0 +1,82 @@
package com.simplemobiletools.smsmessenger.adapters
import android.graphics.drawable.LayerDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Filter
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.normalizeString
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.models.Contact
import kotlinx.android.synthetic.main.item_contact.view.*
class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: ArrayList<Contact>) :
ArrayAdapter<Contact>(activity, 0, contacts) {
var resultList = ArrayList<Contact>()
private var placeholder = activity.resources.getDrawable(R.drawable.contact_circular_background)
init {
(placeholder as LayerDrawable).findDrawableByLayerId(R.id.attendee_circular_background)
.applyColorFilter(activity.config.primaryColor)
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val contact = resultList[position]
var listItem = convertView
if (listItem == null || listItem.tag != contact.name.isNotEmpty()) {
listItem = LayoutInflater.from(activity).inflate(R.layout.item_contact, parent, false)
}
listItem!!.apply {
tag = contact.name.isNotEmpty()
item_autocomplete_name.text = contact.name
item_autocomplete_number.text = contact.phoneNumber
contact.updateImage(context, item_autocomplete_image, placeholder)
}
return listItem
}
override fun getFilter() = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null) {
resultList.clear()
val searchString = constraint.toString().normalizeString()
contacts.forEach {
if (it.phoneNumber.contains(searchString, true) || it.name.contains(searchString, true)) {
resultList.add(it)
}
}
resultList.sortWith(compareBy<Contact>
{ it.name.startsWith(searchString, true) }.thenBy
{ it.name.contains(searchString, true) })
resultList.reverse()
filterResults.values = resultList
filterResults.count = resultList.size
}
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results?.count ?: -1 > 0) {
notifyDataSetChanged()
} else {
notifyDataSetInvalidated()
}
}
override fun convertResultToString(resultValue: Any?) = (resultValue as? Contact)?.name
}
override fun getItem(index: Int) = resultList[index]
override fun getCount() = resultList.size
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/attendee_circular_background">
<shape android:shape="oval">
<solid android:color="@color/color_primary" />
</shape>
</item>
<item
android:bottom="@dimen/medium_margin"
android:drawable="@drawable/ic_person_vector"
android:left="@dimen/medium_margin"
android:right="@dimen/medium_margin"
android:top="@dimen/medium_margin" />
</layer-list>