3

I need to inject in my interactor my prefrerences interfaces from a dagger module, So my question if I need to instance component in my interactor for get my preferences? ,each process need to do for any class that I need to inject?

this is my interactor.

public class SplashInteractorImpl  implements SplashContract.Interactor {

    private SplashContract.Presenter presenter;
    @Inject
    PreferencesHelper preferences;

    public SplashInteractorImpl(SplashContract.Presenter presenter){
        this.presenter=presenter;
    }

}
user1153174
  • 133
  • 1
  • 10

1 Answers1

0

so what I normally do with shared preferences is that I add a providePreferences method in my application module.

    @Provides
    @Singleton
    public PreferencesUtil providesPreferences() {
        return new PreferencesUtil(application);
    }

In the above code PreferencesUtil is a util class that wraps some preferences for me. And normally for me, all other dagger components are subcomponents of the application component. Therefore I can access(inject) the preferences anywhere I like.

Moreover, should your Interactor be holding a reference for your presenter? Correct me if I'm wrong, but the M layer of MVP should deliver the results to the presenter using some sort of callback structure, like Observables.

EDIT: To inject the preferences util inside your Interactor just call @Inject in the variable declaration:

public class MyInteractor{
    @Inject
    PreferencesUtil prefs;
}
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • Hi Levi, Thanks for your answer, In fact I have a preferencesUtil in my module application. my doubt is how inject this providePreferences in my interactor? how call a component inside my Interactor, I need to get a data from my preferences to call a api and the answer send back to my presenter. I am just new in mvp with dagger. Thanks alot for your time. – user1153174 Aug 10 '17 at 04:24