Separates the new GaleryParser from Composables so that later we can cache it and parse it on the Default thread.

This commit is contained in:
Vitor Pamplona
2025-10-20 17:11:34 -04:00
parent c4653b147c
commit 3a73de6828
10 changed files with 556 additions and 707 deletions
@@ -0,0 +1,148 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.richtext
import android.R.attr.text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.Color
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.nip01Core.core.EmptyTagList
import kotlinx.collections.immutable.toImmutableList
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class GalleryParserTest {
@Test
fun testMixedImageAndVideoRenderedIndividually() {
// Test the bug: when mixed image + video, both should be rendered individually
val text =
"Renfield (2023)\n" +
"https://image.tmdb.org/t/p/original/ekfIcBvqfqKbI6m227NFipBNh7O.jpg\n" +
"https://archive.org/download/cinema-horror-sci-fi/Renfield.2023.ia.mp4"
val state = RichTextParser().parseText(text, EmptyTagList, null)
// Should have 2 image segments (image + video URLs)
Assert.assertEquals(2, state.paragraphs.size)
Assert.assertTrue(state.paragraphs[0] !is ImageGalleryParagraph)
Assert.assertEquals(1, state.paragraphs[0].words.size)
Assert.assertTrue(state.paragraphs[1] is ImageGalleryParagraph)
Assert.assertEquals("https://image.tmdb.org/t/p/original/ekfIcBvqfqKbI6m227NFipBNh7O.jpg", state.paragraphs[1].words[0].segmentText)
Assert.assertEquals("https://archive.org/download/cinema-horror-sci-fi/Renfield.2023.ia.mp4", state.paragraphs[1].words[1].segmentText)
}
@Test
fun testMixedImageAndVideoRenderedIndividuallyDoubled() {
// Test the bug: when mixed image + video, both should be rendered individually
val text =
"Renfield (2023)\n" +
"https://image.tmdb.org/t/p/original/ekfIcBvqfqKbI6m227NFipBNh7O.jpg\n" +
"https://archive.org/download/cinema-horror-sci-fi/Renfield.2023.ia.mp4\n" +
"Renfield (2023)\n" +
"https://image.tmdb.org/t/p/original/ekfIcBvqfqKbI6m227NFipBNh7O.jpg\n" +
"https://archive.org/download/cinema-horror-sci-fi/Renfield.2023.ia.mp4"
val state = RichTextParser().parseText(text, EmptyTagList, null)
// Should have 2 image segments (image + video URLs)
Assert.assertEquals(4, state.paragraphs.size)
Assert.assertTrue(state.paragraphs[0] !is ImageGalleryParagraph)
Assert.assertEquals(1, state.paragraphs[0].words.size)
Assert.assertTrue(state.paragraphs[1] is ImageGalleryParagraph)
Assert.assertEquals("https://image.tmdb.org/t/p/original/ekfIcBvqfqKbI6m227NFipBNh7O.jpg", state.paragraphs[1].words[0].segmentText)
Assert.assertEquals("https://archive.org/download/cinema-horror-sci-fi/Renfield.2023.ia.mp4", state.paragraphs[1].words[1].segmentText)
Assert.assertTrue(state.paragraphs[2] !is ImageGalleryParagraph)
Assert.assertEquals(1, state.paragraphs[2].words.size)
Assert.assertTrue(state.paragraphs[3] is ImageGalleryParagraph)
Assert.assertEquals("https://image.tmdb.org/t/p/original/ekfIcBvqfqKbI6m227NFipBNh7O.jpg", state.paragraphs[3].words[0].segmentText)
Assert.assertEquals("https://archive.org/download/cinema-horror-sci-fi/Renfield.2023.ia.mp4", state.paragraphs[3].words[1].segmentText)
}
@Test
fun testMultipleImagesRenderedAsGallery() {
// Test that multiple images (no videos) are correctly grouped as gallery
val text =
"Gallery:\n" +
"https://example.com/image1.jpg\n" +
"https://example.com/image2.png"
val state = RichTextParser().parseText(text, EmptyTagList, null)
// Should have 2 image segments (image + video URLs)
Assert.assertEquals(2, state.paragraphs.size)
// Should render as gallery (1 gallery with 2 images)
Assert.assertTrue("Should render 1 gallery", state.paragraphs[1] is ImageGalleryParagraph)
Assert.assertEquals("Gallery should contain 2 images", 2, state.paragraphs[1].words.size)
Assert.assertEquals("https://example.com/image1.jpg", state.paragraphs[1].words[0].segmentText)
Assert.assertEquals("https://example.com/image2.png", state.paragraphs[1].words[1].segmentText)
}
@Test
fun testMultipleImagesInAParagraphRenderedAsGallery() {
// Test that multiple images (no videos) are correctly grouped as gallery
val text =
"Gallery:\n" +
"https://example.com/image1.jpg https://example.com/image2.png"
val state = RichTextParser().parseText(text, EmptyTagList, null)
// Should have 2 image segments (image + video URLs)
Assert.assertEquals(2, state.paragraphs.size)
// Should render as gallery (1 gallery with 2 images)
Assert.assertTrue("Should render 1 gallery", state.paragraphs[1] is ImageGalleryParagraph)
Assert.assertEquals("Gallery should contain 2 images", 2, state.paragraphs[1].words.size)
Assert.assertEquals("https://example.com/image1.jpg", state.paragraphs[1].words[0].segmentText)
Assert.assertEquals("https://example.com/image2.png", state.paragraphs[1].words[1].segmentText)
}
@Test
fun testSingleImageRenderedIndividually() {
// Test that a single image is rendered individually, not as gallery
val text = "Single image:\nhttps://example.com/image.jpg"
val state = RichTextParser().parseText(text, EmptyTagList, null)
// Should render individually (not as gallery)
Assert.assertTrue("Should render 1 individual image", state.paragraphs[1] !is ImageGalleryParagraph)
Assert.assertTrue("Should not render as gallery", state.paragraphs[0] !is ImageGalleryParagraph)
Assert.assertEquals("https://example.com/image.jpg", state.paragraphs[1].words[0].segmentText)
}
@Test
fun testGigiMessage() {
val text = "I played The Typing of the Dead a lot when I was younger, and now I just found out that there's a new take on it: The Last Sentence. Tempted! https://relay.dergigi.com/d6a3e33b101fe219ef251ac6261c10392c2af9918c3c252d4c202016b0b4ec83.jpg https://relay.dergigi.com/d60c9c562912573f214c2b1958cc20bf8913cd718d2ed9e020621e3e3120634b.jpg"
val state = RichTextParser().parseText(text, EmptyTagList, null)
// Should render individually (not as gallery)
Assert.assertEquals("I played The Typing of the Dead a lot when I was younger, and now I just found out that there's a new take on it: The Last Sentence. Tempted!", state.paragraphs[0].words[0].segmentText)
Assert.assertTrue("Should render as gallery", state.paragraphs[1] is ImageGalleryParagraph)
Assert.assertEquals("https://relay.dergigi.com/d6a3e33b101fe219ef251ac6261c10392c2af9918c3c252d4c202016b0b4ec83.jpg", state.paragraphs[1].words[0].segmentText)
Assert.assertEquals("https://relay.dergigi.com/d60c9c562912573f214c2b1958cc20bf8913cd718d2ed9e020621e3e3120634b.jpg", state.paragraphs[1].words[1].segmentText)
}
}
@@ -4042,7 +4042,7 @@ class RichTextParserTest {
org.junit.Assert.assertTrue(state.imageList.isEmpty())
org.junit.Assert.assertTrue(state.customEmoji.isEmpty())
org.junit.Assert.assertEquals(
"Hi, how are you doing? ",
"Hi, how are you doing?",
state.paragraphs
.firstOrNull()
?.words
@@ -4060,7 +4060,7 @@ class RichTextParserTest {
org.junit.Assert.assertTrue(state.imageList.isEmpty())
org.junit.Assert.assertTrue(state.customEmoji.isEmpty())
org.junit.Assert.assertEquals(
"\nHi, \nhow\n\n\n are you doing? \n",
"\nHi,\nhow\n\n\n are you doing?\n",
state.paragraphs.joinToString("\n") { it.words.joinToString(" ") { it.segmentText } },
)
}
@@ -4231,7 +4231,6 @@ class RichTextParserTest {
listOf<String>(
"RegularText(Goon Night everybody :sleep:)",
"Image(81ca16-b665-4f57-80cb-11a58461fb61.avif)",
"RegularText()",
"Image(https://bae.st/media/66b08dde784287ed8f92c455bc62076a04671ccb44097550626a532185a5d3ed.avif?name=81ca16-b665-4f57-80cb-11a58461fb61.avif)",
)
@@ -0,0 +1,190 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.richtext
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlin.collections.ArrayList
data class ParagraphImageAnalysis(
val imageCount: Int,
val isImageOnly: Boolean,
val hasMultipleImages: Boolean,
)
class GalleryParser {
fun analyzeParagraphImages(paragraph: ParagraphState): ParagraphImageAnalysis {
var imageCount = 0
var hasNonWhitespaceNonImageContent = false
paragraph.words.forEach { word ->
when (word) {
is ImageSegment, is Base64Segment -> imageCount++
is RegularTextSegment -> {
if (word.segmentText.isNotBlank()) {
hasNonWhitespaceNonImageContent = true
}
}
else -> hasNonWhitespaceNonImageContent = true // Links, emojis, etc.
}
}
val isImageOnly = imageCount > 0 && !hasNonWhitespaceNonImageContent
val hasMultipleImages = imageCount > 1
return ParagraphImageAnalysis(
imageCount = imageCount,
isImageOnly = isImageOnly,
hasMultipleImages = hasMultipleImages,
)
}
fun collectConsecutiveImageParagraphs(
paragraphs: List<ParagraphState>,
startIndex: Int,
): Pair<List<ParagraphState>, Int> {
val imageParagraphs = mutableListOf<ParagraphState>()
var j = startIndex
while (j < paragraphs.size) {
val currentParagraph = paragraphs[j]
val words = currentParagraph.words
// Fast path for empty check
if (words.isEmpty()) {
j++
continue
}
// Check for single whitespace word
if (words.size == 1) {
val firstWord = words.first()
if (firstWord is RegularTextSegment && firstWord.segmentText.isBlank()) {
j++
continue
}
}
// Check if it's an image-only paragraph using unified analysis
val analysis = analyzeParagraphImages(currentParagraph)
if (analysis.isImageOnly) {
imageParagraphs.add(currentParagraph)
j++
} else {
break
}
}
return imageParagraphs to j
}
@OptIn(ExperimentalLayoutApi::class)
fun processParagraphs(paragraphs: List<ParagraphState>): List<ParagraphState> {
val result = mutableListOf<ParagraphState>()
var paragraphIndex = 0
while (paragraphIndex < paragraphs.size) {
val paragraph = paragraphs[paragraphIndex]
if (paragraph.words.isEmpty()) {
// Empty paragraph - render normally with FlowRow (will render nothing)
result.add(paragraph)
paragraphIndex++
} else {
val analysis = analyzeParagraphImages(paragraph)
if (analysis.isImageOnly) {
// Collect consecutive image-only paragraphs for gallery
val (imageParagraphs, endIndex) = collectConsecutiveImageParagraphs(paragraphs, paragraphIndex)
val allImageWords = imageParagraphs.flatMap { it.words }.toImmutableList()
if (allImageWords.size > 1) {
result.add(ImageGalleryParagraph(allImageWords, paragraph.isRTL))
} else {
// Single image - render with FlowRow wrapper
result.add(paragraph)
}
paragraphIndex = endIndex // Return next index to process
} else if (analysis.hasMultipleImages) {
// Mixed paragraph with multiple images - break it down into many paragraphs
result.addAll(processWordsWithImageGrouping(paragraph))
paragraphIndex++
} else {
result.add(paragraph)
paragraphIndex++
}
}
}
return result
}
fun processWordsWithImageGrouping(paragraph: ParagraphState): List<ParagraphState> {
val resultingParagraphs = mutableListOf<ParagraphState>()
var i = 0
val n = paragraph.words.size
var currentParagraphSegments = mutableListOf<Segment>()
while (i < n) {
val word = paragraph.words[i]
if (word is ImageSegment || word is Base64Segment) {
// Collect consecutive image/whitespace segments without extra list allocations
val imageSegments = mutableListOf<Segment>()
var j = i
while (j < n) {
val seg = paragraph.words[j]
when {
seg is ImageSegment || seg is Base64Segment -> imageSegments.add(seg)
seg is RegularTextSegment && seg.segmentText.isBlank() -> { /* skip whitespace */ }
else -> break
}
j++
}
if (imageSegments.size <= 1) {
currentParagraphSegments.add(word)
} else {
if (currentParagraphSegments.isNotEmpty()) {
resultingParagraphs.add(ParagraphState(currentParagraphSegments.toImmutableList(), paragraph.isRTL))
currentParagraphSegments = mutableListOf<Segment>()
}
resultingParagraphs.add(ImageGalleryParagraph(imageSegments.toImmutableList(), paragraph.isRTL))
}
i = j // jump past processed run
} else {
currentParagraphSegments.add(word)
i++
}
}
if (currentParagraphSegments.isNotEmpty()) {
resultingParagraphs.add(ParagraphState(currentParagraphSegments.toImmutableList(), paragraph.isRTL))
}
return resultingParagraphs
}
}
@@ -172,30 +172,30 @@ class RichTextParser {
val paragraphSegments = ArrayList<ParagraphState>(lines.size)
lines.forEach { paragraph ->
var isDirty = false
val isRTL = isArabic(paragraph)
val wordList = paragraph.trimEnd().split(' ')
val segments = ArrayList<Segment>(wordList.size)
wordList.forEach { word ->
val wordSegment = wordIdentifier(word, images, urls, emojis, tags)
if (wordSegment !is RegularTextSegment) {
isDirty = true
}
segments.add(wordSegment)
segments.add(wordIdentifier(word, images, urls, emojis, tags))
}
val newSegments =
if (isDirty) {
ParagraphState(segments.toPersistentList(), isRTL)
} else {
ParagraphState(persistentListOf<Segment>(RegularTextSegment(paragraph)), isRTL)
}
paragraphSegments.add(newSegments)
paragraphSegments.add(ParagraphState(segments.toPersistentList(), isRTL))
}
return paragraphSegments.toImmutableList()
val segmentsWithGalleries = GalleryParser().processParagraphs(paragraphSegments)
return segmentsWithGalleries
.map { paragraph ->
if (paragraph.words.isEmpty() || paragraph.words.any { it !is RegularTextSegment }) {
paragraph
} else {
ParagraphState(
persistentListOf<Segment>(RegularTextSegment(paragraph.words.joinToString(" ") { it.segmentText })),
paragraph.isRTL,
)
}
}.toImmutableList()
}
private fun isNumber(word: String) = numberPattern.matcher(word).matches()
@@ -37,11 +37,17 @@ class RichTextViewerState(
)
@Immutable
data class ParagraphState(
open class ParagraphState(
val words: ImmutableList<Segment>,
val isRTL: Boolean,
)
@Immutable
class ImageGalleryParagraph(
words: ImmutableList<Segment>,
isRTL: Boolean,
) : ParagraphState(words, isRTL)
@Immutable
open class Segment(
val segmentText: String,