How to implement it interface implementation using Dagger2?

Good evening. Thought googled but not found the answer to my question. Can you tell how to injecting with dagger 2 implementation of the interface. There are SavedListInteractor he implementation FavoritedListImpl, IgnoredListImpl. For example I have 1 fragment with a view model that is created by a factory which provides SavedListInteractor and depending on the value of the argument fragment (1 or 0) should assign me a copy FavoritedListImpl or IgnoredListImpl. No matter how much googling did not find the answer. Came only to create several components for a piece... or 1 containing the component 3 implementation.
March 23rd 20 at 18:57
1 answer
March 23rd 20 at 18:59
Solution
In the Builder component to make a method (the name is not important)
@BindsInstance
fun bindInteractor(interactor: SavedListInteractor): Builder

when you create a component in a fragment, call this method

https://dagger.dev/api/2.10/dagger/BindsInstance.html

You can still do something like this:
@Qualifier
annotation class MyFlag

@Component(modules = [InteractorModule::class])
interface MyComponent {
@Component.Builder
 interface Builder {
@BindsInstance
 fun bindMyFlag(@MyFlag flag: Int): Builder
 fun build(): MyComponent
}
...
}

@Module
object InteractorModule {
 @Provides 
 fun provideInteractor(@MyFlag flag: Int, impl0: Provider< FavoritedListImpl>, impl1: Provider< IgnoredListImpl>): SavedListInteractor = when (flag) {
 0 -> impl0.get()
 1 -> impl1.get()
 else -> throw IllegalArgumentExcepion("Unknown flag: $flag")
}
}


That is, the graph is annotated sui flag that comes up in arguments(you can do without the annotations, but will punish the gods of daggers - worse unnamed INTA in the graph only the unnamed int in the event bus. Not comme Il faut, in short). Then this flag lazily resolve which implementation to give
I'm afraid will not work, because the interactor need to create manually or to injecting Daguerre. Because the interactor, in itself, the repository accepts, and the repository web services and Dao... - dino.Schimmel14 commented on March 23rd 20 at 19:02
@dino.Schimmel14, updated the answer - Robyn67 commented on March 23rd 20 at 19:05
@Robyn67, oooo thank you so much!!! And I thought that the 3 implementation of the interactor module is the only way... - dino.Schimmel14 commented on March 23rd 20 at 19:08

Find more questions by tags Android