is JetBrains, or anyone in the community, working ...
# multiplatform
w
is JetBrains, or anyone in the community, working on bidirectional Flows between Jetpack Composables and ViewModel fields, similar to SwiftUI Bindings? (or does this already exist?)
p
Not a Swiftui expert but it seems to me swiftui bindings are not the compose analog, I rather say compose MutableStateT is the right comparison here, which is bidirectional already. ViewModel enters more in the State management or State hoist theory. But honestly thinking about it twice, I don't think there is a one to one match between swiftui and compose
w
ah yes, fields would be MutableStates
really, I’m just looking for something to automagically apply UI changes back to the MutableState in the ViewModel, to cut down on boiler plate code.
right now, I create ViewModel functions that I invoke in the different composables’ event lambdas (onValueChanged for TextField, for example)
w
While not great for API isolation and maybe with some performance overhead, I've just been passing
MutableStateFlow
all the way down from my VM to the UI. With an extension that converts it to a
MutableState
delegate which I can use like normal.
p
The philosophy behind compose is state down, events up. You really never touch the state from the View, because the View doesn't really exist, is not a class instance but rather a function that just runs
Basically you pass state down the tree starting from the root Composable to leaf. You also pass down a callback function that will be invoked when you want to mutate the states. The leaf will invoke the callback asking the parent Composable hoisting its state to refresh it.
Then the parent update the state and send it down again. And they keep updating each other using this circular communication between them
But mutating the MutableState directly in the child Composable is a bad practice. You have to signal the actual parent holding the state that you want an update, and it will send the updated state down
ViewModel is just another StateHoister. Don't let the name confuse you
m
I hate that SwiftUI using bindings for everything. It's kinda nice for simple things, but once want to do something more complicated it gets in the way. My general feel from Apple's frameworks are they make the easy things really easy, and then they make hard things even harder. The Compose approach events up and data down feels so much nicer.
💯 1
p
Same feeling right here. At first view, the swiftui model seems easy to grasp but when you put time on it and try more complex stuff, the compose approach is cleaner, just a function and a state.