Hello I’m looking for some ideas here.
I’m trying to model a system to pass dialogs throughout my application.Currently all my components receive state and postInput parameters. the problem is calling ShowDialog passing one of my dialogs as an input looses the ability to observe value changes in state since this state is no longer the same as the one in the composable tree. I thought about passing the view model instead of the state, but that feels like breaking the model. Can anyone help me here?
In general, it’s not wrong to pass a ViewModel around your application, especially for ones that are naturally app-wide concerns like Routing or Repositories. The main reason for preferring to pass the
is more for the screen-level ViewModels, in following with the principles of Compose of lifting screen state, and helps avoid tight coupling to the ViewModel itself. So it’s preferable to lift the Ballast state, but not strictly necessary.Also, the problem here isn’t really a Ballast problem, but more general Compose. I’ve run into difficulties passing Composable lambdas up the tree because it kinda breaks the mental model and makes it difficult to update properly. You’ll basically need to observe the VM state directly within that lambda, so passing the VM itself will be necessary to update the dialog contents as you’d expect. You’ll need to think of that lambda as a new content root with nothing in it, because thats where it’s actually being placed in the composition (the root of your application with the router), despite the fact that the lambda is created further down the tree, which is really unintuitive.
So in general, I would suggest not having a lambda in your
, but maybe take an approach more similar to the route itself and just place some “token” representing the active dialog in the router. Then your screens can observe the Router state in some way, check if the token is one they care about, and render the dialog deeper within the composition rather than at the root.There are a couple ways you could implement this system, depending on how complex you want to get, where you want to post dialogs from, and how tightly you want to couple yourself to the Router.1) Just pass the Router state/postInput into your screens (or the VM itself), along with the screen’s VM state. Could potentially use a CompositionLocal or DI to hold onto the Router ViewModel to keep things tidier. This would be the simplest method, but couples your screens pretty tightly to the Router.
2) Treat the Router as a dependency of each screen’s own ViewModels. Expose the methods for showing/hiding dialogs on the individual screen contracts, and it’s only on the implementation of the InputHandler that you’re actually interacting with the Router. This can help you build your screens decoupled from the router itself and clarify which dialogs belong to which screens, at the cost of some additional boilerplate