From bcec8ed25befdc9f0d8832901c85a183ad32875c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 5 Jan 2019 21:42:23 +0100 Subject: [PATCH] improve text highlighting, if it contains numbers --- build.gradle | 2 +- .../commons/extensions/Activity.kt | 6 ++++-- .../commons/extensions/String.kt | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index ab39bdc18..40ea4ed85 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { propMinSdkVersion = 21 propTargetSdkVersion = propCompileSdkVersion propVersionCode = 1 - propVersionName = '5.6.3' + propVersionName = '5.6.4' kotlin_version = '1.3.11' } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt index afb292dc8..588265d99 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -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.* diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/String.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/String.kt index a9161c30a..207966a41 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/String.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/String.kt @@ -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 {