I'm still learning Dagger 2 and tried to create a simple app using it. I am having trouble making Picasso work since I can't see any errors in the logs. Here's my code
@Module(includes = {AndroidInjectionModule.class, NetworkModule.class, ViewModelModule.class})
public class AppModule {
...
@Provides
@AppScope
Picasso picasso(App app, OkHttp3Downloader okHttp3Downloader) {
return new Picasso.Builder(app.getApplicationContext())
.downloader(okHttp3Downloader)
.loggingEnabled(true)
.build();
}
...
}
This is where the OkHttp3Downloader dependency is located.
@Module
public class NetworkModule {
@Provides
@AppScope
HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> Timber.i(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
@Provides
@AppScope
public File file(App app) {
return new File(app.getApplicationContext().getCacheDir(), "okhttp_cache");
}
@Provides
@AppScope
Cache cache(File file) {
return new Cache(file, 10 * 1000 * 1000);
}
@Provides
@AppScope
OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
return new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.cache(cache)
.build();
}
@Provides
@AppScope
OkHttp3Downloader okHttp3Downloader(OkHttpClient okHttpClient) {
return new OkHttp3Downloader(okHttpClient);
}
}
@AppScope
@Component(modules = {AppModule.class, AndroidSupportInjectionModule.class, BuildersModule.class})
public interface AppComponent{
void inject(App app);
@Component.Builder
interface Builder {
@BindsInstance
Builder application(App application);
AppComponent build();
}
}
This is my App.java class where I initialized Dagger.
public class App extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
Timber.plant(new Timber.DebugTree());
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
And in my activity I made use of field injection @Inject MoviesAdapter adapter;
and made use of constructor injection in my adapter
@Inject
public MoviesAdapter(Picasso picasso) {
this.picasso = picasso;
}
I then called picasso.load(...)
method. But no images are being loaded on the ImageViews of my RecyclerView. Attached below is the Picasso log and it doesn't say any error. What might be the problem? Is there something wrong on how I initialized Picasso using Dagger? Your help is very much appreciated.
D/Picasso: Main created [R230] Request{https://image.tmdb.org/t/p/w500//8bcpki9GfXdXj9esFpPtlate8v0.jpg}
D/Picasso: Hunter removed [R222]+466ms from
D/Picasso: Dispatcher enqueued [R230]+4ms
D/Picasso: Hunter executing [R230]+5ms
D/Picasso: Main created [R231] Request{https://image.tmdb.org/t/p/w500//4nKoB6wMVXfsYgRZK5lHZ5VMQ6J.jpg}
D/Picasso: Hunter removed [R223]+635ms from
D/Picasso: Dispatcher enqueued [R231]+5ms
D/Picasso: Hunter executing [R231]+13ms
D/Picasso: Main created [R232] Request{https://image.tmdb.org/t/p/w500//5LYSsOPzuP13201qSzMjNxi8FxN.jpg}
D/Picasso: Hunter removed [R224]+706ms from
D/Picasso: Dispatcher enqueued [R232]+3ms
D/Picasso: Hunter executing [R232]+4ms
D/Picasso: Main created [R233] Request{https://image.tmdb.org/t/p/w500//2lIr27lBdxCpzYDl6WUHzzD6l6H.jpg}
D/Picasso: Hunter removed [R226]+637ms from
D/Picasso: Dispatcher enqueued [R233]+2ms
D/Picasso: Hunter executing [R233]+4ms
D/Picasso: Main created [R234] Request{https://image.tmdb.org/t/p/w500//tCBxnZwLiY1BOKw3tH6AxHZdqPh.jpg}
D/Picasso: Hunter removed [R225]+736ms from
D/Picasso: Hunter executing [R234]+4ms
D/Picasso: Dispatcher enqueued [R234]+3ms
On onBindViewHolder()
I called picasso.load()
@Override
public void onBindViewHolder(@NonNull MoviesHolder holder, int position) {
picasso.load("https://image.tmdb.org/t/p/w500/" + movieList.get(position).getPosterPath()).into(holder.ivMovie);
}
I finally figured it out. There was a problem regarding OkHttp3Downloader since when I remove it, picasso can successfully load images. Then I tried importing com.squareup.picasso.OkHttp3Downloader rather than com.jakewharton.picasso.OkHttp3Downloader and it works now. I don't really know why I can't use jakewharton's though and what's the difference between the two.