Caching images that cause errors and avoid calling them again.

This commit is contained in:
Vitor Pamplona 2023-04-20 17:18:53 -04:00
parent fe6ef1df0a
commit cca7a10322

View File

@ -50,11 +50,13 @@ fun RobohashAsyncImage(
} }
} }
var imageErrors = setOf<String>()
@Composable @Composable
fun RobohashFallbackAsyncImage( fun RobohashFallbackAsyncImage(
robot: String, robot: String,
robotSize: Dp, robotSize: Dp,
model: String?, model: String,
contentDescription: String?, contentDescription: String?,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
alignment: Alignment = Alignment.Center, alignment: Alignment = Alignment.Center,
@ -63,6 +65,19 @@ fun RobohashFallbackAsyncImage(
colorFilter: ColorFilter? = null, colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality filterQuality: FilterQuality = DrawScope.DefaultFilterQuality
) { ) {
if (imageErrors.contains(model)) {
RobohashAsyncImage(
robot = robot,
robotSize = robotSize,
contentDescription = contentDescription,
modifier = modifier,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter,
filterQuality = filterQuality
)
} else {
val context = LocalContext.current val context = LocalContext.current
val painter = with(LocalDensity.current) { val painter = with(LocalDensity.current) {
rememberAsyncImagePainter( rememberAsyncImagePainter(
@ -85,9 +100,13 @@ fun RobohashFallbackAsyncImage(
contentScale = contentScale, contentScale = contentScale,
alpha = alpha, alpha = alpha,
colorFilter = colorFilter, colorFilter = colorFilter,
filterQuality = filterQuality filterQuality = filterQuality,
onError = {
imageErrors = imageErrors + model
}
) )
} }
}
@Composable @Composable
fun RobohashAsyncImageProxy( fun RobohashAsyncImageProxy(
@ -101,7 +120,8 @@ fun RobohashAsyncImageProxy(
colorFilter: ColorFilter? = null, colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality filterQuality: FilterQuality = DrawScope.DefaultFilterQuality
) { ) {
if (model.url == null) { val proxy = model.proxyUrl()
if (proxy == null) {
RobohashAsyncImage( RobohashAsyncImage(
robot = robot, robot = robot,
robotSize = model.size, robotSize = model.size,
@ -117,7 +137,7 @@ fun RobohashAsyncImageProxy(
RobohashFallbackAsyncImage( RobohashFallbackAsyncImage(
robot = robot, robot = robot,
robotSize = model.size, robotSize = model.size,
model = model.proxyUrl(), model = proxy,
contentDescription = contentDescription, contentDescription = contentDescription,
modifier = modifier, modifier = modifier,
alignment = alignment, alignment = alignment,