make getAvailableContacts asynchronous

This commit is contained in:
tibbi 2020-04-06 22:06:58 +02:00
parent fdf4957da2
commit 97299a0900
2 changed files with 34 additions and 31 deletions

View File

@ -1,19 +1,15 @@
package com.simplemobiletools.smsmessenger.activities
import android.content.Intent
import android.database.Cursor
import android.os.Bundle
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds
import android.text.TextUtils
import android.view.WindowManager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.onTextChangeListener
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.adapters.ContactsAdapter
import com.simplemobiletools.smsmessenger.extensions.getAvailableContacts
import com.simplemobiletools.smsmessenger.extensions.getContactNames
import com.simplemobiletools.smsmessenger.extensions.getContactPhoneNumbers
import com.simplemobiletools.smsmessenger.extensions.getThreadId
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
@ -40,8 +36,12 @@ class NewMessageActivity : SimpleActivity() {
}
private fun initContacts() {
allContacts = getAvailableContacts()
setupAdapter(allContacts)
getAvailableContacts {
allContacts = it
runOnUiThread {
setupAdapter(allContacts)
}
}
new_message_to.onTextChangeListener {
val searchString = it

View File

@ -10,6 +10,7 @@ import android.provider.Telephony
import android.text.TextUtils
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.smsmessenger.helpers.Config
import com.simplemobiletools.smsmessenger.models.Contact
@ -179,32 +180,34 @@ fun Context.getPersonsName(id: Int): String? {
return null
}
fun Context.getAvailableContacts(): ArrayList<Contact> {
val names = getContactNames()
var allContacts = getContactPhoneNumbers()
allContacts.forEach {
val contactId = it.id
val contact = names.firstOrNull { it.id == contactId }
val name = contact?.name
if (name != null) {
it.name = name
fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
ensureBackgroundThread {
val names = getContactNames()
var allContacts = getContactPhoneNumbers()
allContacts.forEach {
val contactId = it.id
val contact = names.firstOrNull { it.id == contactId }
val name = contact?.name
if (name != null) {
it.name = name
}
val photoUri = contact?.photoUri
if (photoUri != null) {
it.photoUri = photoUri
}
it.isOrganization = contact?.isOrganization ?: false
}
val photoUri = contact?.photoUri
if (photoUri != null) {
it.photoUri = photoUri
}
allContacts = allContacts.filter { it.name.isNotEmpty() }.distinctBy {
val startIndex = Math.max(0, it.phoneNumber.length - 9)
it.phoneNumber.substring(startIndex)
}.toMutableList() as ArrayList<Contact>
it.isOrganization = contact?.isOrganization ?: false
allContacts.sortBy { it.name.normalizeString().toLowerCase() }
callback(allContacts)
}
allContacts = allContacts.filter { it.name.isNotEmpty() }.distinctBy {
val startIndex = Math.max(0, it.phoneNumber.length - 9)
it.phoneNumber.substring(startIndex)
}.toMutableList() as ArrayList<Contact>
allContacts.sortBy { it.name.normalizeString().toLowerCase() }
return allContacts
}
fun Context.getNameFromPhoneNumber(number: String): Int? {