show some suggestions at top of the contact picker screen
This commit is contained in:
parent
e792ff37ab
commit
5a462775fd
7 changed files with 124 additions and 7 deletions
|
|
@ -5,16 +5,16 @@ import android.os.Bundle
|
|||
import android.view.WindowManager
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.adapters.ContactsAdapter
|
||||
import com.simplemobiletools.smsmessenger.extensions.config
|
||||
import com.simplemobiletools.smsmessenger.extensions.getAvailableContacts
|
||||
import com.simplemobiletools.smsmessenger.extensions.getThreadId
|
||||
import com.simplemobiletools.smsmessenger.extensions.*
|
||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
|
||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_TEXT
|
||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_TITLE
|
||||
import com.simplemobiletools.smsmessenger.models.Contact
|
||||
import kotlinx.android.synthetic.main.activity_new_message.*
|
||||
import kotlinx.android.synthetic.main.item_suggested_contact.view.*
|
||||
|
||||
class NewMessageActivity : SimpleActivity() {
|
||||
private var allContacts = ArrayList<Contact>()
|
||||
|
|
@ -46,6 +46,7 @@ class NewMessageActivity : SimpleActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
fillSuggestedContacts()
|
||||
new_message_to.onTextChangeListener {
|
||||
val searchString = it
|
||||
val filteredContacts = ArrayList<Contact>()
|
||||
|
|
@ -78,13 +79,35 @@ class NewMessageActivity : SimpleActivity() {
|
|||
}
|
||||
|
||||
private fun setupAdapter(contacts: ArrayList<Contact>) {
|
||||
ContactsAdapter(this, contacts, suggestions_list, null) {
|
||||
ContactsAdapter(this, contacts, contacts_list, null) {
|
||||
hideKeyboard()
|
||||
|
||||
val text = intent.getStringExtra(Intent.EXTRA_TEXT) ?: ""
|
||||
launchThreadActivity((it as Contact).phoneNumber, it.name, text)
|
||||
}.apply {
|
||||
suggestions_list.adapter = this
|
||||
contacts_list.adapter = this
|
||||
}
|
||||
}
|
||||
|
||||
private fun fillSuggestedContacts() {
|
||||
ensureBackgroundThread {
|
||||
val suggestions = getSuggestedContacts()
|
||||
runOnUiThread {
|
||||
suggestions_holder.removeAllViews()
|
||||
if (suggestions.isEmpty()) {
|
||||
suggestions_label.beGone()
|
||||
suggestions_scrollview.beGone()
|
||||
} else {
|
||||
suggestions.forEach {
|
||||
val contact = it
|
||||
layoutInflater.inflate(R.layout.item_suggested_contact, null).apply {
|
||||
suggested_contact_name.text = contact.name
|
||||
loadImage(contact.photoUri, suggested_contact_image, contact.name)
|
||||
suggestions_holder.addView(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -338,6 +338,33 @@ fun Context.getPhoneNumberFromAddressId(canonicalAddressId: Int): String {
|
|||
return ""
|
||||
}
|
||||
|
||||
fun Context.getSuggestedContacts(): ArrayList<Contact> {
|
||||
val contacts = ArrayList<Contact>()
|
||||
val uri = Sms.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
Sms.ADDRESS
|
||||
)
|
||||
|
||||
val selection = "1 == 1) GROUP BY (${Sms.ADDRESS}"
|
||||
val selectionArgs = null
|
||||
val sortOrder = "${Sms.DATE} DESC LIMIT 20"
|
||||
|
||||
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
|
||||
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
|
||||
val namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber)
|
||||
if (namePhoto == null || namePhoto.name == senderNumber) {
|
||||
return@queryCursor
|
||||
}
|
||||
|
||||
val senderName = namePhoto.name
|
||||
val photoUri = namePhoto.photoUri ?: ""
|
||||
val contact = Contact(0, senderName, photoUri, senderNumber)
|
||||
contacts.add(contact)
|
||||
}
|
||||
|
||||
return contacts
|
||||
}
|
||||
|
||||
fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
val names = getContactNames()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue