Frank
07/23/2021, 3:11 PMBryan Herbst
07/23/2021, 3:14 PMFrank
07/23/2021, 3:14 PMFrank
07/23/2021, 3:15 PMFrank
07/23/2021, 3:15 PMFrank
07/23/2021, 3:15 PMFrank
07/23/2021, 3:15 PMFrank
07/23/2021, 3:15 PMFrank
07/23/2021, 3:16 PMBryan Herbst
07/23/2021, 3:16 PMcollectAsState()?
https://developer.android.com/jetpack/compose/libraries#streamsFrank
07/23/2021, 3:18 PMvm.countChannel.consumeAsFlow().collectAsState(99)
But this works only onceFrank
07/23/2021, 3:19 PMFrank
07/23/2021, 3:19 PMFrank
07/23/2021, 3:19 PMAdam Powell
07/23/2021, 3:27 PMcountChannel need to be a channel or could it be snapshot state? e.g.
var count by mutableStateOf(99)
private set
accessing vm.count directly in your @Composable fun will observe updates to count made from elsewhere.Frank
07/23/2021, 3:31 PMFrank
07/23/2021, 3:31 PMFrank
07/23/2021, 3:32 PMFrank
07/23/2021, 3:32 PMFrank
07/23/2021, 3:33 PMFrank
07/23/2021, 3:33 PMAdam Powell
07/23/2021, 3:37 PMFrank
07/23/2021, 4:41 PMFrank
07/23/2021, 4:42 PMFrank
07/23/2021, 4:43 PMDominaezzz
07/23/2021, 5:14 PMremember the right stuff.Frank
07/23/2021, 5:23 PMMichael Paus
07/24/2021, 9:02 AMFrank
07/24/2021, 9:02 AMFrank
07/24/2021, 10:09 AMFrank
07/24/2021, 10:10 AMval cc: State<Int> = vm.countChannel.consumeAsFlow().collectAsState(0)Dominaezzz
07/24/2021, 10:15 AMval cc: State<Int> = remember(vm.countChannel) { vm.countChannel.consumeAsFlow() }.collectAsState(0)Frank
07/24/2021, 10:17 AMFrank
07/24/2021, 10:41 AMval s = remember { mutableStateOf(0) }Frank
07/24/2021, 10:49 AMDominaezzz
07/24/2021, 11:10 AMAdam Powell
07/24/2021, 3:35 PMAdam Powell
07/24/2021, 3:38 PMChannel.consumeAsFlow you were not only consuming values from the viewmodel - changing the viewmodel as you receive those values from the channel - consumeAsFlow enforces a single collector and cancels the channel after that single collector cancels the collection of the converted flow. Recomposing without the remember meant collectAsState cancelled collection of the flow to start collection of a new one, but that cancellation also rendered the channel unusable for the future.Adam Powell
07/24/2021, 3:39 PMAdam Powell
07/24/2021, 3:41 PMChannel.consumeAsFlow to Channel.receiveAsFlow then it won't enforce only a single collector or cancel the channel, but it will fan-out values instead, so if you have multiple composables observing that same viewmodel again, some values would go to composable A and some would go to composable BAdam Powell
07/24/2021, 3:42 PMmutableStateOf to store the count here instead. If you wanted to be more complicated about it you could use a StateFlow.Adam Powell
07/24/2021, 3:44 PMFrank
07/25/2021, 7:23 AMFrank
07/25/2021, 8:06 AMDominaezzz
07/25/2021, 8:13 AM.value.Frank
07/25/2021, 8:13 AMFrank
07/25/2021, 8:15 AM