presort contacts already at fetching

This commit is contained in:
tibbi 2017-12-11 18:21:58 +01:00
parent a9821488b6
commit 6c4673e371
1 changed files with 18 additions and 1 deletions

View File

@ -5,7 +5,10 @@ import android.provider.ContactsContract
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
class ContactsHelper(val activity: SimpleActivity) { class ContactsHelper(val activity: SimpleActivity) {
@ -14,9 +17,10 @@ class ContactsHelper(val activity: SimpleActivity) {
Thread { Thread {
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = getContactProjection() val projection = getContactProjection()
val sortOrder = getSortString()
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = activity.contentResolver.query(uri, projection, null, null, null) cursor = activity.contentResolver.query(uri, projection, null, null, sortOrder)
if (cursor?.moveToFirst() == true) { if (cursor?.moveToFirst() == true) {
do { do {
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID) val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
@ -67,4 +71,17 @@ class ContactsHelper(val activity: SimpleActivity) {
ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI ContactsContract.CommonDataKinds.Phone.PHOTO_URI
) )
private fun getSortString(): String {
val sorting = activity.config.sorting
var sort = when {
sorting and SORT_BY_NAME != 0 -> "${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} COLLATE NOCASE"
else -> ContactsContract.CommonDataKinds.Phone.NUMBER
}
if (sorting and SORT_DESCENDING != 0) {
sort += " DESC"
}
return sort
}
} }