-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reload image after a failed load in Jetpack Compose #884
Comments
Still figuring out a good public API for this, but if you need this today you can force retry by changing a parameter: var retryHash by remember { mutableStateOf(0) }
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data(url)
.setParameter("retry_hash", retryHash)
.build()
)
Image(
painter = painter,
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = Modifier.fillMaxSize(),
)
when (val state = painter.state) {
is AsyncImagePainter.State.Error -> {
TextButton(onClick = { retryHash++ }) { Text("retry") }
}
} Currently, I'm thinking the public API should be something like this, but let me know what you think! val requestHandle = rememberAsyncImageRequestHandle()
val painter = rememberAsyncImagePainter(
request = ImageRequest.Builder(LocalContext.current)
.data(url)
.requestHandle(requestHandle)
.build()
)
Image(
painter = painter,
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = Modifier.fillMaxSize(),
)
when (val state = painter.state) {
is AsyncImagePainter.State.Error -> {
TextButton(onClick = { requestHandle.restart() }) { Text("retry") }
}
} |
Thanks! I'll try it later. The api is indeed a problem. Your example is very similar to |
Yep, adding a method to |
I see. In that case, I would prefer code like this: AsyncImage(...) { state ->
if (state is AsyncImagePainter.State.Error) {
TextButton(onClick = { painter.retry() }) { Text("retry") }
} else {
AsyncImageContent()
}
} I don't think the retry function should be used outside of If someone needs more complex control logic (like refreshing multiple images at once), he can use a Anyway, I prefer the direct api, mainly because |
A retry handle would be desirable. @colinrtwhite - Is this feature something you see in a near future release? |
No immediate plans for adding this (currently focused on making sure |
Are there any updates on this issue, or a workaround? |
would be nice to add a support for this |
is this implemented and exposed to the API? |
Only works for me if Also if |
I am getting this Error when i add an image URI with an "i". Heres the Code: Getting this Runtime Error if this helps: ava.lang.NoClassDefFoundError: Failed resolution of: Landroidx/compose/runtime/PrimitiveSnapshotStateKt; |
Any updates? 👀 |
Hi folks, quick update on this. This is definitely something I want to address properly in Coil 3.0. Ideally, we can kill two birds with one stone and hoist val painter = rememberAsyncImagePainter(
model = "https://example.com/image.jpg"
)
AsyncImage(
model = painter,
contentDescription = null,
)
when (painter.state) {
is AsyncImagePainter.State.Error -> {
ErrorButton(onClick = { painter.restart() })
}
} There are some implementation details that might cause this to not work in practice, but it's the API I'd like! In the meantime for 2.x I'd continue to use this solution, which isn't ideal, but should force the request to restart. |
Related to this I'd love to have the ability to disable "autostarting" the request, I recently had very slow connections speeds and it would have been nice to not automatically trigger some image loads. I assume I could wrap those images and instantly fail them with an interceptor but I think it would be even nicer UX to be able to differentiate the initial request from retries. Thanks for all the work so far! |
This is not related and you are overthinking this. You can just hide your painter and async image before the user manually clicks it. |
Sure, but I'd want to show the same placeholder and reload due to error and reload due to non autostart are conceptually similar in my mind, but it's obviously fine if coil doesn't want to take on the api burden for such a niche use case. |
Maybe not the optimal solution from the recomposition perspective but seems like we can use the .key() function to easily "reload" the AsyncImage in Coil 3. Something like:
p.s. the most efficient solution would be to perform a reloading at the network loader level (which could take the reload strategy as a parameter). |
Is your feature request related to a problem? Please describe.
After a failed load, the user should be able to reload the image via the retry button.
Describe the solution you'd like
It is better to provide
reload
method, but it seems that settingrequest
inImagePainter
to public would also work.The text was updated successfully, but these errors were encountered: