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()
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,7 +180,8 @@ fun Context.getPersonsName(id: Int): String? {
return null
}
fun Context.getAvailableContacts(): ArrayList<Contact> {
fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
ensureBackgroundThread {
val names = getContactNames()
var allContacts = getContactPhoneNumbers()
allContacts.forEach {
@ -204,7 +206,8 @@ fun Context.getAvailableContacts(): ArrayList<Contact> {
}.toMutableList() as ArrayList<Contact>
allContacts.sortBy { it.name.normalizeString().toLowerCase() }
return allContacts
callback(allContacts)
}
}
fun Context.getNameFromPhoneNumber(number: String): Int? {