frogermcs/GithubClient

Make UserComponent be Sub Component of another one?

IHNEL opened this issue · 4 comments

IHNEL commented

Thank you for your sharing this tutorial, it s very helpful

I am using your idea about User Scope to share signed user instance across it's sub components. And I want to make UserComponent itself be sub component of another one, like Base Activity Component.
My graph is
App component > base activity component > User Component > User Details Component (and many others)

The arrow stands for sub-component relationship.
In your example, you used SubComponent Builder to create User component, how can i make it extends all resource provide by App component and Base Activity component?

Did you figure this out? I'm interested in ways to create App component > Activity Component > Fragment Component relationship!

IHNEL commented

@smithc42 Yes, I investigate again and again his post here: http://frogermcs.github.io/building-userscope-with-dagger2/, and then I apply similar way to my application. using @Subcomponent.Builder I can make a component be subcomponent of any other one.

For the relationship App component > Activity Component > Fragment Component , it's easy to achieve by define the <Component> plus( <module>) function in the parent component interface, once you have the parent component instance, just call this method and supply the sub-module instance, you will got new sub-component and it inherits all properties of its parents.

For more details, read this post: http://frogermcs.github.io/dependency-injection-with-dagger-2-custom-scopes/

if you are unable to build this in practices, share me the sample git and I can help.

@IHNEL Thanks for the reply. I managed to set it up correctly but Dagger kept throwing:

...dependency cannot be provided without an @provides-annotated method...a binding with matching key exists in MainComponent

I finally figured out I forgot to remove void inject(MainFragment fragment) from the root component (legacy code). Once I removed the injection from the root component it worked as expected.

Cheers!

IHNEL commented

the message from Dagger is very clear. you inject a class into somewhere but didn't define @Provide method or inject constructure