implement the Filter functionality

This commit is contained in:
tibbi 2017-12-12 22:37:56 +01:00
parent 7b849cb090
commit 92fc1bee02
5 changed files with 19 additions and 3 deletions

View File

@ -151,7 +151,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
}
contacts_fastscroller.setViews(contacts_list) {
val item = contacts.getOrNull(it)
val item = (contacts_list.adapter as ContactsAdapter).contactItems.getOrNull(it)
contacts_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
}
} else {

View File

@ -18,7 +18,7 @@ class FilterContactSourcesAdapter(val activity: SimpleActivity, val contactSourc
init {
contactSources.forEachIndexed { index, value ->
if ((displayContactSources.size == 1 && displayContactSources.first() == "-1") || displayContactSources.contains(value)) {
if (activity.config.showAllContacts() || displayContactSources.contains(value)) {
selectedPositions.add(index)
}
}

View File

@ -20,4 +20,6 @@ class Config(context: Context) : BaseConfig(context) {
var displayContactSources: Set<String>
get() = prefs.getStringSet(DISPLAY_CONTACT_SOURCES, hashSetOf("-1"))
set(displayContactSources) = prefs.edit().remove(DISPLAY_CONTACT_SOURCES).putStringSet(DISPLAY_CONTACT_SOURCES, displayContactSources).apply()
fun showAllContacts() = displayContactSources.size == 1 && displayContactSources.first() == "-1"
}

View File

@ -10,6 +10,7 @@ import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.overloads.times
import java.util.*
class ContactsHelper(val activity: SimpleActivity) {
@ -38,12 +39,16 @@ class ContactsHelper(val activity: SimpleActivity) {
fun getContacts(callback: (ArrayList<Contact>) -> Unit) {
val contacts = HashMap<Int, Contact>()
Thread {
val sources = activity.config.displayContactSources
val questionMarks = ("?," * sources.size).trimEnd(',')
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = getContactProjection()
val selection = if (activity.config.showAllContacts()) null else "${ContactsContract.RawContacts.ACCOUNT_NAME} IN ($questionMarks)"
val selectionArgs = if (activity.config.showAllContacts()) null else sources.toTypedArray()
val sortOrder = getSortString()
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, sortOrder)
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)

View File

@ -0,0 +1,9 @@
package com.simplemobiletools.contacts.overloads
operator fun String.times(x: Int): String {
val stringBuffer = StringBuffer()
for (i in 1..x) {
stringBuffer.append(this)
}
return stringBuffer.toString()
}