Skip to content

Commit 1775840

Browse files
authored
Merge pull request #978 from Naveen3Singh/add_auto_complete
Add auto complete support for name related fields
2 parents a39dca5 + e69b05c commit 1775840

File tree

4 files changed

+220
-8
lines changed

4 files changed

+220
-8
lines changed

app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ import android.media.AudioManager
1010
import android.media.RingtoneManager
1111
import android.net.Uri
1212
import android.os.Bundle
13+
import android.os.Handler
1314
import android.provider.ContactsContract.CommonDataKinds
1415
import android.provider.ContactsContract.CommonDataKinds.*
1516
import android.provider.MediaStore
1617
import android.telephony.PhoneNumberUtils
1718
import android.view.View
1819
import android.view.ViewGroup
1920
import android.view.WindowManager
20-
import android.widget.EditText
21-
import android.widget.ImageView
22-
import android.widget.RelativeLayout
23-
import android.widget.TextView
21+
import android.widget.*
2422
import androidx.core.content.ContextCompat
23+
import androidx.core.widget.doAfterTextChanged
2524
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
2625
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
2726
import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog
@@ -33,7 +32,9 @@ import com.simplemobiletools.commons.models.contacts.*
3332
import com.simplemobiletools.commons.models.contacts.Email
3433
import com.simplemobiletools.commons.models.contacts.Event
3534
import com.simplemobiletools.commons.models.contacts.Organization
35+
import com.simplemobiletools.commons.views.MyAutoCompleteTextView
3636
import com.simplemobiletools.contacts.pro.R
37+
import com.simplemobiletools.contacts.pro.adapters.AutoCompleteTextViewAdapter
3738
import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog
3839
import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleFieldsDialog
3940
import com.simplemobiletools.contacts.pro.dialogs.MyDatePickerDialog
@@ -58,6 +59,8 @@ class EditContactActivity : ContactActivity() {
5859
private val CHOOSE_PHOTO = 2
5960
private val REMOVE_PHOTO = 3
6061

62+
private val AUTO_COMPLETE_DELAY = 5000L
63+
6164
private var mLastSavePromptTS = 0L
6265
private var wasActivityInitialized = false
6366
private var lastPhotoIntentUri: Uri? = null
@@ -254,6 +257,11 @@ class EditContactActivity : ContactActivity() {
254257
setOnLongClickListener { toast(R.string.toggle_favorite); true; }
255258
}
256259

260+
val nameTextViews = arrayOf(contact_first_name, contact_middle_name, contact_surname).filter { it.isVisible() }
261+
if (nameTextViews.isNotEmpty()) {
262+
setupAutoComplete(nameTextViews)
263+
}
264+
257265
updateTextColors(contact_scrollview)
258266
numberViewToColor?.setTextColor(properPrimaryColor)
259267
emailViewToColor?.setTextColor(properPrimaryColor)
@@ -1526,4 +1534,33 @@ class EditContactActivity : ContactActivity() {
15261534
getString(R.string.jabber) -> Im.PROTOCOL_JABBER
15271535
else -> Im.PROTOCOL_CUSTOM
15281536
}
1537+
1538+
private fun setupAutoComplete(nameTextViews: List<MyAutoCompleteTextView>) {
1539+
ContactsHelper(this).getContacts { contacts ->
1540+
val adapter = AutoCompleteTextViewAdapter(this, contacts)
1541+
val handler = Handler(mainLooper)
1542+
nameTextViews.forEach { view ->
1543+
view.setAdapter(adapter)
1544+
view.setOnItemClickListener { _, _, position, _ ->
1545+
val selectedContact = adapter.resultList[position]
1546+
1547+
if (contact_first_name.isVisible()) {
1548+
contact_first_name.setText(selectedContact.firstName)
1549+
}
1550+
if (contact_middle_name.isVisible()) {
1551+
contact_middle_name.setText(selectedContact.middleName)
1552+
}
1553+
if (contact_surname.isVisible()) {
1554+
contact_surname.setText(selectedContact.surname)
1555+
}
1556+
}
1557+
view.doAfterTextChanged {
1558+
handler.postDelayed({
1559+
adapter.autoComplete = true
1560+
adapter.filter.filter(it)
1561+
}, AUTO_COMPLETE_DELAY)
1562+
}
1563+
}
1564+
}
1565+
}
15291566
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.simplemobiletools.contacts.pro.adapters
2+
3+
import android.graphics.drawable.BitmapDrawable
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.ArrayAdapter
8+
import android.widget.Filter
9+
import com.bumptech.glide.Glide
10+
import com.bumptech.glide.load.engine.DiskCacheStrategy
11+
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
12+
import com.bumptech.glide.request.RequestOptions
13+
import com.simplemobiletools.commons.extensions.beGone
14+
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
15+
import com.simplemobiletools.commons.extensions.getProperTextColor
16+
import com.simplemobiletools.commons.extensions.normalizeString
17+
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
18+
import com.simplemobiletools.commons.models.contacts.Contact
19+
import com.simplemobiletools.contacts.pro.R
20+
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
21+
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_image
22+
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_name
23+
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_number
24+
25+
class AutoCompleteTextViewAdapter(
26+
val activity: SimpleActivity,
27+
val contacts: ArrayList<Contact>,
28+
var autoComplete: Boolean = false
29+
) : ArrayAdapter<Contact>(activity, 0, contacts) {
30+
var resultList = ArrayList<Contact>()
31+
32+
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
33+
val contact = resultList[position]
34+
var listItem = convertView
35+
val nameToUse = contact.getNameToDisplay()
36+
if (listItem == null || listItem.tag != nameToUse.isNotEmpty()) {
37+
listItem = LayoutInflater.from(activity).inflate(R.layout.item_autocomplete_name_number, parent, false)
38+
}
39+
40+
val placeholder = BitmapDrawable(activity.resources, SimpleContactsHelper(context).getContactLetterIcon(nameToUse))
41+
listItem!!.apply {
42+
setBackgroundColor(context.getProperBackgroundColor())
43+
item_autocomplete_name.setTextColor(context.getProperTextColor())
44+
item_autocomplete_number.setTextColor(context.getProperTextColor())
45+
46+
tag = nameToUse.isNotEmpty()
47+
item_autocomplete_name.text = nameToUse
48+
contact.phoneNumbers.apply {
49+
val phoneNumber = firstOrNull { it.isPrimary }?.normalizedNumber ?: firstOrNull()?.normalizedNumber
50+
if (phoneNumber.isNullOrEmpty()) {
51+
item_autocomplete_number.beGone()
52+
} else {
53+
item_autocomplete_number.text = phoneNumber
54+
}
55+
}
56+
57+
val options = RequestOptions()
58+
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
59+
.error(placeholder)
60+
.centerCrop()
61+
62+
Glide.with(context)
63+
.load(contact.photoUri)
64+
.transition(DrawableTransitionOptions.withCrossFade())
65+
.placeholder(placeholder)
66+
.apply(options)
67+
.apply(RequestOptions.circleCropTransform())
68+
.into(item_autocomplete_image)
69+
}
70+
71+
return listItem
72+
}
73+
74+
override fun getFilter() = object : Filter() {
75+
override fun performFiltering(constraint: CharSequence?): FilterResults {
76+
val filterResults = FilterResults()
77+
if (constraint != null && autoComplete) {
78+
val searchString = constraint.toString().normalizeString()
79+
val results = mutableListOf<Contact>()
80+
contacts.forEach {
81+
if (it.getNameToDisplay().contains(searchString, true)) {
82+
results.add(it)
83+
}
84+
}
85+
86+
results.sortWith(compareBy<Contact>
87+
{ it.name.startsWith(searchString, true) }.thenBy
88+
{ it.name.contains(searchString, true) })
89+
results.reverse()
90+
91+
filterResults.values = results
92+
filterResults.count = results.size
93+
}
94+
return filterResults
95+
}
96+
97+
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
98+
if (results != null && results.count > 0) {
99+
resultList.clear()
100+
@Suppress("UNCHECKED_CAST")
101+
resultList.addAll(results.values as List<Contact>)
102+
notifyDataSetChanged()
103+
} else {
104+
notifyDataSetInvalidated()
105+
}
106+
}
107+
108+
override fun convertResultToString(resultValue: Any?) = (resultValue as? Contact)?.name
109+
}
110+
111+
override fun getItem(index: Int) = resultList[index]
112+
113+
override fun getCount() = resultList.size
114+
}

