Fixes the crash on images not being present in the image pager.

This commit is contained in:
Vitor Pamplona
2024-02-20 16:51:18 -05:00
parent b70407f447
commit f4cee92702
2 changed files with 28 additions and 28 deletions

View File

@@ -32,7 +32,6 @@ import androidx.compose.runtime.setValue
import com.vitorpamplona.amethyst.model.UrlCachedPreviewer import com.vitorpamplona.amethyst.model.UrlCachedPreviewer
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.HalfVertPadding import com.vitorpamplona.amethyst.ui.theme.HalfVertPadding
import kotlinx.collections.immutable.persistentListOf
@Composable @Composable
fun LoadUrlPreview( fun LoadUrlPreview(
@@ -68,19 +67,17 @@ fun LoadUrlPreview(
if (state.previewInfo.mimeType.type == "image") { if (state.previewInfo.mimeType.type == "image") {
Box(modifier = HalfVertPadding) { Box(modifier = HalfVertPadding) {
ZoomableContentView( ZoomableContentView(
ZoomableUrlImage(url), content = ZoomableUrlImage(url),
persistentListOf(),
roundedCorner = true, roundedCorner = true,
accountViewModel, accountViewModel = accountViewModel,
) )
} }
} else if (state.previewInfo.mimeType.type == "video") { } else if (state.previewInfo.mimeType.type == "video") {
Box(modifier = HalfVertPadding) { Box(modifier = HalfVertPadding) {
ZoomableContentView( ZoomableContentView(
ZoomableUrlVideo(url), content = ZoomableUrlVideo(url),
persistentListOf(),
roundedCorner = true, roundedCorner = true,
accountViewModel, accountViewModel = accountViewModel,
) )
} }
} else { } else {

View File

@@ -229,12 +229,12 @@ fun figureOutMimeType(fullUrl: String): ZoomableContent {
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
fun ZoomableContentView( fun ZoomableContentView(
content: ZoomableContent, content: ZoomableContent,
images: ImmutableList<ZoomableContent> = listOf(content).toImmutableList(), images: ImmutableList<ZoomableContent> = remember(content) { listOf(content).toImmutableList() },
roundedCorner: Boolean, roundedCorner: Boolean,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
) { ) {
// store the dialog open or close state // store the dialog open or close state
var dialogOpen by remember { mutableStateOf(false) } var dialogOpen by remember(content) { mutableStateOf(false) }
// store the dialog open or close state // store the dialog open or close state
val shareOpen = remember { mutableStateOf(false) } val shareOpen = remember { mutableStateOf(false) }
@@ -847,8 +847,9 @@ private fun DialogContent(
SlidingCarousel( SlidingCarousel(
pagerState = pagerState, pagerState = pagerState,
) { index -> ) { index ->
allImages.getOrNull(index)?.let {
RenderImageOrVideo( RenderImageOrVideo(
content = allImages[index], content = it,
roundedCorner = false, roundedCorner = false,
topPaddingForControllers = Size55dp, topPaddingForControllers = Size55dp,
onControllerVisibilityChanged = { controllerVisible.value = it }, onControllerVisibilityChanged = { controllerVisible.value = it },
@@ -856,6 +857,7 @@ private fun DialogContent(
accountViewModel = accountViewModel, accountViewModel = accountViewModel,
) )
} }
}
} else { } else {
RenderImageOrVideo( RenderImageOrVideo(
content = imageUrl, content = imageUrl,
@@ -879,7 +881,7 @@ private fun DialogContent(
) { ) {
CloseButton(onPress = onDismiss) CloseButton(onPress = onDismiss)
val myContent = allImages[pagerState.currentPage] allImages.getOrNull(pagerState.currentPage)?.let { myContent ->
if (myContent is ZoomableUrlContent) { if (myContent is ZoomableUrlContent) {
Row { Row {
CopyToClipboard(content = myContent) CopyToClipboard(content = myContent)
@@ -895,6 +897,7 @@ private fun DialogContent(
} }
} }
} }
}
@Composable @Composable
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)