apply search to emails too

This commit is contained in:
tibbi 2017-12-31 10:02:35 +01:00
parent ff3f2ea7a3
commit 00e5fdde5e
2 changed files with 19 additions and 8 deletions

View File

@ -159,7 +159,9 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fun onSearchQueryChanged(text: String) { fun onSearchQueryChanged(text: String) {
(fragment_list.adapter as ContactsAdapter).apply { (fragment_list.adapter as ContactsAdapter).apply {
val filtered = contactsIgnoringSearch.filter { val filtered = contactsIgnoringSearch.filter {
it.getFullName(startNameWithSurname).contains(text, true) || it.phoneNumbers.any { it.value.contains(text, true) } it.getFullName(startNameWithSurname).contains(text, true) ||
it.phoneNumbers.any { it.value.contains(text, true) } ||
it.emails.any { it.value.contains(text, true) }
} as ArrayList } as ArrayList
updateItems(filtered) updateItems(filtered)
} }

View File

@ -65,12 +65,19 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
} }
val phoneNumbers = getPhoneNumbers() val phoneNumbers = getPhoneNumbers()
val size = phoneNumbers.size() var size = phoneNumbers.size()
for (i in 0 until size) { for (i in 0 until size) {
val key = phoneNumbers.keyAt(i) val key = phoneNumbers.keyAt(i)
contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i) contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i)
} }
val emails = getEmails()
size = emails.size()
for (i in 0 until size) {
val key = emails.keyAt(i)
contacts[key]?.emails = emails.valueAt(i)
}
val contactsSize = contacts.size() val contactsSize = contacts.size()
val resultContacts = ArrayList<Contact>(contactsSize) val resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) } (0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
@ -112,30 +119,32 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return phoneNumbers return phoneNumbers
} }
private fun getEmails(contactId: Int): SparseArray<ArrayList<Email>> { private fun getEmails(contactId: Int? = null): SparseArray<ArrayList<Email>> {
val emails = SparseArray<ArrayList<Email>>() val emails = SparseArray<ArrayList<Email>>()
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE ContactsContract.CommonDataKinds.Email.TYPE
) )
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(contactId.toString()) val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString())
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) { if (cursor?.moveToFirst() == true) {
do { do {
val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA) val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Email.TYPE) val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Email.TYPE)
if (emails[contactId] == null) { if (emails[id] == null) {
emails.put(contactId, ArrayList()) emails.put(id, ArrayList())
} }
emails[contactId]!!.add(Email(email, type)) emails[id]!!.add(Email(email, type))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} finally { } finally {