Remove highlight from previous match when navigating between search results

This commit is contained in:
Sylvain Berfini 2026-01-08 12:06:13 +01:00
parent 57644a34de
commit 3f868e02fe
2 changed files with 27 additions and 25 deletions

View file

@ -676,21 +676,6 @@ class MessageModel
return
}
// Check for search
if (highlight.isNotEmpty()) {
val indexStart = rawTextContent.indexOf(highlight, 0, ignoreCase = true)
if (indexStart >= 0) {
isTextHighlighted = true
val indexEnd = indexStart + highlight.length
spannableBuilder.setSpan(
StyleSpan(Typeface.BOLD),
indexStart,
indexEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
// Check for mentions
val chatRoom = chatMessage.chatRoom
val matcher = Pattern.compile(MENTION_REGEXP).matcher(rawTextContent)
@ -787,6 +772,21 @@ class MessageModel
)
.build(spannableBuilder)
)
// Check for search
if (highlight.isNotEmpty()) {
val indexStart = rawTextContent.indexOf(highlight, 0, ignoreCase = true)
if (indexStart >= 0) {
isTextHighlighted = true
val indexEnd = indexStart + highlight.length
spannableBuilder.setSpan(
StyleSpan(Typeface.BOLD),
indexStart,
indexEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
}
@WorkerThread

View file

@ -152,6 +152,8 @@ class ConversationViewModel
private var latestMatch: EventLog? = null
private var latestMatchModel: MessageModel? = null
private val chatRoomListener = object : ChatRoomListenerStub() {
@WorkerThread
override fun onConferenceJoined(chatRoom: ChatRoom, eventLog: EventLog) {
@ -381,20 +383,17 @@ class ConversationViewModel
@UiThread
fun closeSearchBar() {
coreContext.postOnCoreThread {
latestMatchModel?.highlightText("")
latestMatchModel = null
}
searchFilter.value = ""
searchBarVisible.value = false
focusSearchBarEvent.value = Event(false)
latestMatch = null
canSearchDown.value = false
canSearchUp.value = false
coreContext.postOnCoreThread {
for (eventLog in eventsList) {
if ((eventLog.model as? MessageModel)?.isTextHighlighted == true) {
eventLog.model.highlightText("")
}
}
}
}
@UiThread
@ -1031,11 +1030,13 @@ class ConversationViewModel
canSearchDown.postValue(true)
canSearchUp.postValue(true)
// Clear highlight from previous match
latestMatchModel?.highlightText("")
Log.i(
"$TAG Found result [${match.chatMessage?.messageId}] while looking up for message with text [$textToSearch] in direction [$direction] starting from message [${latestMatch?.chatMessage?.messageId}]"
)
latestMatch = match
val found = eventsList.find {
it.eventLog == match
}
@ -1044,7 +1045,8 @@ class ConversationViewModel
loadMessagesUpTo(match)
} else {
Log.i("$TAG Found result is already in history, no need to load more history")
(found.model as? MessageModel)?.highlightText(textToSearch)
latestMatchModel = (found.model as? MessageModel)
latestMatchModel?.highlightText(textToSearch)
val index = eventsList.indexOf(found)
itemToScrollTo.postValue(index)
searchInProgress.postValue(false)