modify DisplayUtils and MessageUtils

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2025-02-07 10:03:41 +01:00
parent 6dfd3b6956
commit 522d18e9d8
No known key found for this signature in database
GPG Key ID: F7AA2A8B65B50220
2 changed files with 37 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.TextUtils
@ -224,6 +225,28 @@ object DisplayUtils {
return chip
}
fun replaceLabelWithPlaceholder(
text: Spanned,
labelToSearch: String,
placeholder: String
): Spannable {
val spannableStringBuilder = SpannableStringBuilder(text)
val stringText = text.toString()
val m = Pattern.compile(
Pattern.quote(labelToSearch),
Pattern.CASE_INSENSITIVE or Pattern.LITERAL or Pattern.MULTILINE
)
.matcher(spannableStringBuilder)
var lastStartIndex = 0
while (m.find()) {
val start = stringText.indexOf(m.group(), lastStartIndex)
val end = start + m.group().length
lastStartIndex = end
spannableStringBuilder.replace(start, end, placeholder)
}
return spannableStringBuilder
}
fun searchAndReplaceWithMentionSpan(
key: String,
context: Context,

View File

@ -118,6 +118,19 @@ class MessageUtils(val context: Context) {
} else {
individualHashMap["id"]
}
val label = individualHashMap["name"]!!
val type = individualHashMap["type"]!!
val labelToSearch = if (type == "circle") {
"@team/$label"
} else {
"@$label"
}
messageStringInternal = DisplayUtils.replaceLabelWithPlaceholder(
messageStringInternal,
labelToSearch,
PLACEHOLDER
)
messageStringInternal = DisplayUtils.searchAndReplaceWithMentionSpan(
key!!,
@ -169,5 +182,6 @@ class MessageUtils(val context: Context) {
private const val TAG = "MessageUtils"
const val MAX_REPLY_LENGTH = 250
const val HTTPS_PROTOCOL = "https://"
const val PLACEHOLDER = "[mention]"
}
}