Skip to content
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

Using AsyncImagePreviewHandler with rememberAsyncImagePainter for Preview not working #2859

Open
colinrtwhite opened this issue Feb 21, 2025 · 0 comments

Comments

@colinrtwhite
Copy link
Member

Discussed in #2851

Originally posted by MihaMarkic February 19, 2025
Using coil 3.1.0 and Compose (obviously).

Trying to use AsyncImagePreviewHandler with rememberAsyncImagePainter for Preview but it seems like painter gets stuck in Loading state (judging from preview).

Relevant code below.
Note that if I substitute FullPhoto with AsyncImage, I see the nice yellow image instead.
Is it me doing something stupid?

@OptIn(ExperimentalCoilApi::class)
@Preview
@Composable
private fun Preview() {
    val previewHandler = AsyncImagePreviewHandler {
        ColorImage(Color.Yellow.toArgb(), 1920, 1080)
    }
    CompositionLocalProvider(LocalAsyncImagePreviewHandler provides previewHandler) {
        FullPhoto(
            "", "Sample"
        )
    }
}
@Composable
fun FullPhoto(url: String, description: String, modifier: Modifier = Modifier) {
    val painter =
        rememberAsyncImagePainter(
            model = ImageRequest.Builder(LocalPlatformContext.current)
                .data(url)
                .build()
        )
    val state by painter.state.collectAsState()

    when (state) {
        is AsyncImagePainter.State.Empty, is AsyncImagePainter.State.Loading -> {
            CircularProgressIndicator(
                modifier = Modifier
                    .width(60.dp)
                    .height(60.dp)
            )
        }

        is AsyncImagePainter.State.Success -> {
            Image(
                painter = painter,
                contentScale = ContentScale.Crop,
                contentDescription = description,
                modifier = modifier
                    .fillMaxWidth()
            )
        }

        is AsyncImagePainter.State.Error -> {
            Text("?")
        }
    }
}
```</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant