From 00e5fdde5ec1cd3bac875008387485fedfa896eb Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 31 Dec 2017 10:02:35 +0100 Subject: [PATCH] apply search to emails too --- .../contacts/fragments/MyViewPagerFragment.kt | 4 +++- .../contacts/helpers/ContactsHelper.kt | 23 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt index 296c4a4e..0800aefe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt @@ -159,7 +159,9 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) fun onSearchQueryChanged(text: String) { (fragment_list.adapter as ContactsAdapter).apply { 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 updateItems(filtered) } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 8b83c8f1..11cf1a24 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -65,12 +65,19 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } val phoneNumbers = getPhoneNumbers() - val size = phoneNumbers.size() + var size = phoneNumbers.size() for (i in 0 until size) { val key = phoneNumbers.keyAt(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 resultContacts = ArrayList(contactsSize) (0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) } @@ -112,30 +119,32 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return phoneNumbers } - private fun getEmails(contactId: Int): SparseArray> { + private fun getEmails(contactId: Int? = null): SparseArray> { val emails = SparseArray>() val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI val projection = arrayOf( + ContactsContract.Data.RAW_CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE ) - val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" - val selectionArgs = arrayOf(contactId.toString()) + val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?" + val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString()) var cursor: Cursor? = null try { cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) if (cursor?.moveToFirst() == true) { do { + val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA) val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Email.TYPE) - if (emails[contactId] == null) { - emails.put(contactId, ArrayList()) + if (emails[id] == null) { + emails.put(id, ArrayList()) } - emails[contactId]!!.add(Email(email, type)) + emails[id]!!.add(Email(email, type)) } while (cursor.moveToNext()) } } finally {