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

View File

@ -95,7 +95,7 @@ class ZoomableUrlImage(
url: String,
description: String? = null,
hash: String? = null,
val bluehash: String? = null,
val blurhash: String? = null,
dim: String? = null,
uri: String? = null
) : ZoomableUrlContent(url, description, hash, dim, uri)
@ -213,33 +213,37 @@ private fun LocalImageView(
}
Box(contentAlignment = Alignment.Center) {
AsyncImage(
model = content.localFile,
contentDescription = content.description,
contentScale = ContentScale.FillWidth,
modifier = mainImageModifier,
onLoading = {
imageState = it
},
onSuccess = {
imageState = it
}
)
if (content.localFile.exists()) {
AsyncImage(
model = content.localFile,
contentDescription = content.description,
contentScale = ContentScale.FillWidth,
modifier = mainImageModifier,
onLoading = {
imageState = it
},
onSuccess = {
imageState = it
}
)
}
if (imageState is AsyncImagePainter.State.Success) {
HashVerificationSymbol(content.isVerified, Modifier.align(Alignment.TopEnd))
}
AnimatedVisibility(
visible = imageState !is AsyncImagePainter.State.Success,
exit = fadeOut(animationSpec = tween(200))
) {
if (content.blurhash != null) {
DisplayBlueHash(content, mainImageModifier)
} else {
DisplayUrlWithLoadingSymbol(content)
if (content.blurhash != null) {
AnimatedVisibility(
visible = imageState !is AsyncImagePainter.State.Success,
exit = fadeOut(animationSpec = tween(200))
) {
DisplayBlurHash(content.blurhash, content.description, mainImageModifier)
}
}
if (imageState is AsyncImagePainter.State.Error) {
DisplayUrlWithLoadingSymbol(content)
}
}
}
@ -286,16 +290,18 @@ private fun UrlImageView(
HashVerificationSymbol(verifiedHash, Modifier.align(Alignment.TopEnd))
}
AnimatedVisibility(
visible = imageState !is AsyncImagePainter.State.Success,
exit = fadeOut(animationSpec = tween(200))
) {
if (content.bluehash != null) {
DisplayBlueHash(content, mainImageModifier)
} else {
DisplayUrlWithLoadingSymbol(content)
if (content.blurhash != null) {
AnimatedVisibility(
visible = imageState !is AsyncImagePainter.State.Success,
exit = fadeOut(animationSpec = tween(200))
) {
DisplayBlurHash(content.blurhash, content.description, mainImageModifier)
}
}
if (imageState is AsyncImagePainter.State.Error) {
DisplayUrlWithLoadingSymbol(content)
}
}
}
@ -354,38 +360,20 @@ private fun DisplayUrlWithLoadingSymbol(content: ZoomableContent) {
}
@Composable
private fun DisplayBlueHash(
content: ZoomableUrlImage,
private fun DisplayBlurHash(
blurhash: String?,
description: String?,
modifier: Modifier
) {
if (content.bluehash == null) return
if (blurhash == null) return
val context = LocalContext.current
AsyncImage(
model = BlurHashRequester.imageRequest(
context,
content.bluehash
blurhash
),
contentDescription = content.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,
contentDescription = description,
contentScale = ContentScale.FillWidth,
modifier = modifier
)