Hilt using 2 components at one android entry point

Viewed 2873

I'm new to Hilt (and dagger) and I'm not sure if the title actually is my issue but its a starting point,

I'm trying to use hilt on a recycler view adapter, it takes 4 things: an interface, glide(image loader), a type class I own, and a String identifier, Ive set up the components and modules for it and added a builder for the identifier, this seems to work as I can annotate the adapters constructor and it compiles, but I need access to the adapter in my fragment, so I thought I could just annotate the adapter field with @Inject but when i add this it fails (error below), so heres a snippet of my fragment

@AndroidEntryPoint
public class CardHolderFragment extends Fragment implements ItemTouchListener, 
SwipeRefreshLayout.OnRefreshListener {

@Inject
public CardAdapter cardAdapter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    DaggerCardAdapterComponent.builder()
            .cardAdapterDependencies(() -> layoutIdentifier)
            .build()
            .inject(this);

    DaggerSentenceViewModelComponent.builder()
            .context(getActivity())
            .appDependencies(() -> args)
            .build()
            .inject(this);

and the error I get is

/Users/martinseal/StudioProjects/SimpleAAC/app/src/main/java/com/xxx/SentenceViewModelComponent.java:14: 
error: [Dagger/MissingBinding] 
com.xxx.ItemTouchListener cannot be provided without an @Provides-annotated method.
    public interface SentenceViewModelComponent {
           ^
          com.xxx.ItemTouchListener is injected at
              com.xxx.CardAdapter(onItemTouchListener, …)
          com.xxx.CardAdapter is injected at
              com.xxx.CardHolderFragment.cardAdapter
          com.xxx.CardHolderFragment is injected at

com.xxx.SentenceViewModelComponent.inject(com.xxx.CardHolderFragment)
/Users/martinseal/StudioProjects/SimpleAAC/app/src/main/java/
com/xxx/CardAdapterComponent.java:13: error: 

[Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.

So I can see that its telling me there is a problem with it not having a provides method for the ItemTouchListener (which I did add to no avail) for SentenceViewModelComponent and CardAdapterComponent but why would they be related in this dependency graph? They are both injected into the same CardHolderFragment, heres my CardAdapterComponent:

@Singleton
@Component(dependencies = CardAdapterDependencies.class, modules = {TypeFactoryModule.class, ItemTouchListenerModule.class, GlideModule.class})

public interface CardAdapterComponent {

    void inject(CardHolderFragment cardHolderFragment);

    @Component.Builder
    interface Builder {
        Builder cardAdapterDependencies(CardAdapterDependencies cardAdapterDependencies);

        CardAdapterComponent build();
    }
}

as you can see it relies on some modules first type factory

@Module
@InstallIn(ActivityComponent.class)
public abstract class TypeFactoryModule {

    @Binds
    abstract TypeFactory bindTypeFactory(TypeFactoryForList typeFactoryForList);
}

then the item touch listener

@Module
@InstallIn(ActivityComponent.class)
public abstract class ItemTouchListenerModule {
    @Binds
    abstract ItemTouchListener bindItemTouchListener(CardHolderFragment cardHolderFragment);
}

and then glide

@Module
@InstallIn(ApplicationComponent.class)
public class GlideModule {

    @Singleton
    @Provides
    public static RequestManager provideGlideRequestManager(@ApplicationContext Context context) {
        return Glide.with(context);
    }
}

and then the dependencies for the component

@EntryPoint
@InstallIn(ApplicationComponent.class)
public interface CardAdapterDependencies {

    @LayoutIdentifier String layoutIdentifier();

}

LayoutIdentifier is a qualifier with a RetentionPolicy of RUNTIME which qualifies my layout identifier module

@Module
@InstallIn(ApplicationComponent.class)
public class LayoutIdentifierModule {

    @Singleton
    @Provides
    @LayoutIdentifier
    public String provideLayoutIdentifier(){ return Constants.MAIN_IDENTIFIER; };

}

my SentenceViewModelComponent doesn't rely on any of these

@Singleton
@Component(dependencies = SentenceViewModelDependencies.class)
public interface SentenceViewModelComponent {

    void inject(CardHolderFragment cardHolderFragment);


    @Component.Builder
    interface Builder {
        Builder context(@BindsInstance Context context);
        Builder appDependencies(SentenceViewModelDependencies sentenceViewModelDependencies);
        SentenceViewModelComponent build();
    }

}

but does have a dependency on SentenceViewModelDependencies

@EntryPoint
@InstallIn(ApplicationComponent.class)
public interface SentenceViewModelDependencies {

    @SentenceTypeAndDescription String[] sentenceTypeAndDescription();

}

which has a qualifier called SentenceTypeAndDescription which annotates a module SentenceTypeAndDescriptionModule

@Module
@InstallIn(ApplicationComponent.class)
public class SentenceTypeAndDescriptionModule {

    @Singleton
    @Provides
    @SentenceTypeAndDescription
    public String[] provideSentenceTypeAndDescription(){ return new String[]{Constants.QUICKS, "PRONOUNS"}; };

}

I fail to see how the error is related other than that they both inject this same fragment, is there something Im missing maybe around sub components that I should be using here? any help would be appreciated

0 Answers
Related