Allows the RichTextViewer to render before resolving urls.

This commit is contained in:
Vitor Pamplona
2023-05-13 12:46:11 -04:00
parent b51186de30
commit 5db04ef2b0
@@ -44,11 +44,14 @@ import com.vitorpamplona.amethyst.service.nip19.Nip19
import com.vitorpamplona.amethyst.ui.note.NoteCompose import com.vitorpamplona.amethyst.ui.note.NoteCompose
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URISyntaxException import java.net.URISyntaxException
import java.net.URL import java.net.URL
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
val imageExtensions = listOf("png", "jpg", "gif", "bmp", "jpeg", "webp", "svg") val imageExtensions = listOf("png", "jpg", "gif", "bmp", "jpeg", "webp", "svg")
val videoExtensions = listOf("mp4", "avi", "wmv", "mpg", "amv", "webm", "mov", "mp3") val videoExtensions = listOf("mp4", "avi", "wmv", "mpg", "amv", "webm", "mov", "mp3")
@@ -106,11 +109,12 @@ fun RichTextViewer(
class RichTextViewerState( class RichTextViewerState(
val content: String, val content: String,
val urlSet: LinkedHashSet<String>, val urlSet: Set<String>,
val imagesForPager: Map<String, ZoomableUrlContent>, val imagesForPager: Map<String, ZoomableUrlContent>,
val imageList: List<ZoomableUrlContent> val imageList: List<ZoomableUrlContent>
) )
@OptIn(ExperimentalTime::class)
@Composable @Composable
private fun RenderRegular( private fun RenderRegular(
content: String, content: String,
@@ -121,11 +125,13 @@ private fun RenderRegular(
navController: NavController navController: NavController
) { ) {
var processedState by remember { var processedState by remember {
mutableStateOf<RichTextViewerState?>(null) mutableStateOf<RichTextViewerState?>(RichTextViewerState(content, emptySet(), emptyMap(), emptyList()))
} }
val scope = rememberCoroutineScope()
LaunchedEffect(key1 = content) { LaunchedEffect(key1 = content) {
withContext(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
val urls = UrlDetector(content, UrlDetectorOptions.Default).detect() val urls = UrlDetector(content, UrlDetectorOptions.Default).detect()
val urlSet = urls.mapTo(LinkedHashSet(urls.size)) { it.originalUrl } val urlSet = urls.mapTo(LinkedHashSet(urls.size)) { it.originalUrl }
val imagesForPager = urlSet.mapNotNull { fullUrl -> val imagesForPager = urlSet.mapNotNull { fullUrl ->
@@ -140,7 +146,8 @@ private fun RenderRegular(
}.associateBy { it.url } }.associateBy { it.url }
val imageList = imagesForPager.values.toList() val imageList = imagesForPager.values.toList()
processedState = RichTextViewerState(content, urlSet, imagesForPager, imageList) if (urlSet.isNotEmpty())
processedState = RichTextViewerState(content, urlSet, imagesForPager, imageList)
} }
} }