0
votes

I'm creating an application in Kotlin using the MVP pattern. I would need to inject a Repository into my Presenter for this purpose. Except that for this, my Repository requires a Retrofit interface as a parameter of its constructuor.

I'm a beginner in the use of Dagger2, and the answers found on the internet are far too complicated for such a basic case like mine.

Here's the repository i want to be injected :

class RepositoryInventory(private val api: Service): IRepositoryInventory {

    override fun getInventoryItemByNum(itemnum: String): Observable<Response<Item>> {
        return api.getInventoryItemByNum(itemnum)
            .toObservable()
    }

    override fun getAllInventoryItems(): Single<Response<Item>> {
        return api.getAllInventoryItems()
    }
}

My Component

@Singleton
@Component(modules = arrayOf(ActivityModule::class))
interface ActivityComponent {

    fun inject(loginActivity: LoginActivity)

    fun inject(itemDetailActivity: ItemDetailActivity)
}

My module :

@Module
class ActivityModule(private var activity: Activity) {

    @Provides
    fun provideActivity(): Activity {
        return activity
    }

    @Provides
    fun provideLoginPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    @Provides
    fun provideItemDetailPresenter(): ItemDetailPresenter {
        return ItemDetailPresenter()
    }
}

In my activity, my module is injected with this method :

private fun injectDependency() {
        val activityComponent = DaggerActivityComponent.builder()
            .activityModule(ActivityModule(this))
            .build()

        activityComponent.inject(this)
    }

I have 2 components and 2 modules: one designed to inject into a fragment and the other into an activity. Except in my case, I want to inject into a Presenter that is not a Fragment or an Activity but a class

1
Have you created a module? If yes, please post your code: modules, presenter, activity/fragment with that presenter - Bach Vu
I have edited my post - mamenejr

1 Answers

0
votes

Ok, my guess is you want to inject RepositoryInventory into LoginPresenter. If so, you can make use of @ContributesAndroidInjector and Binds

First, create a LoginActivityModule

@Module
abstract class LoginActivityModule {

    @Binds
    abstract fun loginPresenter(loginPresenter: LoginPresenter): LoginPresenter
}

Then, create a module called ActivityBindingModule

@Module
abstract class ActivityBindingModule {

    @ContributesAndroidInjector(modules = [LoginActivityModule::class])
    abstract fun loginActivity(): LoginActivity

    @ContributesAndroidInjector()
    abstract fun itemDetailActivity(): ItemDetailActivity
}

And change your ActivityComponent like this

@Singleton
@Component(modules = arrayOf(ActivityModule::class, ActivityBindingModule::class))
interface ActivityComponent {
}

And in your LoginPresenter:

class LoginPresenter @Inject constructor(private val repositoryInventory: IRepositoryInventory) {
...
}

Remember to remove this in ActivityModule:

@Provides
fun provideLoginPresenter(): LoginPresenter {
    return LoginPresenter()
}