RCH
RCH

Reputation: 1309

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?

Upvotes: 4

Views: 1596

Answers (1)

Jan Bína
Jan Bína

Reputation: 7278

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()

Upvotes: 9

Related Questions