appyx
appyx copied to clipboard
Make Node a viewmodel store owner
Description
Its a hacky draft showcasing possible way to integrate viewmodel with Appyx. In this case Node implements ViewmodelStore interface, allowing devs to use the same viewmodel extenstion methods used for Composables, Fragments & Activity.
On the root of the tree in ActivityIntegrationPoint we have a viewmodel that stores all of the viewmodel stores for nodes, where node id is the key in that storage. That viewmodel itself survives configuration change, with its contents.
Node clears it's ViewmodelStore when destroyed. To differentiate DESTROY event when we're recreating activity, and DESTROY when Node is actually destroyed we have to check activtiy.isChanging configuration. ComposeNavigation for example does it a bit different - it associates ViewmodelStore with backstack entry and clears it once removed from backstack. But for Appyx this approach is not suitable since we'll have to have this kind of logic in every navmodel.
Pros: Using same viewmodel() methods from androidx Cons: Having viewmodel logic in Node
Alternative: Create custom integration point, eg ViewModelIntegrationPoint and set of extetion functions to the node to create viewmodels inside node. Crash(?) when node.integrationPoint doesn't cast to ViewModelIntegrationPoint.
DI: Yuri already mentioned in #303 but I'm not sure what we can do about that
Anyway in both variants we have another problem – we change DI flow. We usually pass all dependencies into Node ready to use, but here we have to instantiate ViewModel inside Node, comparing to everything else that is done outside. Not like a bad thing, something that I want to come to later.
Check list
- [ ] I have updated
CHANGELOG.md
if required. - [ ] I have updated documentation if required.