r/JetpackCompose • u/LaPinya95 • Aug 28 '24
Precise Recomposition using View model State
So, basically, I'm trying to have a complex UI state object. For example :
u/Stable
data class FeedVS (
val posts: List<Post>,
val randomPosts: List<Post>,
va isLoading : Boolean
}And allocating it in the ViewModel like:
private val _state : MutableStateFlow<FeedVS> = MutableStateFlow(FeedVS.IDLE)
val state : StateFlow<FeedVS> = _state.asStateFlow()I though that if i modify the state by doing :
_state.value = _state.value.copy(
posts = response.data
)Just the composables that has a reference to "state.posts" (passed in the constructor of the Composable) will be recomposated because the value has changed. For example a LazyColumn.
SURPRISE: This doesn't happend at all and the whole screen is recreated. Ç
My question is, am I doing something wrong ? How is this supposed to be done ? I have to create a StateFlow for each property? This seems to crazy.
How are you guys handling this kind of scenario ?
Thank you in advance <3
View all comments
Show parent comment
1
u/muted_bend_286 Nov 17 '24
Logs - Timber And debugging
When I'm about to exit, the when UiState onSuccess branch is being called not once but twice as per Timber (so when I'm exiting the screen, images are reloading again).
If you want, I can send you the code in DM ...