Tolriq
09/23/2021, 7:52 AMval state by viewModel.state.collectAsState()
Then all the child of that component are recomposed if any child use any property of the state. What would be a proper way to collect a large state but only update sub component on some part of the state.
For example the state contains a media item and a progress state. when only the progress change I only want to recompose the progress bar and not all components that display the media information.Zach Klippenstein (he/him) [MOD]
09/23/2021, 3:34 PMstate
is not a stable type, or its properties aren’t stable, that could be the issue.Tolriq
09/23/2021, 7:24 PMRow {
LogCompositions("CompactPlayer")
val isPlaying by viewModel.isPlayingState.collectAsState()
PlayPauseIcon(isPlaying) {
viewModel.playPause()
}
StopIcon {
viewModel.stop()
}
NextIcon {
viewModel.next()
}
}
When isPlayingState change the Row is recomposed as well as all icons. If I pass the viewmodel to the PlayPauseIcon and collectasstate there then only the PlayPauseIcon is recomposedTolriq
09/23/2021, 7:24 PMval isPlayingState: StateFlow<Boolean>
and it still occurs 😞Zach Klippenstein (he/him) [MOD]
09/23/2021, 9:59 PMisPlaying
is read is the body of the Row
lambda, so when isPlaying
is changed then that lambda will be re-executedZach Klippenstein (he/him) [MOD]
09/23/2021, 10:00 PMTolriq
09/24/2021, 6:22 AMZach Klippenstein (he/him) [MOD]
09/24/2021, 4:58 PMTolriq
09/24/2021, 5:12 PMZach Klippenstein (he/him) [MOD]
09/24/2021, 5:23 PMZach Klippenstein (he/him) [MOD]
09/24/2021, 5:25 PMZach Klippenstein (he/him) [MOD]
09/24/2021, 5:25 PMTolriq
09/24/2021, 6:16 PMTolriq
09/24/2021, 6:18 PMZach Klippenstein (he/him) [MOD]
09/24/2021, 7:03 PMTolriq
09/24/2021, 7:11 PMZach Klippenstein (he/him) [MOD]
09/24/2021, 7:17 PMState
so you don’t have to collect each one and can just read it directly. You could do that directly in your view model, or just do it in your composable with something like this:
val playerState by viewModel.state.collectAsState()
val playingActive by remember { derivedStateOf { playerState.active } }
val title by remember { derivedStateOf { playerState.title } }
val subtitle by remember { derivedStateOf { playerState.subtitle } }
val progress by remember { derivedStateOf { playerState.progress } }
Zach Klippenstein (he/him) [MOD]
09/24/2021, 7:19 PMawaitFrameMillis
in a LaunchedEffect
.Tolriq
09/24/2021, 7:40 PM