Parse for video segments and exclude them from image gallery

This commit is contained in:
davotoula
2025-11-01 16:27:28 +01:00
parent 1e12decd36
commit 7c374fb586
3 changed files with 23 additions and 7 deletions
@@ -37,6 +37,7 @@ class GalleryParser {
paragraph.words.forEach { word ->
when (word) {
is ImageSegment, is Base64Segment -> imageCount++
is VideoSegment -> hasNonWhitespaceNonImageContent = true // Videos are not images
is RegularTextSegment -> {
if (word.segmentText.isNotBlank()) {
hasNonWhitespaceNonImageContent = true
@@ -146,13 +147,18 @@ class GalleryParser {
val word = paragraph.words[i]
if (word is ImageSegment || word is Base64Segment) {
// Collect consecutive image/whitespace segments without extra list allocations
// Collect consecutive image/whitespace segments (but not videos)
val imageSegments = mutableListOf<Segment>()
var j = i
var hasVideo = false
while (j < n) {
val seg = paragraph.words[j]
when {
seg is VideoSegment -> {
hasVideo = true
break
}
seg is ImageSegment || seg is Base64Segment -> imageSegments.add(seg)
seg is RegularTextSegment && seg.segmentText.isBlank() -> { /* skip whitespace */ }
else -> break
@@ -160,8 +166,9 @@ class GalleryParser {
j++
}
if (imageSegments.size <= 1) {
currentParagraphSegments.add(word)
// If we found a video, don't create a gallery - render images individually
if (hasVideo || imageSegments.size <= 1) {
currentParagraphSegments.addAll(imageSegments)
} else {
if (currentParagraphSegments.isNotEmpty()) {
resultingParagraphs.add(ParagraphState(currentParagraphSegments.toImmutableList(), paragraph.isRTL))
@@ -139,9 +139,12 @@ class RichTextParser {
val imagesForPager =
urlSet.mapNotNull { fullUrl -> createMediaContent(fullUrl, imetas, content, callbackUri) }.associateBy { it.url }
val imageUrls = imagesForPager.filterValues { it is MediaUrlImage }.keys
val videoUrls = imagesForPager.filterValues { it is MediaUrlVideo }.keys
val emojiMap = CustomEmoji.createEmojiMap(tags)
val segments = findTextSegments(content, imagesForPager.keys, urlSet, emojiMap, tags)
val segments = findTextSegments(content, imageUrls, videoUrls, urlSet, emojiMap, tags)
val base64Images = segments.map { it.words.filterIsInstance<Base64Segment>() }.flatten()
@@ -164,6 +167,7 @@ class RichTextParser {
private fun findTextSegments(
content: String,
images: Set<String>,
videos: Set<String>,
urls: Set<String>,
emojis: Map<String, String>,
tags: ImmutableListOfLists<String>,
@@ -177,7 +181,7 @@ class RichTextParser {
val wordList = paragraph.trimEnd().split(' ')
val segments = ArrayList<Segment>(wordList.size)
wordList.forEach { word ->
segments.add(wordIdentifier(word, images, urls, emojis, tags))
segments.add(wordIdentifier(word, images, videos, urls, emojis, tags))
}
paragraphSegments.add(ParagraphState(segments.toPersistentList(), isRTL))
@@ -229,6 +233,7 @@ class RichTextParser {
private fun wordIdentifier(
word: String,
images: Set<String>,
videos: Set<String>,
urls: Set<String>,
emojis: Map<String, String>,
tags: ImmutableListOfLists<String>,
@@ -239,6 +244,8 @@ class RichTextParser {
if (images.contains(word)) return ImageSegment(word)
if (videos.contains(word)) return VideoSegment(word)
if (urls.contains(word)) return LinkSegment(word)
if (CustomEmoji.fastMightContainEmoji(word, emojis) && emojis.any { word.contains(it.key) }) return EmojiSegment(word)