Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.fossify.phone.R
import org.fossify.phone.activities.SimpleActivity
import org.fossify.phone.adapters.ContactsAdapter
import org.fossify.phone.databinding.DialogSelectContactBinding
import org.fossify.phone.extensions.matchesSearchQuery
import org.fossify.phone.extensions.setupWithContacts

class SelectContactDialog(val activity: SimpleActivity, val contacts: List<Contact>, val callback: (selectedContact: Contact) -> Unit) {
Expand Down Expand Up @@ -97,8 +98,10 @@ class SelectContactDialog(val activity: SimpleActivity, val contacts: List<Conta
private fun filterContactListBySearchQuery(query: String) {
val adapter = binding.selectContactList.adapter as? ContactsAdapter
var contactsToShow = contacts
if (query.isNotEmpty()) {
contactsToShow = contacts.filter { it.name.contains(query, true) }
val fixedQuery = query.trim().replace("\\s+".toRegex(), " ")
if (fixedQuery.isNotEmpty()) {
val shouldNormalize = fixedQuery.normalizeString() == fixedQuery
contactsToShow = contacts.filter { it.matchesSearchQuery(fixedQuery, shouldNormalize) }
}
checkPlaceholderVisibility(contactsToShow)

Expand Down
17 changes: 17 additions & 0 deletions app/src/main/kotlin/org/fossify/phone/extensions/Contact.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.fossify.phone.extensions

import org.fossify.commons.helpers.getProperText
import org.fossify.commons.models.contacts.Contact

fun Contact.matchesSearchQuery(query: String, shouldNormalize: Boolean): Boolean {
return getProperText(getNameToDisplay(), shouldNormalize).contains(query, true) ||
getProperText(nickname, shouldNormalize).contains(query, true) ||
(query.toLongOrNull() != null && doesContainPhoneNumber(query, true)) ||
emails.any { it.value.contains(query, true) } ||
addresses.any { getProperText(it.value, shouldNormalize).contains(query, true) } ||
IMs.any { it.value.contains(query, true) } ||
getProperText(notes, shouldNormalize).contains(query, true) ||
getProperText(organization.company, shouldNormalize).contains(query, true) ||
getProperText(organization.jobPosition, shouldNormalize).contains(query, true) ||
websites.any { it.contains(query, true) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.fossify.phone.databinding.FragmentContactsBinding
import org.fossify.phone.databinding.FragmentLettersLayoutBinding
import org.fossify.phone.extensions.handleGenericContactClick
import org.fossify.phone.extensions.launchCreateNewContactIntent
import org.fossify.phone.extensions.matchesSearchQuery
import org.fossify.phone.extensions.setupWithContacts
import org.fossify.phone.extensions.startContactDetailsIntent
import org.fossify.phone.interfaces.RefreshItemsListener
Expand Down Expand Up @@ -159,18 +160,7 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
override fun onSearchQueryChanged(text: String) {
val fixedText = text.trim().replace("\\s+".toRegex(), " ")
val shouldNormalize = fixedText.normalizeString() == fixedText
val filtered = allContacts.filter { contact ->
getProperText(contact.getNameToDisplay(), shouldNormalize).contains(fixedText, true) ||
getProperText(contact.nickname, shouldNormalize).contains(fixedText, true) ||
(fixedText.toLongOrNull() != null && contact.doesContainPhoneNumber(fixedText, true)) ||
contact.emails.any { it.value.contains(fixedText, true) } ||
contact.addresses.any { getProperText(it.value, shouldNormalize).contains(fixedText, true) } ||
contact.IMs.any { it.value.contains(fixedText, true) } ||
getProperText(contact.notes, shouldNormalize).contains(fixedText, true) ||
getProperText(contact.organization.company, shouldNormalize).contains(fixedText, true) ||
getProperText(contact.organization.jobPosition, shouldNormalize).contains(fixedText, true) ||
contact.websites.any { it.contains(fixedText, true) }
} as ArrayList
val filtered = allContacts.filter { it.matchesSearchQuery(fixedText, shouldNormalize) } as ArrayList

filtered.sortBy {
val nameToDisplay = it.getNameToDisplay()
Expand Down
Loading