Hi all, I feel stuck. I have a MainScreen-Composab...
# compose
t
Hi all, I feel stuck. I have a MainScreen-Composable that contains a scaffold with a fab that is changing it's actions depending on the current screen shown in Scaffold-Content. One of those screens is the AddItem-Screen Composable that contains some TextFields. When I click on the FAB (in MainScreen-Composable) I want to keep the AddItem-Screen-State (the Text-Fields-Input) and call the AddItemViewModel.onFabSaveClicked() method (both in AdddItemScreen-Viemodel). It would be a no-brainer if the scaffold was inside AddItem-Screen-Composable but now I don't know how trigger AddItemViewModel.onFabSavedClicked() from the Fab in the MainScreen-Composable.
j
If you’re not on AddItemScreen, wouldnt AddItemScreen-ViewModel not exist yet? You probably need to hoist what you’re doing inside AddItemViewModel.onFabSaveClicked() up a level so both screens can access it
t
Yes it wouldn't exist yet. The viewModels-lifecycle is scoped to the AddItem-Screen Composable. If I hoistet it up, f.e. to MainScreen-ViewModel it would get messy fast. For me that would be breaking seperations of concerns because MainScreen should not be concerned with saving new Items. What do you think?
j
If you need to access AddItemViewModel.onFabSaveClicked() from your MainScreen isnt it already concerned about it?
t
Yeah, you are absolutely right, I would rather avoid that as well. For me the best would be If I could just listen to Fab-onClick in AddItem-Screen and then call AddItemViewModel.onFabSavedClicked() from there . I don't know how to do that and I'm searching for a clean way to achieve it.
j
Events flow up not down, so you wont be able to do that
Your only choices are for AddItemScreen to own its own fab component, or hoist this method up a level to a shared object which both screens can access
t
Yeah i suspected that... That's just not a good solution imo. Nontheless I thank you very much for your thoughts and yout time to help me with this issue!
j
If you really wanted to pass events down you could pass your own event emitter down and have your AddItemScreen subscribe to it, but I dont think its recommended