Merge pull request #978 from Naveen3Singh/add_auto_complete

Add auto complete support for name related fields
This commit is contained in:
Tibor Kaputa 2023-05-14 16:12:48 +02:00 committed by GitHub
commit 1775840404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 220 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import android.media.AudioManager
import android.media.RingtoneManager
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.provider.ContactsContract.CommonDataKinds
import android.provider.ContactsContract.CommonDataKinds.*
import android.provider.MediaStore
@ -17,11 +18,9 @@ import android.telephony.PhoneNumberUtils
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog
@ -33,7 +32,9 @@ import com.simplemobiletools.commons.models.contacts.*
import com.simplemobiletools.commons.models.contacts.Email
import com.simplemobiletools.commons.models.contacts.Event
import com.simplemobiletools.commons.models.contacts.Organization
import com.simplemobiletools.commons.views.MyAutoCompleteTextView
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.AutoCompleteTextViewAdapter
import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog
import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleFieldsDialog
import com.simplemobiletools.contacts.pro.dialogs.MyDatePickerDialog
@ -58,6 +59,8 @@ class EditContactActivity : ContactActivity() {
private val CHOOSE_PHOTO = 2
private val REMOVE_PHOTO = 3
private val AUTO_COMPLETE_DELAY = 5000L
private var mLastSavePromptTS = 0L
private var wasActivityInitialized = false
private var lastPhotoIntentUri: Uri? = null
@ -254,6 +257,11 @@ class EditContactActivity : ContactActivity() {
setOnLongClickListener { toast(R.string.toggle_favorite); true; }
}
val nameTextViews = arrayOf(contact_first_name, contact_middle_name, contact_surname).filter { it.isVisible() }
if (nameTextViews.isNotEmpty()) {
setupAutoComplete(nameTextViews)
}
updateTextColors(contact_scrollview)
numberViewToColor?.setTextColor(properPrimaryColor)
emailViewToColor?.setTextColor(properPrimaryColor)
@ -1526,4 +1534,33 @@ class EditContactActivity : ContactActivity() {
getString(R.string.jabber) -> Im.PROTOCOL_JABBER
else -> Im.PROTOCOL_CUSTOM
}
private fun setupAutoComplete(nameTextViews: List<MyAutoCompleteTextView>) {
ContactsHelper(this).getContacts { contacts ->
val adapter = AutoCompleteTextViewAdapter(this, contacts)
val handler = Handler(mainLooper)
nameTextViews.forEach { view ->
view.setAdapter(adapter)
view.setOnItemClickListener { _, _, position, _ ->
val selectedContact = adapter.resultList[position]
if (contact_first_name.isVisible()) {
contact_first_name.setText(selectedContact.firstName)
}
if (contact_middle_name.isVisible()) {
contact_middle_name.setText(selectedContact.middleName)
}
if (contact_surname.isVisible()) {
contact_surname.setText(selectedContact.surname)
}
}
view.doAfterTextChanged {
handler.postDelayed({
adapter.autoComplete = true
adapter.filter.filter(it)
}, AUTO_COMPLETE_DELAY)
}
}
}
}
}

View File

@ -0,0 +1,114 @@
package com.simplemobiletools.contacts.pro.adapters
import android.graphics.drawable.BitmapDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Filter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.normalizeString
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_image
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_name
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_number
class AutoCompleteTextViewAdapter(
val activity: SimpleActivity,
val contacts: ArrayList<Contact>,
var autoComplete: Boolean = false
) : ArrayAdapter<Contact>(activity, 0, contacts) {
var resultList = ArrayList<Contact>()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val contact = resultList[position]
var listItem = convertView
val nameToUse = contact.getNameToDisplay()
if (listItem == null || listItem.tag != nameToUse.isNotEmpty()) {
listItem = LayoutInflater.from(activity).inflate(R.layout.item_autocomplete_name_number, parent, false)
}
val placeholder = BitmapDrawable(activity.resources, SimpleContactsHelper(context).getContactLetterIcon(nameToUse))
listItem!!.apply {
setBackgroundColor(context.getProperBackgroundColor())
item_autocomplete_name.setTextColor(context.getProperTextColor())
item_autocomplete_number.setTextColor(context.getProperTextColor())
tag = nameToUse.isNotEmpty()
item_autocomplete_name.text = nameToUse
contact.phoneNumbers.apply {
val phoneNumber = firstOrNull { it.isPrimary }?.normalizedNumber ?: firstOrNull()?.normalizedNumber
if (phoneNumber.isNullOrEmpty()) {
item_autocomplete_number.beGone()
} else {
item_autocomplete_number.text = phoneNumber
}
}
val options = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.error(placeholder)
.centerCrop()
Glide.with(context)
.load(contact.photoUri)
.transition(DrawableTransitionOptions.withCrossFade())
.placeholder(placeholder)
.apply(options)
.apply(RequestOptions.circleCropTransform())
.into(item_autocomplete_image)
}
return listItem
}
override fun getFilter() = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null && autoComplete) {
val searchString = constraint.toString().normalizeString()
val results = mutableListOf<Contact>()
contacts.forEach {
if (it.getNameToDisplay().contains(searchString, true)) {
results.add(it)
}
}
results.sortWith(compareBy<Contact>
{ it.name.startsWith(searchString, true) }.thenBy
{ it.name.contains(searchString, true) })
results.reverse()
filterResults.values = results
filterResults.count = results.size
}
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results != null && results.count > 0) {
resultList.clear()
@Suppress("UNCHECKED_CAST")
resultList.addAll(results.values as List<Contact>)
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

@ -11,7 +11,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:visibility="gone">
android:visibility="gone"
tools:visibility="visible">
<RelativeLayout
android:id="@+id/contact_holder"
@ -90,7 +91,7 @@
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyEditText
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/contact_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -99,6 +100,7 @@
android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_toEndOf="@+id/contact_name_image"
android:completionThreshold="2"
android:hint="@string/first_name"
android:inputType="textCapWords"
android:lines="1"
@ -107,7 +109,7 @@
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyEditText
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/contact_middle_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -116,6 +118,7 @@
android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_toEndOf="@+id/contact_name_image"
android:completionThreshold="2"
android:hint="@string/middle_name"
android:inputType="textCapWords"
android:lines="1"
@ -124,7 +127,7 @@
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyEditText
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/contact_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -133,6 +136,7 @@
android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_toEndOf="@+id/contact_name_image"
android:completionThreshold="2"
android:hint="@string/surname"
android:inputType="textCapWords"
android:lines="1"

View File

@ -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/list_avatar_size"
android:layout_height="@dimen/list_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="hello@simplemobiletools.com" />
</androidx.constraintlayout.widget.ConstraintLayout>