Skip to content

Approach on UserComponent using entry points #1

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

Open
wants to merge 3 commits into
base: interop
Choose a base branch
from

Conversation

manuelvicnt
Copy link
Owner

@manuelvicnt manuelvicnt commented Jun 22, 2020

This is a way to "insert" a component in the middle of the hierarchy.

You create a custom component as specified in the docs and access it with an entry point. As any Component needs to have a lifecycle owner, you can make that owner implement the GeneratedComponentManager interface and use an instance of it in the EntryPoints.get method.

In this case, UserComponent is managed by UserManager (which implements GeneratedComponentManager). When an object scoped to that component is needed, then the UserManager is injected into that class and the scoped type is retrieved using an entry point. UserDataRepository is scoped to UserComponent, that's what the presenters take in as a dependency. UserComponent's parent is ApplicationComponent so we're using it as a Component in the middle of application and activity components.

Current code:

  • This example uses AssistedInject but it's not necessary.
  • This solution uses Presenters instead of ViewModels but you can get them to work as shown in this gist.

Caveats:

  • This solution works well when the user has a sign-in and sign-out flow. If there's account switching while the user is on the same screen, then you need a way to restart the activity/fragment the user is in.

@@ -17,6 +17,7 @@
package com.example.android.dagger.user

import com.example.android.dagger.storage.Storage
import dagger.hilt.internal.GeneratedComponentManager

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, internal?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to avoid this, you can remove the GeneratedComponentManager interface from UserManager and pass the userManager.userComponent to the entry point. It's the same

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created google/dagger#1948 to get this addressed :)

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.

2 participants