Don't try to draw local image if the file does not exist

This commit is contained in:
Vitor Pamplona
2023-05-02 15:53:44 -04:00
parent 99c16879c1
commit 797d1bb2b4
@@ -95,7 +95,7 @@ class ZoomableUrlImage(
url: String, url: String,
description: String? = null, description: String? = null,
hash: String? = null, hash: String? = null,
val bluehash: String? = null, val blurhash: String? = null,
dim: String? = null, dim: String? = null,
uri: String? = null uri: String? = null
) : ZoomableUrlContent(url, description, hash, dim, uri) ) : ZoomableUrlContent(url, description, hash, dim, uri)
@@ -213,33 +213,37 @@ private fun LocalImageView(
} }
Box(contentAlignment = Alignment.Center) { Box(contentAlignment = Alignment.Center) {
AsyncImage( if (content.localFile.exists()) {
model = content.localFile, AsyncImage(
contentDescription = content.description, model = content.localFile,
contentScale = ContentScale.FillWidth, contentDescription = content.description,
modifier = mainImageModifier, contentScale = ContentScale.FillWidth,
onLoading = { modifier = mainImageModifier,
imageState = it onLoading = {
}, imageState = it
onSuccess = { },
imageState = it onSuccess = {
} imageState = it
) }
)
}
if (imageState is AsyncImagePainter.State.Success) { if (imageState is AsyncImagePainter.State.Success) {
HashVerificationSymbol(content.isVerified, Modifier.align(Alignment.TopEnd)) HashVerificationSymbol(content.isVerified, Modifier.align(Alignment.TopEnd))
} }
AnimatedVisibility( if (content.blurhash != null) {
visible = imageState !is AsyncImagePainter.State.Success, AnimatedVisibility(
exit = fadeOut(animationSpec = tween(200)) visible = imageState !is AsyncImagePainter.State.Success,
) { exit = fadeOut(animationSpec = tween(200))
if (content.blurhash != null) { ) {
DisplayBlueHash(content, mainImageModifier) DisplayBlurHash(content.blurhash, content.description, mainImageModifier)
} else {
DisplayUrlWithLoadingSymbol(content)
} }
} }
if (imageState is AsyncImagePainter.State.Error) {
DisplayUrlWithLoadingSymbol(content)
}
} }
} }
@@ -286,16 +290,18 @@ private fun UrlImageView(
HashVerificationSymbol(verifiedHash, Modifier.align(Alignment.TopEnd)) HashVerificationSymbol(verifiedHash, Modifier.align(Alignment.TopEnd))
} }
AnimatedVisibility( if (content.blurhash != null) {
visible = imageState !is AsyncImagePainter.State.Success, AnimatedVisibility(
exit = fadeOut(animationSpec = tween(200)) visible = imageState !is AsyncImagePainter.State.Success,
) { exit = fadeOut(animationSpec = tween(200))
if (content.bluehash != null) { ) {
DisplayBlueHash(content, mainImageModifier) DisplayBlurHash(content.blurhash, content.description, mainImageModifier)
} else {
DisplayUrlWithLoadingSymbol(content)
} }
} }
if (imageState is AsyncImagePainter.State.Error) {
DisplayUrlWithLoadingSymbol(content)
}
} }
} }
@@ -354,38 +360,20 @@ private fun DisplayUrlWithLoadingSymbol(content: ZoomableContent) {
} }
@Composable @Composable
private fun DisplayBlueHash( private fun DisplayBlurHash(
content: ZoomableUrlImage, blurhash: String?,
description: String?,
modifier: Modifier modifier: Modifier
) { ) {
if (content.bluehash == null) return if (blurhash == null) return
val context = LocalContext.current val context = LocalContext.current
AsyncImage( AsyncImage(
model = BlurHashRequester.imageRequest( model = BlurHashRequester.imageRequest(
context, context,
content.bluehash blurhash
), ),
contentDescription = content.description, contentDescription = description,
contentScale = ContentScale.FillWidth,
modifier = modifier
)
}
@Composable
private fun DisplayBlueHash(
content: ZoomableLocalImage,
modifier: Modifier
) {
if (content.blurhash == null) return
val context = LocalContext.current
AsyncImage(
model = BlurHashRequester.imageRequest(
context,
content.blurhash
),
contentDescription = content.description,
contentScale = ContentScale.FillWidth, contentScale = ContentScale.FillWidth,
modifier = modifier modifier = modifier
) )