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