improve text highlighting, if it contains numbers
This commit is contained in:
parent
f3b43e0398
commit
bcec8ed25b
3 changed files with 23 additions and 4 deletions
|
@ -7,7 +7,7 @@ buildscript {
|
|||
propMinSdkVersion = 21
|
||||
propTargetSdkVersion = propCompileSdkVersion
|
||||
propVersionCode = 1
|
||||
propVersionName = '5.6.3'
|
||||
propVersionName = '5.6.4'
|
||||
kotlin_version = '1.3.11'
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,6 @@ package com.simplemobiletools.commons.extensions
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.AlertDialog
|
||||
import android.app.ProgressDialog.show
|
||||
import android.content.*
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageManager
|
||||
|
@ -14,6 +12,7 @@ import android.os.Looper
|
|||
import android.os.TransactionTooLargeException
|
||||
import android.provider.DocumentsContract
|
||||
import android.provider.MediaStore
|
||||
import android.text.Html
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.Window
|
||||
|
@ -21,6 +20,9 @@ import android.view.WindowManager
|
|||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||
import com.simplemobiletools.commons.dialogs.*
|
||||
|
|
|
@ -10,11 +10,13 @@ import android.media.MediaMetadataRetriever
|
|||
import android.os.Build
|
||||
import android.text.Spannable
|
||||
import android.text.SpannableString
|
||||
import android.text.TextUtils
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import com.simplemobiletools.commons.helpers.*
|
||||
import java.text.Normalizer
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
fun String.getFilenameFromPath() = substring(lastIndexOf("/") + 1)
|
||||
|
||||
|
@ -240,7 +242,7 @@ fun String.substringTo(cnt: Int): String {
|
|||
}
|
||||
}
|
||||
|
||||
fun String.highlightTextPart(textToHighlight: String, color: Int, highlightAll: Boolean = false): SpannableString {
|
||||
fun String.highlightTextPart(textToHighlight: String, color: Int, highlightAll: Boolean = false, ignoreCharsBetweenDigits: Boolean = false): SpannableString {
|
||||
val spannableString = SpannableString(this)
|
||||
if (textToHighlight.isEmpty()) {
|
||||
return spannableString
|
||||
|
@ -259,6 +261,21 @@ fun String.highlightTextPart(textToHighlight: String, color: Int, highlightAll:
|
|||
}
|
||||
}
|
||||
|
||||
// handle cases when we search for 643, but in reality the string contains it like 6-43
|
||||
if (ignoreCharsBetweenDigits && indexes.isEmpty()) {
|
||||
val regex = TextUtils.join("(\\D*)", textToHighlight.toCharArray().toTypedArray())
|
||||
val pattern = Pattern.compile(regex)
|
||||
val result = pattern.matcher(normalizeString())
|
||||
if (result.find()) {
|
||||
try {
|
||||
spannableString.setSpan(ForegroundColorSpan(color), result.start(), result.end(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
return spannableString
|
||||
}
|
||||
|
||||
indexes.forEach {
|
||||
val endIndex = Math.min(it + textToHighlight.length, length)
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue