diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewMessageActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewMessageActivity.kt index dc22ef1b..9dd35db7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewMessageActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewMessageActivity.kt @@ -6,15 +6,20 @@ import android.provider.ContactsContract import android.provider.ContactsContract.CommonDataKinds import android.text.TextUtils import android.view.WindowManager +import android.view.inputmethod.EditorInfo import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.smsmessenger.R +import com.simplemobiletools.smsmessenger.adapters.AutoCompleteTextViewAdapter import com.simplemobiletools.smsmessenger.models.Contact import kotlinx.android.synthetic.main.activity_new_message.* class NewMessageActivity : SimpleActivity() { + private var contacts = ArrayList() + private var selectedContacts = ArrayList() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_new_message) @@ -24,7 +29,7 @@ class NewMessageActivity : SimpleActivity() { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) new_message_to.requestFocus() - // Contact permission is not mandatory, but without it we won't be able to show any suggestions during typing + // READ_CONTACTS permission is not mandatory, but without it we won't be able to show any suggestions during typing handlePermission(PERMISSION_READ_CONTACTS) { initContacts() } @@ -32,7 +37,7 @@ class NewMessageActivity : SimpleActivity() { private fun initContacts() { val names = getNames() - val contacts = getPhoneNumbers() + contacts = getPhoneNumbers() contacts.forEach { val contactId = it.contactId val contact = names.firstOrNull { it.contactId == contactId } @@ -46,6 +51,15 @@ class NewMessageActivity : SimpleActivity() { it.photoUri = photoUri } } + + val adapter = AutoCompleteTextViewAdapter(this, contacts) + 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] + selectedContacts.add(selectedContact) + } } private fun getNames(): List { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt new file mode 100644 index 00000000..4a6ea604 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt @@ -0,0 +1,84 @@ +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_autocomplete_contact.view.* + +class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: ArrayList) : + ArrayAdapter(activity, 0, contacts) { + var resultList = ArrayList() + 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_autocomplete_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 + { it.name.startsWith(searchString, true) }.thenBy + { it.phoneNumber.startsWith(searchString, true) }.thenBy + { it.name.contains(searchString, true) }.thenBy + { it.phoneNumber.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 +} diff --git a/app/src/main/res/drawable-xxhdpi/contact_circular_background.xml b/app/src/main/res/drawable-xxhdpi/contact_circular_background.xml new file mode 100644 index 00000000..7378e493 --- /dev/null +++ b/app/src/main/res/drawable-xxhdpi/contact_circular_background.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_new_message.xml b/app/src/main/res/layout/activity_new_message.xml index 699f7c37..193c8aa4 100644 --- a/app/src/main/res/layout/activity_new_message.xml +++ b/app/src/main/res/layout/activity_new_message.xml @@ -4,13 +4,14 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - diff --git a/app/src/main/res/layout/item_autocomplete_contact.xml b/app/src/main/res/layout/item_autocomplete_contact.xml new file mode 100644 index 00000000..eb3333f4 --- /dev/null +++ b/app/src/main/res/layout/item_autocomplete_contact.xml @@ -0,0 +1,57 @@ + + + + + + + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index fe5a09a8..dfbf070b 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,4 +1,5 @@ 72dp + 40dp