forked from upstream-mirrors/quik
Allow normalized search when filtering contacts
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of QKSMS.
|
||||||
|
*
|
||||||
|
* QKSMS is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* QKSMS is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package util.extensions
|
||||||
|
|
||||||
|
import java.text.Normalizer
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strip the accents from a string
|
||||||
|
*/
|
||||||
|
fun CharSequence.removeAccents() = Normalizer.normalize(this, Normalizer.Form.NFD)
|
||||||
|
.replace(Regex("[^\\p{ASCII}]"), "")
|
||||||
@@ -19,12 +19,13 @@
|
|||||||
package common.util.filter
|
package common.util.filter
|
||||||
|
|
||||||
import model.Contact
|
import model.Contact
|
||||||
|
import util.extensions.removeAccents
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ContactFilter @Inject constructor(private val phoneNumberFilter: PhoneNumberFilter) : Filter<Contact>() {
|
class ContactFilter @Inject constructor(private val phoneNumberFilter: PhoneNumberFilter) : Filter<Contact>() {
|
||||||
|
|
||||||
override fun filter(item: Contact, query: CharSequence): Boolean {
|
override fun filter(item: Contact, query: CharSequence): Boolean {
|
||||||
return item.name.contains(query, true) || // Name
|
return item.name.removeAccents().contains(query, true) || // Name
|
||||||
item.numbers.map { it.address }.any { address -> phoneNumberFilter.filter(address, query) } // Number
|
item.numbers.map { it.address }.any { address -> phoneNumberFilter.filter(address, query) } // Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ import repository.MessageRepository
|
|||||||
import util.extensions.asObservable
|
import util.extensions.asObservable
|
||||||
import util.extensions.isImage
|
import util.extensions.isImage
|
||||||
import util.extensions.mapNotNull
|
import util.extensions.mapNotNull
|
||||||
|
import util.extensions.removeAccents
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
@@ -206,9 +207,14 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
// that have already been selected
|
// that have already been selected
|
||||||
Observables
|
Observables
|
||||||
.combineLatest(view.queryChangedIntent, contacts, selectedContacts, { query, contacts, selectedContacts ->
|
.combineLatest(view.queryChangedIntent, contacts, selectedContacts, { query, contacts, selectedContacts ->
|
||||||
|
|
||||||
|
// Strip the accents from the query. This can be an expensive operation, so
|
||||||
|
// cache the result instead of doing it for each contact
|
||||||
|
val normalizedQuery = query.removeAccents()
|
||||||
|
|
||||||
var filteredContacts = contacts
|
var filteredContacts = contacts
|
||||||
.filterNot { contact -> selectedContacts.contains(contact) }
|
.filterNot { contact -> selectedContacts.contains(contact) }
|
||||||
.filter { contact -> contactFilter.filter(contact, query) }
|
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
||||||
|
|
||||||
// If the entry is a valid destination, allow it as a recipient
|
// If the entry is a valid destination, allow it as a recipient
|
||||||
if (PhoneNumberUtils.isWellFormedSmsAddress(query.toString())) {
|
if (PhoneNumberUtils.isWellFormedSmsAddress(query.toString())) {
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ import io.realm.Realm
|
|||||||
import model.SyncLog
|
import model.SyncLog
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
import util.Preferences
|
import util.Preferences
|
||||||
|
import util.extensions.removeAccents
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -117,6 +118,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
|
|
||||||
view.queryChangedIntent
|
view.queryChangedIntent
|
||||||
.debounce(200, TimeUnit.MILLISECONDS)
|
.debounce(200, TimeUnit.MILLISECONDS)
|
||||||
|
.map { query -> query.removeAccents() }
|
||||||
.withLatestFrom(state, { query, state ->
|
.withLatestFrom(state, { query, state ->
|
||||||
if (state.page is Inbox) {
|
if (state.page is Inbox) {
|
||||||
val filteredConversations = if (query.isEmpty()) conversations
|
val filteredConversations = if (query.isEmpty()) conversations
|
||||||
|
|||||||
Reference in New Issue
Block a user