Skip to content

Commit a25a3e5

Browse files
authored
Merge branch 'master' into fix_ime_inset
2 parents c0964d3 + 387b645 commit a25a3e5

File tree

5 files changed

+221
-9
lines changed

5 files changed

+221
-9
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ android {
6363
}
6464

6565
dependencies {
66-
implementation 'com.github.SimpleMobileTools:Simple-Commons:94b616f462'
66+
implementation 'com.github.SimpleMobileTools:Simple-Commons:8a1114e683'
6767
implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3'
6868
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
6969
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

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,19 +10,18 @@ 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
2523
import androidx.core.view.WindowInsetsCompat
24+
import androidx.core.widget.doAfterTextChanged
2625
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
2726
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
2827
import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog
@@ -34,7 +33,9 @@ import com.simplemobiletools.commons.models.contacts.*
3433
import com.simplemobiletools.commons.models.contacts.Email
3534
import com.simplemobiletools.commons.models.contacts.Event
3635
import com.simplemobiletools.commons.models.contacts.Organization
36+
import com.simplemobiletools.commons.views.MyAutoCompleteTextView
3737
import com.simplemobiletools.contacts.pro.R
38+
import com.simplemobiletools.contacts.pro.adapters.AutoCompleteTextViewAdapter
3839
import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog
3940
import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleFieldsDialog
4041
import com.simplemobiletools.contacts.pro.dialogs.MyDatePickerDialog
@@ -59,6 +60,8 @@ class EditContactActivity : ContactActivity() {
5960
private val CHOOSE_PHOTO = 2
6061
private val REMOVE_PHOTO = 3
6162

63+
private val AUTO_COMPLETE_DELAY = 5000L
64+
6265
private var mLastSavePromptTS = 0L
6366
private var wasActivityInitialized = false
6467
private var lastPhotoIntentUri: Uri? = null
@@ -261,6 +264,11 @@ class EditContactActivity : ContactActivity() {
261264
setOnLongClickListener { toast(R.string.toggle_favorite); true; }
262265
}
263266

267+
val nameTextViews = arrayOf(contact_first_name, contact_middle_name, contact_surname).filter { it.isVisible() }
268+
if (nameTextViews.isNotEmpty()) {
269+
setupAutoComplete(nameTextViews)
270+
}
271+
264272
updateTextColors(contact_scrollview)
265273
numberViewToColor?.setTextColor(properPrimaryColor)
266274
emailViewToColor?.setTextColor(properPrimaryColor)
@@ -1533,4 +1541,33 @@ class EditContactActivity : ContactActivity() {
15331541
getString(R.string.jabber) -> Im.PROTOCOL_JABBER
15341542
else -> Im.PROTOCOL_CUSTOM
15351543
}
1544+
1545+
private fun setupAutoComplete(nameTextViews: List<MyAutoCompleteTextView>) {
1546+
ContactsHelper(this).getContacts { contacts ->
1547+
val adapter = AutoCompleteTextViewAdapter(this, contacts)
1548+
val handler = Handler(mainLooper)
1549+
nameTextViews.forEach { view ->
1550+
view.setAdapter(adapter)
1551+
view.setOnItemClickListener { _, _, position, _ ->
1552+
val selectedContact = adapter.resultList[position]
1553+
1554+
if (contact_first_name.isVisible()) {
1555+
contact_first_name.setText(selectedContact.firstName)
1556+
}
1557+
if (contact_middle_name.isVisible()) {
1558+
contact_middle_name.setText(selectedContact.middleName)
1559+
}
1560+
if (contact_surname.isVisible()) {
1561+
contact_surname.setText(selectedContact.surname)
1562+
}
1563+
}
1564+
view.doAfterTextChanged {
1565+
handler.postDelayed({
1566+
adapter.autoComplete = true
1567+
adapter.filter.filter(it)
1568+
}, AUTO_COMPLETE_DELAY)
1569+
}
1570+
}
1571+
}
1572+
}
15361573
}
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
@@ -12,7 +12,8 @@
1212
android:layout_height="wrap_content"
1313
android:clipToPadding="false"
1414
android:scrollbars="none"
15-
android:visibility="gone">
15+
android:visibility="gone"
16+
tools:visibility="visible">
1617

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

94-
<com.simplemobiletools.commons.views.MyEditText
95+
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
9596
android:id="@+id/contact_first_name"
9697
android:layout_width="match_parent"
9798
android:layout_height="wrap_content"
@@ -100,6 +101,7 @@
100101
android:layout_marginEnd="@dimen/activity_margin"
101102
android:layout_marginBottom="@dimen/normal_margin"
102103
android:layout_toEndOf="@+id/contact_name_image"
104+
android:completionThreshold="2"
103105
android:hint="@string/first_name"
104106
android:inputType="textCapWords"
105107
android:lines="1"
@@ -108,7 +110,7 @@
108110
android:textCursorDrawable="@null"
109111
android:textSize="@dimen/bigger_text_size" />
110112

111-
<com.simplemobiletools.commons.views.MyEditText
113+
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
112114
android:id="@+id/contact_middle_name"
113115
android:layout_width="match_parent"
114116
android:layout_height="wrap_content"
@@ -117,6 +119,7 @@
117119
android:layout_marginEnd="@dimen/activity_margin"
118120
android:layout_marginBottom="@dimen/normal_margin"
119121
android:layout_toEndOf="@+id/contact_name_image"
122+
android:completionThreshold="2"
120123
android:hint="@string/middle_name"
121124
android:inputType="textCapWords"
122125
android:lines="1"
@@ -125,7 +128,7 @@
125128
android:textCursorDrawable="@null"
126129
android:textSize="@dimen/bigger_text_size" />
127130

128-
<com.simplemobiletools.commons.views.MyEditText
131+
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
129132
android:id="@+id/contact_surname"
130133
android:layout_width="match_parent"
131134
android:layout_height="wrap_content"
@@ -134,6 +137,7 @@
134137
android:layout_marginEnd="@dimen/activity_margin"
135138
android:layout_marginBottom="@dimen/normal_margin"
136139
android:layout_toEndOf="@+id/contact_name_image"
140+
android:completionThreshold="2"
137141
android:hint="@string/surname"
138142
android:inputType="textCapWords"
139143
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)