adding some autocomplete suggestions at new messages
This commit is contained in:
parent
b67d2a8dfb
commit
dc806c97d0
|
@ -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<Contact>()
|
||||
private var selectedContacts = ArrayList<Contact>()
|
||||
|
||||
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<Contact> {
|
||||
|
|
|
@ -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<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_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<Contact>
|
||||
{ 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
|
||||
}
|
|
@ -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>
|
|
@ -4,13 +4,14 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyEditText
|
||||
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
|
||||
android:id="@+id/new_message_to"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/normal_icon_size"
|
||||
android:layout_marginStart="@dimen/activity_margin"
|
||||
android:layout_marginEnd="@dimen/activity_margin"
|
||||
android:background="@android:color/transparent"
|
||||
android:completionThreshold="2"
|
||||
android:gravity="center_vertical"
|
||||
android:hint="@string/send_to"
|
||||
android:inputType="textCapWords" />
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/item_autocomplete_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="@dimen/small_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:paddingEnd="@dimen/medium_margin"
|
||||
android:paddingBottom="@dimen/medium_margin">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/item_autocomplete_image"
|
||||
android:layout_width="@dimen/avatar_size"
|
||||
android:layout_height="@dimen/avatar_size"
|
||||
android:layout_margin="@dimen/tiny_margin"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_autocomplete_name"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:lines="1"
|
||||
android:maxLines="1"
|
||||
android:paddingStart="@dimen/medium_margin"
|
||||
android:paddingEnd="@dimen/medium_margin"
|
||||
android:singleLine="true"
|
||||
android:textSize="@dimen/bigger_text_size"
|
||||
app:layout_constraintBottom_toTopOf="@+id/item_autocomplete_number"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/item_autocomplete_image"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_chainStyle="packed"
|
||||
tools:text="Simple Mobile" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_autocomplete_number"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/item_autocomplete_name"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:alpha="0.8"
|
||||
android:lines="1"
|
||||
android:maxLines="1"
|
||||
android:paddingStart="@dimen/medium_margin"
|
||||
android:paddingEnd="@dimen/medium_margin"
|
||||
android:singleLine="true"
|
||||
android:textSize="@dimen/normal_text_size"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/item_autocomplete_image"
|
||||
app:layout_constraintTop_toBottomOf="@+id/item_autocomplete_name"
|
||||
tools:text="123456" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -1,4 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<dimen name="notification_large_icon_size">72dp</dimen>
|
||||
<dimen name="avatar_size">40dp</dimen>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in New Issue