diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt index ddc151c76..051289452 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt @@ -404,10 +404,9 @@ private fun RenderSingleParagraphWithFlowRow( FlowRow( horizontalArrangement = Arrangement.spacedBy(spaceWidth), ) { - RenderWordsWithImageGallery( - words, - context, - ) + words.forEach { word -> + RenderWordWithPreview(word, context) + } } } } @@ -797,7 +796,7 @@ fun CoreSecretMessage( ) if (localSecretContent.paragraphs.size == 1) { - RenderWordsWithImageGallery( + RenderSecretParagraphOptimized( localSecretContent.paragraphs[0].words.toImmutableList(), context, ) @@ -806,14 +805,44 @@ fun CoreSecretMessage( Column(CashuCardBorders) { localSecretContent.paragraphs.forEach { paragraph -> - FlowRow( - modifier = Modifier.align(if (paragraph.isRTL) Alignment.End else Alignment.Start), - horizontalArrangement = Arrangement.spacedBy(spaceWidth), - ) { - RenderWordsWithImageGallery( - paragraph.words.toImmutableList(), - context, - ) + RenderSecretParagraphOptimized( + paragraph.words.toImmutableList(), + context, + spaceWidth, + paragraph.isRTL, + ) + } + } + } +} + +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun RenderSecretParagraphOptimized( + words: ImmutableList, + context: RenderContext, + spaceWidth: Dp? = null, + isRTL: Boolean = false, +) { + // Check if we need single-image optimization + val imageSegments = words.filter { it is ImageSegment || it is Base64Segment } + + if (imageSegments.size > 1) { + // Multiple images - use gallery logic + RenderWordsWithImageGallery(words, context) + } else { + // Single or no images - render directly with FlowRow for optimal performance + val actualSpaceWidth = spaceWidth ?: measureSpaceWidth(LocalTextStyle.current) + + CompositionLocalProvider( + LocalLayoutDirection provides if (isRTL) LayoutDirection.Rtl else LayoutDirection.Ltr, + LocalTextStyle provides LocalTextStyle.current, + ) { + FlowRow( + horizontalArrangement = Arrangement.spacedBy(actualSpaceWidth), + ) { + words.forEach { word -> + RenderWordWithPreview(word, context) } } }