adding a helper extension for filtering phone numbers

This commit is contained in:
tibbi 2018-09-28 12:02:04 +02:00
parent 446db13091
commit 27e1366339
5 changed files with 13 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.extensions
import android.widget.TextView
import com.simplemobiletools.commons.helpers.getDateFormats
import com.simplemobiletools.contacts.helpers.PHONE_NUMBER_PATTERN
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import java.text.DateFormat
@ -32,3 +33,5 @@ fun String.getDateTimeFromDateString(viewToUpdate: TextView? = null): DateTime {
}
return date
}
fun String.applyRegexFiltering() = replace(PHONE_NUMBER_PATTERN.toRegex(), "")

View File

@ -10,10 +10,10 @@ import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALL_LOG
import com.simplemobiletools.contacts.activities.EditContactActivity
import com.simplemobiletools.contacts.adapters.RecentCallsAdapter
import com.simplemobiletools.contacts.extensions.applyRegexFiltering
import com.simplemobiletools.contacts.extensions.contactClicked
import com.simplemobiletools.contacts.helpers.IS_FROM_SIMPLE_CONTACTS
import com.simplemobiletools.contacts.helpers.KEY_PHONE
import com.simplemobiletools.contacts.helpers.PHONE_NUMBER_PATTERN
import com.simplemobiletools.contacts.helpers.RECENTS_TAB_MASK
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.RecentCall
@ -44,10 +44,10 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
val currAdapter = fragment_list.adapter
if (currAdapter == null) {
RecentCallsAdapter(activity!!, recentCalls, activity, fragment_list, fragment_fastscroller) {
val recentCall = (it as RecentCall).number.replace(PHONE_NUMBER_PATTERN.toRegex(), "")
val recentCall = (it as RecentCall).number.applyRegexFiltering()
var selectedContact: Contact? = null
for (contact in allContacts) {
if (contact.phoneNumbers.any { it.value.replace(PHONE_NUMBER_PATTERN.toRegex(), "") == recentCall }) {
if (contact.phoneNumbers.any { it.value.applyRegexFiltering() == recentCall }) {
selectedContact = contact
break
}

View File

@ -166,7 +166,7 @@ class ContactsHelper(val activity: Activity) {
if (filterDuplicates) {
// remove all spaces, dashes etc from numbers for easier comparing, used only at list views
numbers.forEach {
numbers.mapTo(contacts[key].cleanPhoneNumbers) { PhoneNumber(it.value.replace(PHONE_NUMBER_PATTERN.toRegex(), ""), 0, "") }
numbers.mapTo(contacts[key].cleanPhoneNumbers) { PhoneNumber(it.value.applyRegexFiltering(), 0, "") }
}
}
}

View File

@ -16,6 +16,7 @@ import com.simplemobiletools.commons.extensions.getBlobValue
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.contacts.extensions.applyRegexFiltering
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.getByteArray
import com.simplemobiletools.contacts.extensions.getPhotoThumbnailSize
@ -349,7 +350,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val cleanPhoneNumbers = ArrayList<PhoneNumber>()
if (filterDuplicates) {
phoneNumbers.mapTo(cleanPhoneNumbers) { PhoneNumber(it.value.replace(PHONE_NUMBER_PATTERN.toRegex(), ""), 0, "") }
phoneNumbers.mapTo(cleanPhoneNumbers) { PhoneNumber(it.value.applyRegexFiltering(), 0, "") }
}
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, nickname, "", phoneNumbers, emails, addresses,

View File

@ -5,7 +5,7 @@ import com.simplemobiletools.commons.extensions.normalizeString
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.contacts.helpers.PHONE_NUMBER_PATTERN
import com.simplemobiletools.contacts.extensions.applyRegexFiltering
data class Contact(val id: Int, var prefix: String, var firstName: String, var middleName: String, var surname: String, var suffix: String, var nickname: String,
var photoUri: String, var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var addresses: ArrayList<Address>,
@ -114,9 +114,9 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
}
}
val numericOnly = text.replace(PHONE_NUMBER_PATTERN.toRegex(), "")
if (numericOnly.isNotEmpty()) {
if (phoneNumbers.any { it.value.contains(numericOnly) } || cleanPhoneNumbers.any { it.value.contains(numericOnly) }) {
val filteredNumber = text.applyRegexFiltering()
if (filteredNumber.isNotEmpty()) {
if (phoneNumbers.any { it.value.contains(filteredNumber) } || cleanPhoneNumbers.any { it.value.contains(filteredNumber) }) {
return true
}
}