mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-02-21 14:10:41 +01:00
fill the Suggestions list with contacts
This commit is contained in:
parent
979744cbd5
commit
8736473b35
@ -36,7 +36,7 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.simplemobiletools:commons:5.24.17'
|
||||
implementation 'com.simplemobiletools:commons:5.24.18'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
|
||||
implementation 'org.greenrobot:eventbus:3.2.0'
|
||||
}
|
||||
|
@ -8,14 +8,13 @@ import android.text.TextUtils
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.LinearLayout.LayoutParams
|
||||
import android.widget.RelativeLayout
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.adapters.AutoCompleteTextViewAdapter
|
||||
import com.simplemobiletools.smsmessenger.adapters.ContactsAdapter
|
||||
import com.simplemobiletools.smsmessenger.extensions.getThreadId
|
||||
import com.simplemobiletools.smsmessenger.extensions.launchThreadActivity
|
||||
import com.simplemobiletools.smsmessenger.models.Contact
|
||||
@ -56,21 +55,22 @@ class NewMessageActivity : SimpleActivity() {
|
||||
if (photoUri != null) {
|
||||
it.photoUri = photoUri
|
||||
}
|
||||
|
||||
it.isOrganization = contact?.isOrganization ?: false
|
||||
}
|
||||
|
||||
contacts = contacts.distinctBy {
|
||||
contacts = contacts.filter { it.name.isNotEmpty() }.distinctBy {
|
||||
val startIndex = Math.max(0, it.phoneNumber.length - 9)
|
||||
it.phoneNumber.substring(startIndex)
|
||||
}.toMutableList() as ArrayList<Contact>
|
||||
|
||||
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]
|
||||
contacts.sortBy { it.name.normalizeString().toLowerCase() }
|
||||
|
||||
ContactsAdapter(this, contacts, suggestions_list, null) {
|
||||
hideKeyboard()
|
||||
launchThreadActivity(getThreadId(selectedContact.phoneNumber).toInt())
|
||||
launchThreadActivity(getThreadId((it as Contact).phoneNumber).toInt())
|
||||
}.apply {
|
||||
suggestions_list.adapter = this
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,7 +194,7 @@ class NewMessageActivity : SimpleActivity() {
|
||||
if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) {
|
||||
val names = arrayOf(prefix, firstName, middleName, familyName, suffix).filter { it.isNotEmpty() }
|
||||
val fullName = TextUtils.join(" ", names)
|
||||
val contact = Contact(id, fullName, photoUri, "")
|
||||
val contact = Contact(id, fullName, photoUri, "", false)
|
||||
contacts.add(contact)
|
||||
}
|
||||
}
|
||||
@ -205,7 +205,7 @@ class NewMessageActivity : SimpleActivity() {
|
||||
val jobTitle = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: ""
|
||||
if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
|
||||
val fullName = "$company $jobTitle".trim()
|
||||
val contact = Contact(id, fullName, photoUri, "")
|
||||
val contact = Contact(id, fullName, photoUri, "", true)
|
||||
contacts.add(contact)
|
||||
}
|
||||
}
|
||||
@ -233,7 +233,7 @@ class NewMessageActivity : SimpleActivity() {
|
||||
do {
|
||||
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||
val phoneNumber = cursor.getStringValue(CommonDataKinds.Phone.NORMALIZED_NUMBER) ?: continue
|
||||
val contact = Contact(id, "", "", phoneNumber)
|
||||
val contact = Contact(id, "", "", phoneNumber, false)
|
||||
contacts.add(contact)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
|
@ -1,84 +0,0 @@
|
||||
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,90 @@
|
||||
package com.simplemobiletools.smsmessenger.adapters
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.Menu
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.bumptech.glide.Glide
|
||||
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
|
||||
import com.simplemobiletools.commons.views.FastScroller
|
||||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||
import com.simplemobiletools.smsmessenger.models.Contact
|
||||
import kotlinx.android.synthetic.main.item_contact_with_number.view.*
|
||||
import java.util.*
|
||||
|
||||
class ContactsAdapter(
|
||||
activity: SimpleActivity, var contacts: ArrayList<Contact>, recyclerView: MyRecyclerView,
|
||||
fastScroller: FastScroller?, itemClick: (Any) -> Unit
|
||||
) :
|
||||
MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
|
||||
|
||||
private lateinit var contactDrawable: Drawable
|
||||
private lateinit var businessContactDrawable: Drawable
|
||||
|
||||
init {
|
||||
initDrawables()
|
||||
}
|
||||
|
||||
override fun getActionMenuId() = 0
|
||||
|
||||
override fun prepareActionMode(menu: Menu) {}
|
||||
|
||||
override fun actionItemPressed(id: Int) {}
|
||||
|
||||
override fun getSelectableItemCount() = contacts.size
|
||||
|
||||
override fun getIsItemSelectable(position: Int) = true
|
||||
|
||||
override fun getItemSelectionKey(position: Int) = contacts.getOrNull(position)?.id
|
||||
|
||||
override fun getItemKeyPosition(key: Int) = contacts.indexOfFirst { it.id == key }
|
||||
|
||||
override fun onActionModeCreated() {}
|
||||
|
||||
override fun onActionModeDestroyed() {}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_contact_with_number, parent)
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val contact = contacts[position]
|
||||
holder.bindView(contact, true, false) { itemView, layoutPosition ->
|
||||
setupView(itemView, contact)
|
||||
}
|
||||
bindViewHolder(holder)
|
||||
}
|
||||
|
||||
override fun getItemCount() = contacts.size
|
||||
|
||||
private fun initDrawables() {
|
||||
contactDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_person_vector, textColor)
|
||||
businessContactDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_business_vector, textColor)
|
||||
}
|
||||
|
||||
override fun onViewRecycled(holder: ViewHolder) {
|
||||
super.onViewRecycled(holder)
|
||||
if (!activity.isDestroyed && !activity.isFinishing) {
|
||||
Glide.with(activity).clear(holder.itemView.contact_tmb)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupView(view: View, contact: Contact) {
|
||||
view.apply {
|
||||
contact_name.text = contact.name
|
||||
contact_name.setTextColor(textColor)
|
||||
|
||||
contact_number.text = contact.phoneNumber
|
||||
contact_number.setTextColor(textColor)
|
||||
|
||||
val placeholder = if (contact.isOrganization) {
|
||||
businessContactDrawable
|
||||
} else {
|
||||
contactDrawable
|
||||
}
|
||||
|
||||
contact.updateImage(context, contact_tmb, placeholder)
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,8 @@ data class Contact(
|
||||
val id: Int,
|
||||
var name: String,
|
||||
var photoUri: String,
|
||||
var phoneNumber: String
|
||||
var phoneNumber: String,
|
||||
var isOrganization: Boolean
|
||||
) {
|
||||
fun updateImage(context: Context, imageView: ImageView, placeholder: Drawable) {
|
||||
if (photoUri.isEmpty()) {
|
||||
|
@ -1,16 +0,0 @@
|
||||
<?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>
|
9
app/src/main/res/drawable-xxhdpi/ic_business_vector.xml
Normal file
9
app/src/main/res/drawable-xxhdpi/ic_business_vector.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M12,7L12,3L2,3v18h20L22,7L12,7zM6,19L4,19v-2h2v2zM6,15L4,15v-2h2v2zM6,11L4,11L4,9h2v2zM6,7L4,7L4,5h2v2zM10,19L8,19v-2h2v2zM10,15L8,15v-2h2v2zM10,11L8,11L8,9h2v2zM10,7L8,7L8,5h2v2zM20,19h-8v-2h2v-2h-2v-2h2v-2h-2L12,9h8v10zM18,11h-2v2h2v-2zM18,15h-2v2h2v-2z"/>
|
||||
</vector>
|
@ -22,7 +22,7 @@
|
||||
android:importantForAccessibility="no"
|
||||
android:visibility="gone" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyAutoCompleteTextView
|
||||
<com.simplemobiletools.commons.views.MyEditText
|
||||
android:id="@+id/new_message_to"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/normal_icon_size"
|
||||
@ -30,7 +30,6 @@
|
||||
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"
|
||||
@ -45,15 +44,13 @@
|
||||
android:importantForAccessibility="no" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||
android:id="@+id/messages_list"
|
||||
android:id="@+id/suggestions_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@+id/message_divider_two"
|
||||
android:clipToPadding="false"
|
||||
android:overScrollMode="ifContentScrolls"
|
||||
android:padding="@dimen/activity_margin"
|
||||
android:scrollbars="none"
|
||||
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"
|
||||
app:stackFromEnd="true" />
|
||||
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
|
||||
|
||||
</RelativeLayout>
|
||||
|
43
app/src/main/res/layout/item_contact_with_number.xml
Normal file
43
app/src/main/res/layout/item_contact_with_number.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/contact_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:paddingEnd="@dimen/activity_margin"
|
||||
android:paddingBottom="@dimen/medium_margin">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/contact_tmb"
|
||||
android:layout_width="@dimen/normal_icon_size"
|
||||
android:layout_height="@dimen/normal_icon_size"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginStart="@dimen/small_margin"
|
||||
android:layout_marginEnd="@dimen/small_margin"
|
||||
android:padding="@dimen/small_margin"
|
||||
android:src="@drawable/ic_person_vector" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/contact_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_toEndOf="@+id/contact_tmb"
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:textSize="@dimen/big_text_size"
|
||||
tools:text="John Doe" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/contact_number"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_name"
|
||||
android:layout_alignStart="@+id/contact_name"
|
||||
android:layout_toEndOf="@+id/contact_tmb"
|
||||
android:alpha="0.6"
|
||||
android:maxLines="1"
|
||||
android:textSize="@dimen/normal_text_size"
|
||||
tools:text="0123 456 789" />
|
||||
|
||||
</RelativeLayout>
|
Loading…
x
Reference in New Issue
Block a user