Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rethink ViewModel scoping #313

Open
RBusarow opened this issue Sep 9, 2021 · 0 comments
Open

Rethink ViewModel scoping #313

RBusarow opened this issue Sep 9, 2021 · 0 comments

Comments

@RBusarow
Copy link
Owner

RBusarow commented Sep 9, 2021

Currently, ViewModels have their own scope, and the parent is the application scope:
image

There should really be an option to have something in between.

Hilt has an "activity retained" scope in between:
image
In a nearly-single-activity application, this allows us to use Activity as a sort of proxy for scoping. For instance, to implement a LoggedInScope, we can have different activities for logged-in vs not-logged-in workflows. However, this is coupling the framework to business logic.

The problem with ViewModels, particularly in Compose apps, is that a ViewModel can arguably be called an "entry point" into the Dagger graph. Even though a ViewModel will be constructor-injected, it's provided via the ViewModelProvider.Factory.

The existing design should be updated so that there's an Activity component in between, the same as with Hilt. However, it would be nice to provide an option for a different scope via the TangleScope annotation.

@TangleScope(UserScope::class) // optional
class UserViewModel @VMInject constructor( /* ... */ ) : ViewModel {
  // ...
}

Then in TangleViewModelFactory, we can check for a custom scope and use the corresponding component if necessary. If there is no custom scope defined, then the factory would just fall back to the default.

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

No branches or pull requests

1 participant