make string normalizing a string extension

This commit is contained in:
tibbi 2018-08-12 19:24:30 +02:00
parent 6bf77d73bd
commit fd775bf050
3 changed files with 17 additions and 12 deletions

View File

@ -0,0 +1,6 @@
package com.simplemobiletools.contacts.extensions
import com.simplemobiletools.contacts.helpers.normalizeRegex
import java.text.Normalizer
fun String.normalizeString() = Normalizer.normalize(this, Normalizer.Form.NFD).replace(normalizeRegex, "")

View File

@ -122,3 +122,5 @@ val localAccountTypes = arrayListOf("vnd.sec.contact.phone",
"com.android.huawei.phone",
"Local Phone Account"
)
val normalizeRegex = "\\p{InCombiningDiacriticalMarks}+".toRegex()

View File

@ -4,8 +4,8 @@ import android.graphics.Bitmap
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.extensions.normalizeString
import com.simplemobiletools.contacts.helpers.PHONE_NUMBER_PATTERN
import java.text.Normalizer
data class Contact(val id: Int, var prefix: String, var firstName: String, var middleName: String, var surname: String, var suffix: String, var photoUri: String,
var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var addresses: ArrayList<Address>, var events: ArrayList<Event>,
@ -14,7 +14,6 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
companion object {
var sorting = 0
var startWithSurname = false
val regex = "\\p{InCombiningDiacriticalMarks}+".toRegex()
}
override fun compareTo(other: Contact): Int {
@ -23,25 +22,25 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
when {
sorting and SORT_BY_FIRST_NAME != 0 -> {
firstString = normalizeString(firstName)
secondString = normalizeString(other.firstName)
firstString = firstName.normalizeString()
secondString = other.firstName.normalizeString()
}
sorting and SORT_BY_MIDDLE_NAME != 0 -> {
firstString = normalizeString(middleName)
secondString = normalizeString(other.middleName)
firstString = middleName.normalizeString()
secondString = other.middleName.normalizeString()
}
else -> {
firstString = normalizeString(surname)
secondString = normalizeString(other.surname)
firstString = surname.normalizeString()
secondString = other.surname.normalizeString()
}
}
if (firstString.isEmpty() && firstName.isEmpty() && middleName.isEmpty() && surname.isEmpty() && organization.company.isNotEmpty()) {
firstString = normalizeString(organization.company)
firstString = organization.company.normalizeString()
}
if (secondString.isEmpty() && other.firstName.isEmpty() && other.middleName.isEmpty() && other.surname.isEmpty() && other.organization.company.isNotEmpty()) {
secondString = normalizeString(other.organization.company)
secondString = other.organization.company.normalizeString()
}
var result = if (firstString.firstOrNull()?.isLetter() == true && secondString.firstOrNull()?.isLetter() == false) {
@ -69,8 +68,6 @@ data class Contact(val id: Int, var prefix: String, var firstName: String, var m
return result
}
private fun normalizeString(string: String) = Normalizer.normalize(string, Normalizer.Form.NFD).replace(regex, "")
fun getBubbleText() = when {
sorting and SORT_BY_FIRST_NAME != 0 -> firstName
sorting and SORT_BY_MIDDLE_NAME != 0 -> middleName