0

I try create MVP + dagger2

I create Model moudule:

@Module
class ModelsModule {

    @Provides
    BasketModel provideBasketModel() {
        return new BasketModel();
    }

    @Provides
    ProductModel provideProductModel() {
        return new ProductModel();
    }
}

and I need create Presenters. My presenter must use model

Presenters:

public class ProductPresenter {

    private ProductModel;

    public ProductPresenter(ProductModel productModel) {
        this.productModel = productModel;
    }

   publict void test(){
      productModel.someMethod();
     }

And I can not set ProductModel when create Presenter. Presenter I create like this:

@Module
public class PresentersModule {

    @Provides
    ProductPresenter provideProductPresenter() {
        return new ProductPresenter();//What I need set to constructor? new ProductModel()?
    }
Dilip
  • 2,622
  • 1
  • 20
  • 27
ip696
  • 6,574
  • 12
  • 65
  • 128

1 Answers1

1

Since you are passing ProductModel in your presenter class you also need to tell your PresenterModule how to construct your presenter :

@Module
public class PresentersModule {

@Provides
ProductPresenter provideProductPresenter(ProductModel model) {
    return new ProductPresenter(model);
    }
}

Dagger is clever enough to find out that you have already build your model instance in another @Module class.

I think you also need to annotate your Presenter's constructor with @Inject like :

@Inject
public ProductPresenter(ProductModel productModel) {
    this.productModel = productModel;
}

EDIT : And obviously you need a @Component interface. You haven't post any related code but I assume you have one.

mt0s
  • 5,781
  • 10
  • 42
  • 53