From 4003246483ac3d70cc7586fb99fed4cf6326b2ba Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 5 May 2023 14:02:11 +0530 Subject: [PATCH] Use Autofill in name text fields --- .../pro/activities/EditContactActivity.kt | 34 +++++- .../adapters/AutoCompleteTextViewAdapter.kt | 102 ++++++++++++++++++ .../main/res/layout/activity_edit_contact.xml | 12 ++- .../layout/item_autocomplete_name_number.xml | 57 ++++++++++ 4 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/AutoCompleteTextViewAdapter.kt create mode 100644 app/src/main/res/layout/item_autocomplete_name_number.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt index 10c475fd..7622c2ae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt @@ -17,10 +17,7 @@ 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 com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog @@ -33,7 +30,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 @@ -254,6 +253,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()) { + setupAutofill(nameTextViews) + } + updateTextColors(contact_scrollview) numberViewToColor?.setTextColor(properPrimaryColor) emailViewToColor?.setTextColor(properPrimaryColor) @@ -1526,4 +1530,26 @@ class EditContactActivity : ContactActivity() { getString(R.string.jabber) -> Im.PROTOCOL_JABBER else -> Im.PROTOCOL_CUSTOM } + + private fun setupAutofill(nameTextViews: List) { + ContactsHelper(this).getContacts { contacts -> + val adapter = AutoCompleteTextViewAdapter(this, contacts) + 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) + } + } + } + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/AutoCompleteTextViewAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/AutoCompleteTextViewAdapter.kt new file mode 100644 index 00000000..0374f651 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/AutoCompleteTextViewAdapter.kt @@ -0,0 +1,102 @@ +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.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) : ArrayAdapter(activity, 0, contacts) { + var resultList = ArrayList() + + 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_name_number, parent, false) + } + + val nameToUse = contact.name + + 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 = contact.name.isNotEmpty() + item_autocomplete_name.text = contact.name + item_autocomplete_number.text = contact.phoneNumbers.run { + firstOrNull { it.isPrimary }?.normalizedNumber ?: firstOrNull()?.normalizedNumber + } + + 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) { + resultList.clear() + val searchString = constraint.toString().normalizeString() + contacts.forEach { + if (it.name.contains(searchString, true)) { + resultList.add(it) + } + } + + resultList.sortWith(compareBy + { 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 +} diff --git a/app/src/main/res/layout/activity_edit_contact.xml b/app/src/main/res/layout/activity_edit_contact.xml index b6f57568..217017ce 100644 --- a/app/src/main/res/layout/activity_edit_contact.xml +++ b/app/src/main/res/layout/activity_edit_contact.xml @@ -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"> - - - + + + + + + + + +