Home > Enterprise >  Compose - image recomposition
Compose - image recomposition

Time:10-05

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?

CodePudding user response:

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