mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-26 21:26:29 +02:00
Optimised collectConsecutiveImageParagraphs: continue statements for early loop continuation
This commit is contained in:
@@ -20,7 +20,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.ui.components
|
package com.vitorpamplona.amethyst.ui.components
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import androidx.compose.foundation.border
|
import androidx.compose.foundation.border
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
@@ -434,34 +433,36 @@ private fun collectConsecutiveImageParagraphs(
|
|||||||
): Pair<List<ParagraphState>, Int> {
|
): Pair<List<ParagraphState>, Int> {
|
||||||
val imageParagraphs = mutableListOf<ParagraphState>()
|
val imageParagraphs = mutableListOf<ParagraphState>()
|
||||||
var j = startIndex
|
var j = startIndex
|
||||||
|
|
||||||
while (j < paragraphs.size) {
|
while (j < paragraphs.size) {
|
||||||
val currentParagraph = paragraphs[j]
|
val currentParagraph = paragraphs[j]
|
||||||
val isEmpty =
|
val words = currentParagraph.words
|
||||||
currentParagraph.words.isEmpty() ||
|
|
||||||
(
|
|
||||||
currentParagraph.words.size == 1 &&
|
|
||||||
currentParagraph.words.first() is RegularTextSegment &&
|
|
||||||
currentParagraph.words
|
|
||||||
.first()
|
|
||||||
.segmentText
|
|
||||||
.isBlank()
|
|
||||||
)
|
|
||||||
|
|
||||||
val isCurrentImageOnly =
|
// Fast path for empty check
|
||||||
currentParagraph.words.isNotEmpty() && isImageOnlyParagraph(currentParagraph)
|
if (words.isEmpty()) {
|
||||||
|
j++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if (isCurrentImageOnly) {
|
// 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
|
||||||
|
if (isImageOnlyParagraph(currentParagraph)) {
|
||||||
imageParagraphs.add(currentParagraph)
|
imageParagraphs.add(currentParagraph)
|
||||||
j++
|
j++
|
||||||
} else if (isEmpty) {
|
|
||||||
// Skip empty paragraphs but continue looking for consecutive images
|
|
||||||
j++
|
|
||||||
} else {
|
} else {
|
||||||
// Hit a non-empty, non-image paragraph - stop collecting
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Pair(imageParagraphs, j) // Return collected paragraphs and next index to process
|
|
||||||
|
return imageParagraphs to j
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalLayoutApi::class)
|
@OptIn(ExperimentalLayoutApi::class)
|
||||||
@@ -561,8 +562,6 @@ private fun RenderWordsWithImageGallery(
|
|||||||
words: ImmutableList<Segment>,
|
words: ImmutableList<Segment>,
|
||||||
context: RenderContext,
|
context: RenderContext,
|
||||||
) {
|
) {
|
||||||
val startTime = System.currentTimeMillis()
|
|
||||||
|
|
||||||
var i = 0
|
var i = 0
|
||||||
val n = words.size
|
val n = words.size
|
||||||
|
|
||||||
@@ -609,8 +608,6 @@ private fun RenderWordsWithImageGallery(
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("RichTextViewer", "RenderWordsWithImageGallery took ${System.currentTimeMillis() - startTime}ms for ${words.size} segments")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
Reference in New Issue
Block a user