app/src/main/res/layout/activity_edit_contact.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
android:layout_width="match_parent"
1212
android:layout_height="wrap_content"
1313
android:scrollbars="none"
14-
android:visibility="gone">
14+
android:visibility="gone"
15+
tools:visibility="visible">
1516

1617
<RelativeLayout
1718
android:id="@+id/contact_holder"
@@ -90,7 +91,7 @@
9091
android:textCursorDrawable="@null"
9192
android:textSize="@dimen/bigger_text_size" />
9293

93-
<com.simplemobiletools.commons.views.MyEditText
94+
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
9495
android:id="@+id/contact_first_name"
9596
android:layout_width="match_parent"
9697
android:layout_height="wrap_content"
@@ -99,6 +100,7 @@
99100
android:layout_marginEnd="@dimen/activity_margin"
100101
android:layout_marginBottom="@dimen/normal_margin"
101102
android:layout_toEndOf="@+id/contact_name_image"
103+
android:completionThreshold="2"
102104
android:hint="@string/first_name"
103105
android:inputType="textCapWords"
104106
android:lines="1"
@@ -107,7 +109,7 @@
107109
android:textCursorDrawable="@null"
108110
android:textSize="@dimen/bigger_text_size" />
109111

110-
<com.simplemobiletools.commons.views.MyEditText
112+
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
111113
android:id="@+id/contact_middle_name"
112114
android:layout_width="match_parent"
113115
android:layout_height="wrap_content"
@@ -116,6 +118,7 @@
116118
android:layout_marginEnd="@dimen/activity_margin"
117119
android:layout_marginBottom="@dimen/normal_margin"
118120
android:layout_toEndOf="@+id/contact_name_image"
121+
android:completionThreshold="2"
119122
android:hint="@string/middle_name"
120123
android:inputType="textCapWords"
121124
android:lines="1"
@@ -124,7 +127,7 @@
124127
android:textCursorDrawable="@null"
125128
android:textSize="@dimen/bigger_text_size" />
126129

