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

Update documentation for dagger-android in order to show how to use custom scopes #1352

Closed
roshakorost opened this issue Dec 6, 2018 · 4 comments

Comments

@roshakorost
Copy link

In Android world most apps have authorization. Dagger helps with that a lot, as it allows to create custom subcopoments/scopes for authorized user.

The following setup is very usedfull for me:

  • ApplicationScope and AplicationComponent - the root compontent with some basic AndroidDependecies.
  • NonAuthorizedScope and corresponding component, which is used accross login, sign up and screens that doesn't need authorization.
  • AuthorizedScope and its compoent, which is created after login and is kept untill logout. It is very helpfull for storing access token and some user preferences.
  • And some per screen components, which are usually unscoped.

For long time I thought that such setup is not possible to achieve by dagger-android. The documentation suggests that you should have application component and subcomponents per activity and per fragment.
However, I just found ssue #1267 which has suggestions how to organize simmialr component hirereachy and how to use it with dagger-android.

It would be wonderfull to update the documentation with such example. Also example app will be helpfull a lot.

@roshakorost
Copy link
Author

I created sample app how to use custom scopes with dagger-android https://github.com/roshakorost/Dagger-Android-Cookbook/tree/master/customscopes

The overall aproach is very simple, basically you should detect what component to use in your application.
@ContributesAndroidInjector works normally with subcomponents.

I am going to create severall small projects with varios injection stuff, related to dagger android. Please, create issues in case you want something to see.

@damirmiljkovic
Copy link

Is it possible to have two fragments with different scopes in same activity(which is in same scope as one of the fragments). I asked this question on stackoverflow(https://stackoverflow.com/questions/53761310/dagger-2-fragments-with-different-scope)

@rcmkt
Copy link

rcmkt commented Mar 28, 2019

@roshakorost Thanks a lot for sharing the example :)
I thought that's impossible as well. In the meantime, can I use dagger-android and custom scopes using relatives between components like this:

@Component(
        dependencies = [ApplicationProvider::class]
)

instead of Subcomponent?

@Chang-Eric
Copy link
Member

I'm going to close this because dagger-android isn't really being developed now that Hilt is released. While we are still keeping dagger-android maintained, I think this topic is probably complex enough that it would be a fair amount of documentation to write that unfortunately would be a fairly large amount of work.

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

5 participants