Dagger 2 Multimedia Sights

Previously, I had only one AppComponent with four modules (AppModule, NetworkModule, StorageModule, PresentersModule) and everywhere introduced singleton. Recently, I decided to do a little refactoring in my application and divide it into areas. I think leaders can only live within the framework of actions, so I created @ActivityScope and ActivityModule, but the project cannot be compiled due to my lack of understanding how to mix these areas. I have read many articles and questions on stackoverflow, but everywhere there are simple examples where the modules are independent. In my case, a thing like

@Singleton @Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class }) public interface AppComponent { ActivityComponent plus(PresentersModule module); // <-- error } 

does not work. I get this error:

 Error:(19, 1) error: com.my.package.di.component.ActivityComponent scoped with @com.my.package.di.scope.ActivityScope may not reference bindings with different scopes: @Provides @Singleton android.app.Application com.my.package.di.module.AppModule.provideApplication() @Provides @Singleton com.my.package.network.FeedBurnerApi com.my.package.di.module.NetworkModule.provideFeedBurnerApi(android.app.Application) @Provides @Singleton android.database.sqlite.SQLiteOpenHelper com.my.package.di.module.StorageModule.provideSQLiteOpenHelper(android.app.Application) @Provides @Singleton com.my.package.storage.Repository com.my.package.di.module.StorageModule.provideRepository(android.database.sqlite.SQLiteOpenHelper) @Provides @Singleton com.my.package.SharedPreferencesHelper com.my.package.di.module.StorageModule.provideSharedPreferencesHelper(android.app.Application) 

So the question is, how can I get an instance of my ActivityComponent?

You can see the dependencies between the modules below:

Application module:

 @Module public final class AppModule { private final MyApplication mApplication; public AppModule(MyApplication application) { ... } @Provides @Singleton Application provideApplication() { ... } } 

Network module:

 @Module(includes = { AppModule.class }) public final class NetworkModule { @Provides @Singleton FeedBurnerApi provideFeedBurnerApi(Application application) { ... } @Provides @Singleton Retrofit provideRetrofit() { ... } } 

Storage module:

 @Module(includes = { AppModule.class }) public final class StorageModule { @Provides @Singleton Repository provideRepository(SQLiteOpenHelper sqLiteOpenHelper) { ... } @Provides @Singleton SQLiteOpenHelper provideSQLiteOpenHelper(Application application) { ... } @Provides @Singleton SharedPreferencesHelper provideSharedPreferencesHelper(Application application) { ... } } 

Speakers module:

 @Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class }) public final class PresentersModule { @Provides FeedPageViewPresenter provideFeedPageViewPresenter(FeedBurnerApi api, Repository repository, SharedPreferencesHelper preferences) { ... } @Provides @ActivityScope SlidingTabsViewPresenter provideSlidingTabsViewPresenter(Repository repository) { ... } } 

Application Component:

 @Singleton @Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class }) public interface AppComponent {} 

Event:

 @Subcomponent(modules = PresentersModule.class) @ActivityScope public interface ActivityComponent { void inject(FeedPageView view); void inject(SlidingTabsView view); } 
+5
source share
1 answer

The problem was in my view of Module.

Since subcomponents have access to the entire object from their parents, I do not need to include these dependencies in my submodule. So, I changed this code:

 @Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class }) public final class PresentersModule { ... } 

with this:

 @Module public final class PresentersModule { ... } 

and he solved my problem.

+6
source

Source: https://habr.com/ru/post/1235585/


All Articles