Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compose - image recomposition

In my view model I have:

var uri = savedStateHandle.getStateFlow("uri", Uri.EMPTY)
    private set

In my view:

val uri by viewModel.uri.collectAsState()

                Image(
                    painter = rememberAsyncImagePainter(
                        ImageRequest
                            .Builder(LocalContext.current)
                            .data(data = uri)
                            .build()
                    ),
                    contentDescription = "",
                    modifier = Modifier
                        .padding(vertical = 16.dp)
                        .size(avatarSize.value)
                        .clip(CircleShape)
                        ,
                    contentScale = ContentScale.Crop
                )

When I am saving new image it is saved with the same uri in local strage so my Image is not recomposed and old one is presented. I can change uri and then image is recomposed as intended but how to inform my Image that it should be recomposed even when uri is still the same?

like image 716
wioskamala Avatar asked Oct 17 '25 21:10

wioskamala


1 Answers

You can use coil's setParameter method on the builder which will reload whenever the parameter changes. You can use timestamp of last change as a parameter or something like that.

val timestamp by viewModel.timestamp.collectAsState()

ImageRequest
    .Builder(LocalContext.current)
    .data(data = uri)
    .setParameter("timestamp", timestamp, null)
    .build()
like image 89
Jan Bína Avatar answered Oct 20 '25 09:10

Jan Bína



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!