Skip to content

Make Node a viewmodel store owner#322

Closed
antonshilov wants to merge 2 commits intobumble-tech:2.xfrom
antonshilov:viewmodel-owners
Closed

Make Node a viewmodel store owner#322
antonshilov wants to merge 2 commits intobumble-tech:2.xfrom
antonshilov:viewmodel-owners

Conversation

@antonshilov
Copy link
Copy Markdown
Contributor

@antonshilov antonshilov commented Jan 19, 2023

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.

return ViewModelProvider(viewModelStoreOwner).get()
}
}
} No newline at end of file

Check warning

Code scanning / detekt

Checks whether files end with a line separator.

The file /home/runner/work/appyx/appyx/libraries/core/src/main/kotlin/com/bumble/appyx/viewmodel/IntegrationPointViewModel.kt is not ending with a new line.
import kotlinx.coroutines.flow.getAndUpdate
import kotlinx.coroutines.launch

class MyViewModel() : ViewModel() {

Check warning

Code scanning / detekt

Empty block of code detected. As they serve no purpose they should be removed.

An empty default constructor can be removed.
override fun onCleared() {
super.onCleared()
}
} No newline at end of file

Check warning

Code scanning / detekt

Checks whether files end with a line separator.

The file /home/runner/work/appyx/appyx/samples/app/src/main/kotlin/com/bumble/appyx/app/node/samples/MyViewModel.kt is not ending with a new line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants