ursus
01/06/2023, 2:28 PMwhen in every branch, vs. producing a value with the when and calling the composable with the result?
Former seems to lag the scrolling, while latter doesn’t
Code in threadursus
01/06/2023, 2:28 PM@Composable
private fun MyContent(
state: AirPodsBatteryCheckerState,
scrollState: ScrollState
) {
when (state) {
AirPodsBatteryCheckerState.Connecting -> Body(null, scrollState)
is AirPodsBatteryCheckerState.Connected -> Body(state.airPodsBatteryLevel, scrollState)
is AirPodsBatteryCheckerState.Error -> Body(AirPodsBatteryLevel(0, 0, 0), scrollState)
}
}
@Composable
private fun MyContent2(
state: AirPodsBatteryCheckerState,
scrollState: ScrollState
) {
val batteryLevel = when (state) {
AirPodsBatteryCheckerState.Connecting -> null
is AirPodsBatteryCheckerState.Connected -> state.airPodsBatteryLevel
is AirPodsBatteryCheckerState.Error -> AirPodsBatteryLevel(0, 0, 0)
}
Body(batteryLevel, scrollState)
}Filip Wiesner
01/06/2023, 2:52 PMBody UI tree and replaces it with new one. The second variant just updates input params.ursus
01/06/2023, 2:57 PMFilip Wiesner
01/06/2023, 3:18 PMMyContent compose compiler inserts replacable group which switches between two different Body nodes.ursus
01/06/2023, 3:19 PMif when , something else?Filip Wiesner
01/06/2023, 3:19 PMursus
01/06/2023, 3:19 PMFilip Wiesner
01/06/2023, 3:22 PMursus
01/06/2023, 3:26 PMFilip Wiesner
01/06/2023, 3:31 PMA replacable group is a group that cannot be moved during execution and can only either inserted, removed, or replaced. For example, the group created by most control flow constructs such as anstatement are replacable groups.if
Filip Wiesner
01/06/2023, 3:34 PMBlbe je ze si to vsimnem len tak ze scrollovanie lagne, co moze lagovat aj tak v debug modeIt's not that hard to notice in code. If there is
if , when or any other conditional logic, expect the UI to be replaced instead of restarted.
Also please write in english so people can use search and find relevant information 🙏Zach Klippenstein (he/him) [MOD]
01/06/2023, 9:41 PMmovableContentOf. Calling the exact same composable with the exact same arguments from two different places in source will result in completely different state being spun up, maintained, and torn down for each.Alex Vanyo
01/09/2023, 6:17 PM