From 12df2422e8ee3f94a6ced01650709d53018062db Mon Sep 17 00:00:00 2001 From: Mehmedalija Karisik Date: Mon, 16 Dec 2024 02:15:07 +0100 Subject: [PATCH 1/2] Fix repeating hashtag not highlighting in note content --- .../primal/android/core/utils/TextMatcher.kt | 20 ++++++++++++------- .../android/notes/feed/note/ui/NoteContent.kt | 6 +++++- .../android/core/utils/TextMatcherTest.kt | 16 +++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt b/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt index e94a9d91..1fdfcedd 100644 --- a/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt +++ b/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt @@ -3,6 +3,7 @@ package net.primal.android.core.utils class TextMatcher( content: String, texts: List, + repeatingOccurrences: Boolean = false, ) { private val matches: MutableList = mutableListOf() @@ -26,16 +27,21 @@ class TextMatcher( } init { - texts.forEach { - val startIndex = content.indexOfNotMatchedBefore(text = it) - if (startIndex != null) { + texts.forEach { text -> + var currentIndex = content.indexOfNotMatchedBefore(text = text) + + while (currentIndex != null) { matches.add( TextMatch( - value = it, - startIndex = startIndex, - endIndex = startIndex + it.length, - ), + value = text, + startIndex = currentIndex, + endIndex = currentIndex + text.length, + ) ) + + if (!repeatingOccurrences) break + + currentIndex = content.indexOfNotMatchedBefore(text = text) } } } diff --git a/app/src/main/kotlin/net/primal/android/notes/feed/note/ui/NoteContent.kt b/app/src/main/kotlin/net/primal/android/notes/feed/note/ui/NoteContent.kt index 56fcc5ef..cf5a0d62 100644 --- a/app/src/main/kotlin/net/primal/android/notes/feed/note/ui/NoteContent.kt +++ b/app/src/main/kotlin/net/primal/android/notes/feed/note/ui/NoteContent.kt @@ -393,7 +393,11 @@ fun renderContentAsAnnotatedString( ) } - TextMatcher(content = refinedContent, texts = data.hashtags) + TextMatcher( + content = refinedContent, + texts = data.hashtags, + repeatingOccurrences = true, + ) .matches() .forEach { addHashtagAnnotation( diff --git a/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt b/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt index 31fe5be5..99fcabd9 100644 --- a/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt +++ b/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt @@ -2,7 +2,9 @@ package net.primal.android.core.utils import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.collections.shouldContainAll +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import io.kotest.matchers.collections.shouldNotContain +import io.kotest.matchers.shouldBe import org.junit.Test class TextMatcherTest { @@ -31,6 +33,20 @@ class TextMatcherTest { actual.shouldContainAll(hashtags) } + @Test + fun `matches should match hashtags and return hashtags with duplicates`() { + val hashtags = listOf("#nostr", "#bitcoin", "#sats", "#freedom") + val expected = listOf("#nostr", "#nostr", "#nostr", "#bitcoin", "#freedom", "#freedom", "#sats") + val matcher = TextMatcher( + content = "Hello I love #nostr #nostr somemore #nostr #bitcoin #freedom #sats #freedom", + texts = hashtags, + repeatingOccurrences = true, + ) + + val actual = matcher.matches().map { it.value } + actual.shouldContainExactlyInAnyOrder(expected) + } + @Test fun `matches should not match unknown hashtags`() { val hashtags = listOf("#Hiking", "#Trails") From f33f51a7ce341976b5c3f9c4816595845cb15519 Mon Sep 17 00:00:00 2001 From: Mehmedalija Karisik Date: Mon, 16 Dec 2024 02:21:20 +0100 Subject: [PATCH 2/2] start klintFormat task --- .../main/kotlin/net/primal/android/core/utils/TextMatcher.kt | 2 +- .../kotlin/net/primal/android/core/utils/TextMatcherTest.kt | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt b/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt index 1fdfcedd..ca7d53c6 100644 --- a/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt +++ b/app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt @@ -36,7 +36,7 @@ class TextMatcher( value = text, startIndex = currentIndex, endIndex = currentIndex + text.length, - ) + ), ) if (!repeatingOccurrences) break diff --git a/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt b/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt index 99fcabd9..856f91ee 100644 --- a/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt +++ b/app/src/test/kotlin/net/primal/android/core/utils/TextMatcherTest.kt @@ -4,7 +4,6 @@ import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.collections.shouldContainAll import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import io.kotest.matchers.collections.shouldNotContain -import io.kotest.matchers.shouldBe import org.junit.Test class TextMatcherTest {