improve name highlighting from the dialpad

This commit is contained in:
tibbi 2019-01-06 11:16:54 +01:00
parent defb7f9bf0
commit b7fb0b979c
1 changed files with 27 additions and 1 deletions

View File

@ -1,6 +1,10 @@
package com.simplemobiletools.contacts.pro.adapters package com.simplemobiletools.contacts.pro.adapters
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.telephony.PhoneNumberUtils
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.Menu import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -255,7 +259,14 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
view.apply { view.apply {
contact_frame?.isSelected = selectedKeys.contains(contact.id) contact_frame?.isSelected = selectedKeys.contains(contact.id)
val fullName = contact.getNameToDisplay() val fullName = contact.getNameToDisplay()
contact_name.text = if (textToHighlight.isEmpty()) fullName else fullName.highlightTextPart(textToHighlight, adjustedPrimaryColor) contact_name.text = if (textToHighlight.isEmpty()) fullName else {
if (fullName.contains(textToHighlight, true)) {
fullName.highlightTextPart(textToHighlight, adjustedPrimaryColor)
} else {
highlightTextFromNumbers(fullName, textToHighlight)
}
}
contact_name.setTextColor(textColor) contact_name.setTextColor(textColor)
contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0) contact_name.setPadding(if (showContactThumbnails) smallPadding else bigPadding, smallPadding, smallPadding, 0)
@ -300,4 +311,19 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
} }
} }
} }
private fun highlightTextFromNumbers(name: String, textToHighlight: String): SpannableString {
val spannableString = SpannableString(name)
val digits = PhoneNumberUtils.convertKeypadLettersToDigits(name)
if (digits.contains(textToHighlight)) {
val startIndex = digits.indexOf(textToHighlight, 0, true)
val endIndex = Math.min(startIndex + textToHighlight.length, name.length)
try {
spannableString.setSpan(ForegroundColorSpan(adjustedPrimaryColor), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
} catch (ignored: IndexOutOfBoundsException) {
}
}
return spannableString
}
} }