remove special chars from phone numbers at creating, not just at sorting

This commit is contained in:
tibbi 2018-09-04 10:09:59 +02:00
parent 461ffdfd0e
commit 26c4da4f69
5 changed files with 25 additions and 14 deletions

View File

@ -507,7 +507,7 @@ class EditContactActivity : ContactActivity() {
originalContactSource = if (hasContactPermissions()) config.lastUsedContactSource else SMT_PRIVATE
val organization = Organization("", "")
contact = Contact(0, "", "", "", "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), originalContactSource, 0, 0, "",
null, "", ArrayList(), organization, ArrayList())
null, "", ArrayList(), organization, ArrayList(), ArrayList())
contact_source.text = getPublicContactSource(contact!!.source)
}

View File

@ -24,6 +24,7 @@ import com.simplemobiletools.contacts.models.*
import com.simplemobiletools.contacts.overloads.times
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
class ContactsHelper(val activity: Activity) {
private val BATCH_SIZE = 100
@ -127,7 +128,7 @@ class ContactsHelper(val activity: Activity) {
val suffix = cursor.getStringValue(CommonDataKinds.StructuredName.SUFFIX) ?: ""
val nickname = ""
val photoUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_URI) ?: ""
val number = ArrayList<PhoneNumber>() // proper value is obtained below
val numbers = ArrayList<PhoneNumber>() // proper value is obtained below
val emails = ArrayList<Email>()
val addresses = ArrayList<Address>()
val events = ArrayList<Event>()
@ -139,8 +140,9 @@ class ContactsHelper(val activity: Activity) {
val groups = ArrayList<Group>()
val organization = Organization("", "")
val websites = ArrayList<String>()
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, nickname, photoUri, number, emails, addresses,
events, accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites)
val cleanNumbers = ArrayList<PhoneNumber>()
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, nickname, photoUri, numbers, emails, addresses,
events, accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites, cleanNumbers)
contacts.put(id, contact)
} while (cursor.moveToNext())
@ -155,7 +157,15 @@ class ContactsHelper(val activity: Activity) {
var size = phoneNumbers.size()
for (i in 0 until size) {
val key = phoneNumbers.keyAt(i)
contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i)
if (contacts[key] != null) {
val numbers = phoneNumbers.valueAt(i)
contacts[key].phoneNumbers = numbers
// 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, "") }
}
}
}
val nicknames = getNicknames()
@ -726,7 +736,7 @@ class ContactsHelper(val activity: Activity) {
val organization = getOrganizations(id)[id] ?: Organization("", "")
val websites = getWebsites(id)[id] ?: ArrayList()
return Contact(id, prefix, firstName, middleName, surname, suffix, nickname, photoUri, number, emails, addresses, events,
accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites)
accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites, ArrayList())
}
} finally {
cursor?.close()

View File

@ -335,8 +335,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val websites = if (websitesJson == "[]") ArrayList() else gson.fromJson<ArrayList<String>>(websitesJson, websitesToken)
?: ArrayList(1)
val cleanPhoneNumbers = ArrayList<PhoneNumber>()
phoneNumbers.mapTo(cleanPhoneNumbers) { PhoneNumber(it.value.replace(PHONE_NUMBER_PATTERN.toRegex(), ""), 0, "") }
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, nickname, "", phoneNumbers, emails, addresses,
events, SMT_PRIVATE, starred, id, "", photo, notes, groups, organization, websites)
events, SMT_PRIVATE, starred, id, "", photo, notes, groups, organization, websites, cleanPhoneNumbers)
contacts.add(contact)
}
}

View File

@ -110,9 +110,10 @@ class VcfImporter(val activity: SimpleActivity) {
val photoData = ezContact.photos.firstOrNull()?.data
val photo = null
val thumbnailUri = savePhoto(photoData)
val cleanPhoneNumbers = ArrayList<PhoneNumber>()
val contact = Contact(0, prefix, firstName, middleName, surname, suffix, nickname, photoUri, phoneNumbers, emails, addresses, events,
targetContactSource, starred, contactId, thumbnailUri, photo, notes, groups, organization, websites)
targetContactSource, starred, contactId, thumbnailUri, photo, notes, groups, organization, websites, cleanPhoneNumbers)
if (ContactsHelper(activity).insertContact(contact)) {
contactsImported++

View File

@ -5,12 +5,12 @@ 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
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>,
var events: ArrayList<Event>, var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?, var notes: String,
var groups: ArrayList<Group>, var organization: Organization, var websites: ArrayList<String>) : Comparable<Contact> {
var groups: ArrayList<Group>, var organization: Organization, var websites: ArrayList<String>, var cleanPhoneNumbers: ArrayList<PhoneNumber>) :
Comparable<Contact> {
companion object {
var sorting = 0
var startWithSurname = false
@ -93,14 +93,11 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
}
fun getStringToCompare(): String {
val newPhoneNumbers = ArrayList<PhoneNumber>()
phoneNumbers.mapTo(newPhoneNumbers) { PhoneNumber(it.value.replace(PHONE_NUMBER_PATTERN.toRegex(), ""), 0, "") }
val newEmails = ArrayList<Email>()
emails.mapTo(newEmails) { Email(it.value, 0, "") }
return copy(id = 0, prefix = "", firstName = getFullName().toLowerCase(), middleName = "", surname = "", suffix = "", nickname = "", photoUri = "",
phoneNumbers = phoneNumbers, events = ArrayList(), addresses = ArrayList(), emails = newEmails, source = "", starred = 0,
phoneNumbers = ArrayList(), events = ArrayList(), addresses = ArrayList(), emails = newEmails, source = "", starred = 0,
contactId = 0, thumbnailUri = "", notes = "", groups = ArrayList(), websites = ArrayList(), organization = Organization("", "")).toString()
}