127-
<com.simplemobiletools.commons.views.MyEditText
130+
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
128131
android:id="@+id/contact_surname"
129132
android:layout_width="match_parent"
130133
android:layout_height="wrap_content"
@@ -133,6 +136,7 @@
133136
android:layout_marginEnd="@dimen/activity_margin"
134137
android:layout_marginBottom="@dimen/normal_margin"
135138
android:layout_toEndOf="@+id/contact_name_image"
139+
android:completionThreshold="2"
136140
android:hint="@string/surname"
137141
android:inputType="textCapWords"
138142
android:lines="1"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/item_autocomplete_holder"
6+
android:layout_width="match_parent"
7+
android:layout_height="wrap_content"
8+
android:paddingStart="@dimen/small_margin"
9+
android:paddingTop="@dimen/medium_margin"
10+
android:paddingEnd="@dimen/medium_margin"
11+
android:paddingBottom="@dimen/medium_margin">
12+
13+
<ImageView
14+
android:id="@+id/item_autocomplete_image"
15+
android:layout_width="@dimen/list_avatar_size"
16+
android:layout_height="@dimen/list_avatar_size"
17+
android:layout_margin="@dimen/tiny_margin"
18+
app:layout_constraintStart_toStartOf="parent"
19+
app:layout_constraintTop_toTopOf="parent" />
20+
21+
<TextView
22+
android:id="@+id/item_autocomplete_name"
23+
android:layout_width="0dp"
24+
android:layout_height="wrap_content"
25+
android:lines="1"
26+
android:maxLines="1"
27+
android:paddingStart="@dimen/medium_margin"
28+
android:paddingEnd="@dimen/medium_margin"
29+
android:singleLine="true"
30+
android:textSize="@dimen/bigger_text_size"
31+
app:layout_constraintBottom_toTopOf="@+id/item_autocomplete_number"
32+
app:layout_constraintEnd_toEndOf="parent"
33+
app:layout_constraintStart_toEndOf="@+id/item_autocomplete_image"
34+
app:layout_constraintTop_toTopOf="parent"
35+
app:layout_constraintVertical_chainStyle="packed"
36+
tools:text="Simple Mobile" />
37+
38+
<TextView
39+
android:id="@+id/item_autocomplete_number"
40+
android:layout_width="0dp"
41+
android:layout_height="wrap_content"
42+
android:layout_below="@+id/item_autocomplete_name"
43+
android:layout_marginEnd="8dp"
44+
android:alpha="0.8"
45+
android:lines="1"
46+
android:maxLines="1"
47+
android:paddingStart="@dimen/medium_margin"
48+
android:paddingEnd="@dimen/medium_margin"
49+
android:singleLine="true"
50+
android:textSize="@dimen/normal_text_size"
51+
app:layout_constraintBottom_toBottomOf="parent"
52+
app:layout_constraintEnd_toEndOf="parent"
53+
app:layout_constraintStart_toEndOf="@+id/item_autocomplete_image"
54+
app:layout_constraintTop_toBottomOf="@+id/item_autocomplete_name"
55+
tools:text="[email protected]" />
56+
57+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)