0

This is MVP Module

@PosterFragmentScope
@Module
public class PosterFragmentModule {

    PosterFragmentMVP.View view;
    Context mContext;

    public PosterFragmentModule(PosterFragmentMVP.View view,Context mContext) {
        this.view = view;
        this.mContext = mContext;
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentPresenter providePresenter(PosterFragmentMVP.View view , PosterFragmentMVP.InterActor interActor){
        return new PosterFragmentPresenter(view,interActor,mContext);
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentMVP.View provideView(){
        return view;
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentInteractor provideInteractor(APIServices.TMDbPopular tmDbPopular,APIServices.TMDbServiceTopRated tmDbServiceTopRated){
        return new PosterFragmentInteractor(tmDbPopular,tmDbServiceTopRated);
    }

    @PosterFragmentScope
    @Provides
    public PosterFragmentMVP.Presenter providePresenterInterface(APIServices.TMDbPopular tmDbPopular,APIServices.TMDbServiceTopRated tmDbServiceTopRated){
        return providePresenter(view,provideInteractor(tmDbPopular,tmDbServiceTopRated));
    }

    @Provides
    @PosterFragmentScope
    Context provideContext() {
        return mContext;
    }

    @Provides
    @PosterFragmentScope
    public PosterAdapter provideAdapter(){
        return new PosterAdapter(mContext,new ArrayList<Movie>());
    }

    @Provides
    @PosterFragmentScope
    public APIServices.TMDbPopular provideTmDbPopular(Retrofit retrofit){
        return retrofit.create(APIServices.TMDbPopular.class);
    }

    @Provides
    @PosterFragmentScope
    public APIServices.TMDbServiceTopRated provideTmDbServiceTopRated(Retrofit retrofit){
        return retrofit.create(APIServices.TMDbServiceTopRated.class);
    }
}

This API Module

 @Module
@Singleton
public class APIModule {

    private final String BASEURL = "https://api.themoviedb.org/";

    @Provides
    @Singleton
    public OkHttpClient provideClinet (){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder().addInterceptor(interceptor).build();
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(String base_url, OkHttpClient okHttpClient){
        return new Retrofit.Builder()
                .baseUrl(base_url)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Provides
    public APIServices.TrailersService provideTrailersService() {
        return provideRetrofit(BASEURL, provideClinet()).create(APIServices.TrailersService.class);
    }

    @Provides
    public APIServices.ReviewsService provideReviewsService() {
        return provideRetrofit(BASEURL, provideClinet()).create(APIServices.ReviewsService.class);
    }

This Is APP Component

 @Component(modules = {RoomModule.class,APIModule.class})
@Singleton
public interface APPComponent {
    PosterFragmentComponent plus(PosterFragmentModule posterFragmentModule);
    MovieDetailComponent plus(MovieDetailFragmentModule movieDetailFragmentModule);
}

This Is poster Component

@Subcomponent(modules =PosterFragmentModule.class)
@PosterFragmentScope
public interface PosterFragmentComponent {
    void inject(PosterFragment posterFragment);
}

When i make this give me that error

Error:(19, 8) error: [com.example.ali.moviedb.DI.Components.PosterFragmentComponent.inject(com.example.ali.moviedb.Views.PosterFragment)] java.lang.String cannot be provided without an @Inject constructor or from an @Provides-annotated method. java.lang.String is injected at com.example.ali.moviedb.DI.Modules.APIModule.provideRetrofit(base_url, …) retrofit2.Retrofit is injected at com.example.ali.moviedb.DI.Modules.PosterFragmentModule.provideTmDbPopular(retrofit) com.example.ali.moviedb.Contracts.APIServices.TMDbPopular is injected at com.example.ali.moviedb.DI.Modules.PosterFragmentModule.providePresenterInterface(tmDbPopular, …) com.example.ali.moviedb.Contracts.PosterFragmentMVP.Presenter is injected at com.example.ali.moviedb.Views.PosterFragment.presenter com.example.ali.moviedb.Views.PosterFragment is injected at com.example.ali.moviedb.DI.Components.PosterFragmentComponent.inject(posterFragment)

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55

2 Answers2

0

Looks like you're not injecting Retrofit instance directly so shouldn't need that provides (specific error seems to be that it can't find something to inject for base_url parameter). You should be able to change to something like:

public Retrofit createRetrofit(String base_url, OkHttpClient okHttpClient){
    return new Retrofit.Builder()
            .baseUrl(base_url)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}
@Provides
public APIServices.TrailersService provideTrailersService() {
    return createRetrofit(BASEURL, provideClinet()).create(APIServices.TrailersService.class);
}

@Provides
public APIServices.ReviewsService provideReviewsService() {
    return createRetrofit(BASEURL, provideClinet()).create(APIServices.ReviewsService.class);
}
John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
0

I solved this by Make My API Module like this

 @Module
@Singleton
public class APIModule {

    private final String BASEURL = "https://api.themoviedb.org/";

    @Provides
    @Singleton
    public OkHttpClient provideClinet (){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder().addInterceptor(interceptor).build();
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(OkHttpClient okHttpClient){
        return new Retrofit.Builder()
                .baseUrl(BASEURL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

Is There best solution for that