0

just started picking up Compose MP and have been running into difficulty combining 2 sources of mutablestates to a new mutablestate or flow within a viewmodel.

per documentation https://developer.android.com/develop/ui/compose/state-hoistingI figured snapshotflow would be what I needed but it only seems to emit the initial value when viewmodel is initialized, while manipulations to v1 and v2 later do not emit further events.

I've attempted the following (v1, v2 are mutablestate objects)

in viewmodel

combinedFlow = snapshotFlow { v1 to v2 } 
    .mapLatest { (v1, v2) -> v1.value && v2.value } }
    .stateIn(viewmodelscope)

in composable

...
Button(...
        enabled = screenModel.combinedFlow.collectAsState(false).value,
...

what am I missing? If there are better practices for achieving this please say so, I remember something like transformations existed with livedata and combine for Rx.

thanks!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.