Dagger2 merge components for MVP

119 views Asked by At

I have a current application where i'm trying learn MVP and Dagger2 by practice but i must be missing something.

I have the following classes:

[MVP]

LoginActivity (View)

   public class LoginActivity extends AppCompatActivity implements LoginContract.View {        
            @Inject
            LoginPresenter presenter;

            @Override
            protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_login);
                DaggerLoginComponent.builder()
                        .loginModule(new LoginModule(this))
                        .build().inject(this);
         }
}

LoginPresenter (Presenter)

 public class LoginPresenter implements LoginContract.Presenter {
            private LoginContract.View view;

            @Inject LoginModel loginModel;

            @Inject
            public LoginPresenter(LoginContract.View view) {
                this.view = view;
            }
    }

LoginModel (Model)

public class LoginModel {

    Context context;

    @Inject
    public LoginModel(Context context){
        this.context=context;
    }

}

[Dagger 2]

ActivityScope

@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

ApplicationContext

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationContext {

}

ApplicationComponent

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

    @ApplicationContext
    Context getContext();

    @ApplicationContext
    BloggerPro getBloggerProApp();

}

ApplicationModule

@Module
public class ApplicationModule {

    private final Application mApplication;

    public ApplicationModule(Application mApplication) {
        this.mApplication = mApplication;
    }

    @Provides
    @ApplicationContext
    Context provideContext() {
        return mApplication;
    }

}

LoginComponent

@ActivityScope
@Component(modules = {ApplicationModule.class, LoginModule.class})
public interface LoginComponent extends ApplicationComponent {

    void inject(LoginActivity loginActivity);

}

LoginModule

@Module
public class LoginModule {

    private final LoginContract.View mView;

    public LoginModule(LoginContract.View view){
        this.mView=view;
    }

    @Provides
    @ActivityScope
    LoginContract.View providesMainScreenContractView() {
        return mView;
    }
}

When i compile the application, i get the following error:

Error:(19, 10) error: android.content.Context cannot be provided without an @Provides-annotated method.
android.content.Context is injected at
com.bloggerpro.android.app.features.login.LoginModel.<init>(context)
com.bloggerpro.android.app.features.login.LoginModel is injected at
com.bloggerpro.android.app.features.login.LoginPresenter.loginModel
com.bloggerpro.android.app.features.login.LoginPresenter is injected at
com.bloggerpro.android.app.features.login.LoginActivity.presenter
com.bloggerpro.android.app.features.login.LoginActivity is injected at
com.bloggerpro.android.di.components.LoginComponent.inject(loginActivity)

What i'm trying to do is to have separate components and combine them but so far i haven't been successfull.

Already tried to make LoginComponent inherit from ApplicationComponent but no dice as well.

0

There are 0 answers