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
@@ -1,217 +0,0 @@
/**
* 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
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.amethyst.commons.richtext.ImageSegment
import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
import com.vitorpamplona.amethyst.commons.richtext.Segment
import com.vitorpamplona.amethyst.ui.components.ParagraphParser
import com.vitorpamplona.amethyst.ui.components.RenderContext
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.quartz.nip01Core.core.EmptyTagList
import io.mockk.mockk
import kotlinx.collections.immutable.toImmutableList
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ParagraphParserTest {
@get:Rule
val composeTestRule = createComposeRule()
@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)
// Extract the image segments from the parsed paragraphs
val imageSegments = mutableListOf<ImageSegment>()
state.paragraphs.forEach { paragraph ->
paragraph.words.forEach { word ->
if (word is ImageSegment) {
imageSegments.add(word)
}
}
}
// Should have 2 image segments (image + video URLs)
assertEquals(2, imageSegments.size)
// Track what gets rendered
val singleWordRenders = mutableListOf<Segment>()
val galleryRenders = mutableListOf<List<MediaUrlImage>>()
// Set up the test with mocked dependencies
val context =
RenderContext(
state = state,
backgroundColor = mutableStateOf(Color.White),
quotesLeft = 3,
callbackUri = null,
accountViewModel = mockk<AccountViewModel>(relaxed = true),
nav = mockk<INav>(relaxed = true),
)
// Execute the actual ParagraphParser method
composeTestRule.setContent {
ParagraphParser().ProcessWordsWithImageGrouping(
words = imageSegments.toImmutableList(),
context = context,
renderSingleWord = { segment, _ ->
singleWordRenders.add(segment)
},
renderGallery = { images, _ ->
galleryRenders.add(images)
},
)
}
composeTestRule.waitForIdle()
assertTrue(
"Mixed image/video should be rendered individually (2 renders), not as gallery (0 renders). " +
"Found: $singleWordRenders individual, $galleryRenders gallery",
singleWordRenders.size == 2 && galleryRenders.isEmpty(),
)
}
@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)
// Extract the image segments
val imageSegments = mutableListOf<ImageSegment>()
state.paragraphs.forEach { paragraph ->
paragraph.words.forEach { word ->
if (word is ImageSegment) {
imageSegments.add(word)
}
}
}
assertEquals(2, imageSegments.size)
// Track renders
val singleWordRenders = mutableListOf<Segment>()
val galleryRenders = mutableListOf<List<MediaUrlImage>>()
val context =
RenderContext(
state = state,
backgroundColor = mutableStateOf(Color.White),
quotesLeft = 3,
callbackUri = null,
accountViewModel = mockk<AccountViewModel>(relaxed = true),
nav = mockk<INav>(relaxed = true),
)
composeTestRule.setContent {
ParagraphParser().ProcessWordsWithImageGrouping(
words = imageSegments.toImmutableList(),
context = context,
renderSingleWord = { segment, _ ->
singleWordRenders.add(segment)
},
renderGallery = { images, _ ->
galleryRenders.add(images)
},
)
}
composeTestRule.waitForIdle()
// Should render as gallery (1 gallery with 2 images)
assertEquals("Should render 1 gallery", 1, galleryRenders.size)
assertEquals("Gallery should contain 2 images", 2, galleryRenders[0].size)
assertEquals("Should not render individually", 0, singleWordRenders.size)
}
@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)
val imageSegments = mutableListOf<ImageSegment>()
state.paragraphs.forEach { paragraph ->
paragraph.words.forEach { word ->
if (word is ImageSegment) {
imageSegments.add(word)
}
}
}
assertEquals(1, imageSegments.size)
val singleWordRenders = mutableListOf<Segment>()
val galleryRenders = mutableListOf<List<MediaUrlImage>>()
val context =
RenderContext(
state = state,
backgroundColor = mutableStateOf(Color.White),
quotesLeft = 3,
callbackUri = null,
accountViewModel = mockk<AccountViewModel>(relaxed = true),
nav = mockk<INav>(relaxed = true),
)
composeTestRule.setContent {
ParagraphParser().ProcessWordsWithImageGrouping(
words = imageSegments.toImmutableList(),
context = context,
renderSingleWord = { segment, _ ->
singleWordRenders.add(segment)
},
renderGallery = { images, _ ->
galleryRenders.add(images)
},
)
}
composeTestRule.waitForIdle()
// Should render individually (not as gallery)
assertEquals("Should render 1 individual image", 1, singleWordRenders.size)
assertEquals("Should not render as gallery", 0, galleryRenders.size)
}
}
@@ -49,6 +49,7 @@ import com.vitorpamplona.amethyst.commons.richtext.HashTagSegment
import com.vitorpamplona.amethyst.commons.richtext.RegularTextSegment
import com.vitorpamplona.amethyst.ui.components.HashTag
import com.vitorpamplona.amethyst.ui.components.RenderRegular
import com.vitorpamplona.amethyst.ui.components.RenderTextParagraph
import com.vitorpamplona.amethyst.ui.navigation.navs.EmptyNav
import com.vitorpamplona.amethyst.ui.theme.ThemeComparisonColumn
import com.vitorpamplona.quartz.nip01Core.core.EmptyTagList
@@ -60,10 +61,12 @@ fun RenderHashTagIconsPreview() {
RenderRegular(
"Testing rendering of hashtags: #flowerstr #Bitcoin, #nostr, #lightning, #zap, #amethyst, #cashu, #plebs, #coffee, #skullofsatoshi, #grownostr, #footstr, #tunestr, #weed, #mate, #gamestr, #gamechain",
EmptyTagList,
) { word, state ->
when (word) {
is HashTagSegment -> HashTag(word, EmptyNav)
is RegularTextSegment -> Text(word.segmentText)
) { paragraph, state, spaceWidth, modifier ->
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
when (word) {
is HashTagSegment -> HashTag(word, EmptyNav)
is RegularTextSegment -> Text(word.segmentText)
}
}
}
}
@@ -33,11 +33,16 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Dp
import com.vitorpamplona.amethyst.commons.richtext.ImageGalleryParagraph
import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage
import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.Size10dp
import com.vitorpamplona.amethyst.ui.theme.Size5dp
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableSet
private const val ASPECT_RATIO = 4f / 3f
private val IMAGE_SPACING: Dp = Size5dp
@@ -64,20 +69,28 @@ private fun GalleryImage(
@Composable
fun ImageGallery(
images: ImmutableList<MediaUrlImage>,
images: ImageGalleryParagraph,
state: RichTextViewerState,
accountViewModel: AccountViewModel,
modifier: Modifier = Modifier,
roundedCorner: Boolean = true,
) {
if (images.isEmpty()) return
if (images.words.isEmpty()) return
val resolvedImages =
images.words
.mapNotNull { segment ->
val imageUrl = segment.segmentText
state.imagesForPager[imageUrl] as? MediaUrlImage
}.toImmutableList()
Column(modifier = modifier.padding(vertical = Size10dp)) {
when (images.size) {
1 -> SingleImageGallery(images, accountViewModel, roundedCorner)
2 -> TwoImageGallery(images, accountViewModel, roundedCorner)
3 -> ThreeImageGallery(images, accountViewModel, roundedCorner)
4 -> FourImageGallery(images, accountViewModel, roundedCorner)
else -> ManyImageGallery(images, accountViewModel, roundedCorner)
when (resolvedImages.size) {
1 -> SingleImageGallery(resolvedImages, accountViewModel, roundedCorner)
2 -> TwoImageGallery(resolvedImages, accountViewModel, roundedCorner)
3 -> ThreeImageGallery(resolvedImages, accountViewModel, roundedCorner)
4 -> FourImageGallery(resolvedImages, accountViewModel, roundedCorner)
else -> ManyImageGallery(resolvedImages, accountViewModel, roundedCorner)
}
}
}
@@ -1,279 +0,0 @@
/**
* 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.ui.components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.material3.LocalTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.MutableState
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import com.vitorpamplona.amethyst.commons.richtext.Base64Segment
import com.vitorpamplona.amethyst.commons.richtext.ImageSegment
import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage
import com.vitorpamplona.amethyst.commons.richtext.ParagraphState
import com.vitorpamplona.amethyst.commons.richtext.RegularTextSegment
import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
import com.vitorpamplona.amethyst.commons.richtext.Segment
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
data class RenderContext(
val state: RichTextViewerState,
val backgroundColor: MutableState<Color>,
val quotesLeft: Int,
val callbackUri: String?,
val accountViewModel: AccountViewModel,
val nav: INav,
)
data class ParagraphImageAnalysis(
val imageCount: Int,
val isImageOnly: Boolean,
val hasMultipleImages: Boolean,
)
class ParagraphParser {
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: ImmutableList<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)
@Composable
fun processParagraph(
paragraphs: ImmutableList<ParagraphState>,
paragraphIndex: Int,
spaceWidth: Dp,
context: RenderContext,
renderSingleParagraph: @Composable (ParagraphState, ImmutableList<Segment>, Dp, RenderContext) -> Unit,
renderImageGallery: @Composable (ImmutableList<Segment>, RenderContext) -> Unit,
): Int {
val paragraph = paragraphs[paragraphIndex]
if (paragraph.words.isEmpty()) {
// Empty paragraph - render normally with FlowRow (will render nothing)
renderSingleParagraph(paragraph, paragraph.words.toImmutableList(), spaceWidth, context)
return paragraphIndex + 1
}
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) {
// Multiple images - render as gallery (no FlowRow wrapper needed)
renderImageGallery(allImageWords, context)
} else {
// Single image - render with FlowRow wrapper
renderSingleParagraph(paragraph, paragraph.words.toImmutableList(), spaceWidth, context)
}
return endIndex // Return next index to process
} else if (analysis.hasMultipleImages) {
// Mixed paragraph with multiple images - use renderImageGallery for smart grouping
renderImageGallery(paragraph.words.toImmutableList(), context)
return paragraphIndex + 1
} else {
// Regular paragraph (no images or single image) - render normally with FlowRow
renderSingleParagraph(paragraph, paragraph.words.toImmutableList(), spaceWidth, context)
return paragraphIndex + 1
}
}
@Composable
fun ProcessWordsWithImageGrouping(
words: ImmutableList<Segment>,
context: RenderContext,
renderSingleWord: @Composable (Segment, RenderContext) -> Unit,
renderGallery: @Composable (ImmutableList<MediaUrlImage>, AccountViewModel) -> Unit,
) {
var i = 0
val n = words.size
while (i < n) {
val word = 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 = 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) {
renderSingleWord(imageSegments.firstOrNull() ?: word, context)
} else {
val resolvedImages =
imageSegments.mapNotNull { segment ->
val imageUrl = segment.segmentText
context.state.imagesForPager[imageUrl] as? MediaUrlImage
}
// Render gallery only if all segments are images
if (resolvedImages.size == imageSegments.size) {
renderGallery(
resolvedImages.toImmutableList(),
context.accountViewModel,
)
} else {
imageSegments.forEach { segment ->
renderSingleWord(segment, context)
}
}
}
i = j // jump past processed run
} else {
renderSingleWord(word, context)
i++
}
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun RenderSingleParagraphWithFlowRow(
paragraph: ParagraphState,
words: ImmutableList<Segment>,
spaceWidth: Dp,
context: RenderContext,
renderWord: @Composable (Segment, RenderContext) -> Unit,
) {
CompositionLocalProvider(
LocalLayoutDirection provides
if (paragraph.isRTL) {
LayoutDirection.Rtl
} else {
LayoutDirection.Ltr
},
LocalTextStyle provides LocalTextStyle.current,
) {
FlowRow(
horizontalArrangement = Arrangement.spacedBy(spaceWidth),
) {
words.forEach { word ->
renderWord(word, context)
}
}
}
}
@Composable
fun ProcessAllParagraphs(
paragraphs: ImmutableList<ParagraphState>,
spaceWidth: Dp,
context: RenderContext,
renderSingleParagraph: @Composable (ParagraphState, ImmutableList<Segment>, Dp, RenderContext) -> Unit,
renderImageGallery: @Composable (ImmutableList<Segment>, RenderContext) -> Unit,
) {
var i = 0
while (i < paragraphs.size) {
i =
processParagraph(
paragraphs = paragraphs,
paragraphIndex = i,
spaceWidth = spaceWidth,
context = context,
renderSingleParagraph = renderSingleParagraph,
renderImageGallery = renderImageGallery,
)
}
}
}
@@ -67,12 +67,15 @@ import com.vitorpamplona.amethyst.commons.richtext.BechSegment
import com.vitorpamplona.amethyst.commons.richtext.CashuSegment
import com.vitorpamplona.amethyst.commons.richtext.EmailSegment
import com.vitorpamplona.amethyst.commons.richtext.EmojiSegment
import com.vitorpamplona.amethyst.commons.richtext.GalleryParser
import com.vitorpamplona.amethyst.commons.richtext.HashIndexEventSegment
import com.vitorpamplona.amethyst.commons.richtext.HashIndexUserSegment
import com.vitorpamplona.amethyst.commons.richtext.HashTagSegment
import com.vitorpamplona.amethyst.commons.richtext.ImageGalleryParagraph
import com.vitorpamplona.amethyst.commons.richtext.ImageSegment
import com.vitorpamplona.amethyst.commons.richtext.InvoiceSegment
import com.vitorpamplona.amethyst.commons.richtext.LinkSegment
import com.vitorpamplona.amethyst.commons.richtext.ParagraphState
import com.vitorpamplona.amethyst.commons.richtext.PhoneSegment
import com.vitorpamplona.amethyst.commons.richtext.RegularTextSegment
import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
@@ -90,6 +93,7 @@ import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUse
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
import com.vitorpamplona.amethyst.ui.components.markdown.RenderContentAsMarkdown
import com.vitorpamplona.amethyst.ui.navigation.navs.EmptyNav
import com.vitorpamplona.amethyst.ui.navigation.navs.EmptyNav.nav
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.navigation.routes.Route
import com.vitorpamplona.amethyst.ui.navigation.routes.routeFor
@@ -107,10 +111,10 @@ import com.vitorpamplona.amethyst.ui.theme.innerPostModifier
import com.vitorpamplona.quartz.nip01Core.core.EmptyTagList
import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.text.Typography.paragraph
fun isMarkdown(content: String): Boolean =
content.startsWith("> ") ||
@@ -149,15 +153,18 @@ fun RenderStrangeNamePreview() {
RenderRegular(
"If you want to stream or download the music from nostr:npub1sctag667a7np6p6ety2up94pnwwxhd2ep8n8afr2gtr47cwd4ewsvdmmjm can you here",
EmptyTagList,
) { word, state ->
when (word) {
is BechSegment -> {
Text(
"FreeFrom Official \uD80C\uDD66",
modifier = Modifier.border(1.dp, Color.Red),
)
) { paragraph, state, spaceWidth, modifier ->
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
when (word) {
is BechSegment -> {
Text(
"FreeFrom Official \uD80C\uDD66",
modifier = Modifier.border(1.dp, Color.Red),
)
}
is RegularTextSegment -> Text(word.segmentText)
}
is RegularTextSegment -> Text(word.segmentText)
}
}
}
@@ -166,14 +173,51 @@ fun RenderStrangeNamePreview() {
@Preview
@Composable
fun RenderRegularPreview() {
val nav = EmptyNav
Column(modifier = Modifier.padding(10.dp)) {
RenderRegular(
"nostr:npub1e0z776cpe0gllgktjk54fuzv8pdfxmq6smsmh8xd7t8s7n474n9smk0txy but i'm Monthly funding" +
" 7 other humans vitor@vitorpamplona.com at the moment so spread #test a bit thin, but won't always be the case.",
EmptyTagList,
) { word, state ->
) { paragraph, state, spaceWidth, modifier ->
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
when (word) {
// is ImageSegment -> ZoomableContentView(word.segmentText, state, accountViewModel)
// is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, accountViewModel)
is EmojiSegment -> RenderCustomEmoji(word.segmentText, state)
// is InvoiceSegment -> MayBeInvoicePreview(word.segmentText)
// is WithdrawSegment -> MayBeWithdrawal(word.segmentText)
// is CashuSegment -> CashuPreview(word.segmentText, accountViewModel)
is EmailSegment -> ClickableEmail(word.segmentText)
is PhoneSegment -> ClickablePhone(word.segmentText)
is BechSegment -> {
CreateClickableText(
word.segmentText.substring(0, 10),
"",
1,
route = Route.EventRedirect(word.segmentText),
nav = EmptyNav,
)
}
is HashTagSegment -> HashTag(word, EmptyNav)
// is HashIndexUserSegment -> TagLink(word, accountViewModel, nav)
// is HashIndexEventSegment -> TagLink(word, true, backgroundColorState, accountViewModel, nav)
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
is RegularTextSegment -> Text(word.segmentText)
}
}
}
}
}
@Preview
@Composable
fun RenderRegularPreview2() {
RenderRegular(
"#Amethyst v0.84.1: ncryptsec support (NIP-49)",
EmptyTagList,
) { paragraph, state, spaceWidth, modifier ->
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
when (word) {
// is ImageSegment -> ZoomableContentView(word.segmentText, state, accountViewModel)
// is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, accountViewModel)
@@ -183,17 +227,8 @@ fun RenderRegularPreview() {
// is CashuSegment -> CashuPreview(word.segmentText, accountViewModel)
is EmailSegment -> ClickableEmail(word.segmentText)
is PhoneSegment -> ClickablePhone(word.segmentText)
is BechSegment -> {
CreateClickableText(
word.segmentText.substring(0, 10),
"",
1,
route = Route.EventRedirect(word.segmentText),
nav = nav,
)
}
is HashTagSegment -> HashTag(word, nav)
// is BechSegment -> BechLink(word.segmentText, true, backgroundColor, accountViewModel, nav)
is HashTagSegment -> HashTag(word, EmptyNav)
// is HashIndexUserSegment -> TagLink(word, accountViewModel, nav)
// is HashIndexEventSegment -> TagLink(word, true, backgroundColorState, accountViewModel, nav)
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
@@ -203,34 +238,6 @@ fun RenderRegularPreview() {
}
}
@Preview
@Composable
fun RenderRegularPreview2() {
val nav = EmptyNav
RenderRegular(
"#Amethyst v0.84.1: ncryptsec support (NIP-49)",
EmptyTagList,
) { word, state ->
when (word) {
// is ImageSegment -> ZoomableContentView(word.segmentText, state, accountViewModel)
// is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, accountViewModel)
is EmojiSegment -> RenderCustomEmoji(word.segmentText, state)
// is InvoiceSegment -> MayBeInvoicePreview(word.segmentText)
// is WithdrawSegment -> MayBeWithdrawal(word.segmentText)
// is CashuSegment -> CashuPreview(word.segmentText, accountViewModel)
is EmailSegment -> ClickableEmail(word.segmentText)
is PhoneSegment -> ClickablePhone(word.segmentText)
// is BechSegment -> BechLink(word.segmentText, true, backgroundColor, accountViewModel, nav)
is HashTagSegment -> HashTag(word, nav)
// is HashIndexUserSegment -> TagLink(word, accountViewModel, nav)
// is HashIndexEventSegment -> TagLink(word, true, backgroundColorState, accountViewModel, nav)
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
is RegularTextSegment -> Text(word.segmentText)
}
}
}
@Preview
@Composable
fun RenderRegularPreview3() {
@@ -244,7 +251,6 @@ fun RenderRegularPreview3() {
arrayOf("proxy", "https://misskey.io/notes/9q0x6gtdysir03qh", "activitypub"),
),
)
val nav = EmptyNav
val accountViewModel = mockAccountViewModel()
RenderRegular(
@@ -252,22 +258,24 @@ fun RenderRegularPreview3() {
"#ioメシヨソイゲーム\n" +
"https://misskey.io/play/9g3qza4jow",
tags,
) { word, state ->
when (word) {
// is ImageSegment -> ZoomableContentView(word.segmentText, state, accountViewModel)
is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, null, accountViewModel)
is EmojiSegment -> RenderCustomEmoji(word.segmentText, state)
// is InvoiceSegment -> MayBeInvoicePreview(word.segmentText)
// is WithdrawSegment -> MayBeWithdrawal(word.segmentText)
// is CashuSegment -> CashuPreview(word.segmentText, accountViewModel)
is EmailSegment -> ClickableEmail(word.segmentText)
is PhoneSegment -> ClickablePhone(word.segmentText)
// is BechSegment -> BechLink(word.segmentText, true, backgroundColor, accountViewModel, nav)
is HashTagSegment -> HashTag(word, nav)
// is HashIndexUserSegment -> TagLink(word, accountViewModel, nav)
// is HashIndexEventSegment -> TagLink(word, true, backgroundColorState, accountViewModel, nav)
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
is RegularTextSegment -> Text(word.segmentText)
) { paragraph, state, spaceWidth, modifier ->
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
when (word) {
// is ImageSegment -> ZoomableContentView(word.segmentText, state, accountViewModel)
is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, null, accountViewModel)
is EmojiSegment -> RenderCustomEmoji(word.segmentText, state)
// is InvoiceSegment -> MayBeInvoicePreview(word.segmentText)
// is WithdrawSegment -> MayBeWithdrawal(word.segmentText)
// is CashuSegment -> CashuPreview(word.segmentText, accountViewModel)
is EmailSegment -> ClickableEmail(word.segmentText)
is PhoneSegment -> ClickablePhone(word.segmentText)
// is BechSegment -> BechLink(word.segmentText, true, backgroundColor, accountViewModel, nav)
is HashTagSegment -> HashTag(word, EmptyNav)
// is HashIndexUserSegment -> TagLink(word, accountViewModel, nav)
// is HashIndexEventSegment -> TagLink(word, true, backgroundColorState, accountViewModel, nav)
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
is RegularTextSegment -> Text(word.segmentText)
}
}
}
}
@@ -284,62 +292,58 @@ private fun RenderRegular(
nav: INav,
) {
if (canPreview) {
RenderRegularWithGallery(content, tags, backgroundColor, quotesLeft, callbackUri, accountViewModel, nav)
RenderRegular(content, tags, callbackUri) { paragraph, state, spaceWidth, modifier ->
if (paragraph is ImageGalleryParagraph) {
ImageGallery(
images = paragraph,
state = state,
accountViewModel = accountViewModel,
modifier = modifier,
roundedCorner = true,
)
} else {
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
RenderWordWithPreview(
word,
state,
backgroundColor,
quotesLeft,
callbackUri,
accountViewModel,
nav,
)
}
}
}
} else {
RenderRegular(content, tags, callbackUri) { word, state ->
RenderWordWithoutPreview(
word,
state,
backgroundColor,
accountViewModel,
nav,
)
RenderRegular(content, tags, callbackUri) { paragraph, state, spaceWidth, modifier ->
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
RenderWordWithoutPreview(
word,
state,
backgroundColor,
accountViewModel,
nav,
)
}
}
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun RenderRegularWithGallery(
content: String,
tags: ImmutableListOfLists<String>,
backgroundColor: MutableState<Color>,
quotesLeft: Int,
callbackUri: String? = null,
accountViewModel: AccountViewModel,
nav: INav,
fun RenderTextParagraph(
paragraph: ParagraphState,
spaceWidth: Dp,
modifier: Modifier,
renderWord: @Composable (word: Segment) -> Unit,
) {
val state by remember(content, tags) { mutableStateOf(CachedRichTextParser.parseText(content, tags, callbackUri)) }
val context =
RenderContext(
state = state,
backgroundColor = backgroundColor,
quotesLeft = quotesLeft,
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
val spaceWidth = measureSpaceWidth(LocalTextStyle.current)
val paragraphParser = remember { ParagraphParser() }
Column {
paragraphParser.ProcessAllParagraphs(
paragraphs = state.paragraphs,
spaceWidth = spaceWidth,
context = context,
renderSingleParagraph = { paragraph, words, width, ctx ->
paragraphParser.RenderSingleParagraphWithFlowRow(
paragraph = paragraph,
words = words,
spaceWidth = width,
context = ctx,
renderWord = { word, renderContext -> RenderWordWithPreview(word, renderContext) },
)
},
renderImageGallery = { words, ctx -> RenderWordsWithImageGallery(words, ctx) },
)
FlowRow(
modifier = modifier,
horizontalArrangement = Arrangement.spacedBy(spaceWidth),
) {
paragraph.words.forEach { word ->
renderWord(word)
}
}
}
@@ -349,7 +353,7 @@ fun RenderRegular(
content: String,
tags: ImmutableListOfLists<String>,
callbackUri: String? = null,
wordRenderer: @Composable (Segment, RichTextViewerState) -> Unit,
renderParagraph: @Composable (ParagraphState, state: RichTextViewerState, Dp, modifier: Modifier) -> Unit,
) {
val state by remember(content, tags) { mutableStateOf(CachedRichTextParser.parseText(content, tags, callbackUri)) }
@@ -376,14 +380,12 @@ fun RenderRegular(
},
LocalTextStyle provides textStyle,
) {
FlowRow(
modifier = Modifier.align(if (paragraph.isRTL) Alignment.End else Alignment.Start),
horizontalArrangement = Arrangement.spacedBy(spaceWidth),
) {
paragraph.words.forEach { word ->
wordRenderer(word, state)
}
}
renderParagraph(
paragraph,
state,
spaceWidth,
Modifier.align(if (paragraph.isRTL) Alignment.End else Alignment.Start),
)
}
}
}
@@ -435,49 +437,33 @@ private fun RenderWordWithoutPreview(
}
}
@Composable
private fun RenderWordsWithImageGallery(
words: ImmutableList<Segment>,
context: RenderContext,
) {
val paragraphParser = remember { ParagraphParser() }
paragraphParser.ProcessWordsWithImageGrouping(
words = words,
context = context,
renderSingleWord = { word, ctx -> RenderWordWithPreview(word, ctx) },
renderGallery = { imageContents, accountViewModel ->
ImageGallery(
images = imageContents,
accountViewModel = accountViewModel,
roundedCorner = true,
)
},
)
}
@Composable
private fun RenderWordWithPreview(
word: Segment,
context: RenderContext,
state: RichTextViewerState,
backgroundColor: MutableState<Color>,
quotesLeft: Int,
callbackUri: String? = null,
accountViewModel: AccountViewModel,
nav: INav,
) {
when (word) {
is ImageSegment -> ZoomableContentView(word.segmentText, context.state, context.accountViewModel)
is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, context.callbackUri, context.accountViewModel)
is EmojiSegment -> RenderCustomEmoji(word.segmentText, context.state)
is InvoiceSegment -> MayBeInvoicePreview(word.segmentText, context.accountViewModel)
is WithdrawSegment -> MayBeWithdrawal(word.segmentText, context.accountViewModel)
is CashuSegment -> CashuPreview(word.segmentText, context.accountViewModel)
is ImageSegment -> ZoomableContentView(word.segmentText, state, accountViewModel)
is LinkSegment -> LoadUrlPreview(word.segmentText, word.segmentText, callbackUri, accountViewModel)
is EmojiSegment -> RenderCustomEmoji(word.segmentText, state)
is InvoiceSegment -> MayBeInvoicePreview(word.segmentText, accountViewModel)
is WithdrawSegment -> MayBeWithdrawal(word.segmentText, accountViewModel)
is CashuSegment -> CashuPreview(word.segmentText, accountViewModel)
is EmailSegment -> ClickableEmail(word.segmentText)
is SecretEmoji -> DisplaySecretEmoji(word, context.state, context.callbackUri, true, context.quotesLeft, context.backgroundColor, context.accountViewModel, context.nav)
is SecretEmoji -> DisplaySecretEmoji(word, state, callbackUri, true, quotesLeft, backgroundColor, accountViewModel, nav)
is PhoneSegment -> ClickablePhone(word.segmentText)
is BechSegment -> BechLink(word.segmentText, true, context.quotesLeft, context.backgroundColor, context.accountViewModel, context.nav)
is HashTagSegment -> HashTag(word, context.nav)
is HashIndexUserSegment -> TagLink(word, context.accountViewModel, context.nav)
is HashIndexEventSegment -> TagLink(word, true, context.quotesLeft, context.backgroundColor, context.accountViewModel, context.nav)
is BechSegment -> BechLink(word.segmentText, true, quotesLeft, backgroundColor, accountViewModel, nav)
is HashTagSegment -> HashTag(word, nav)
is HashIndexUserSegment -> TagLink(word, accountViewModel, nav)
is HashIndexEventSegment -> TagLink(word, true, quotesLeft, backgroundColor, accountViewModel, nav)
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
is RegularTextSegment -> Text(word.segmentText)
is Base64Segment -> ZoomableContentView(word.segmentText, context.state, context.accountViewModel)
is Base64Segment -> ZoomableContentView(word.segmentText, state, accountViewModel)
}
}
@@ -636,36 +622,36 @@ fun CoreSecretMessage(
accountViewModel: AccountViewModel,
nav: INav,
) {
val context =
RenderContext(
state = localSecretContent,
backgroundColor = backgroundColor,
quotesLeft = quotesLeft,
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
if (localSecretContent.paragraphs.size == 1) {
localSecretContent.paragraphs[0].words.forEach { word ->
RenderWordWithPreview(
word,
context,
word = word,
state = localSecretContent,
backgroundColor = backgroundColor,
quotesLeft = quotesLeft,
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
}
} else if (localSecretContent.paragraphs.size > 1) {
val spaceWidth = measureSpaceWidth(LocalTextStyle.current)
val paragraphParser = remember { ParagraphParser() }
Column(CashuCardBorders) {
localSecretContent.paragraphs.forEach { paragraph ->
paragraphParser.RenderSingleParagraphWithFlowRow(
paragraph = paragraph,
words = paragraph.words.toImmutableList(),
spaceWidth = spaceWidth,
context = context,
renderWord = { word, ctx -> RenderWordWithPreview(word, ctx) },
)
val modifier = Modifier.align(if (paragraph.isRTL) Alignment.End else Alignment.Start)
RenderTextParagraph(paragraph, spaceWidth, modifier) { word ->
RenderWordWithPreview(
word,
localSecretContent,
backgroundColor,
quotesLeft,
callbackUri,
accountViewModel,
nav,
)
}
}
}
}