Search code examples
androidkotlindagger-2dagger

Dagger 2 - Error while providing a dependency


I'm really new with Dagger 2, I know how it works and what it does, but I'm getting some problems trying to implement it into my project.

My objective for now, is just inject the presenter into my view, the goal is to decouple my view of doing

presenter = Presenter(myInteractor())

This is what I have tried

MyAppApplication

class MyAppApplication: Application() {

    lateinit var presentationComponent: PresentationComponent

    override fun onCreate() {
        super.onCreate()
        createPresentationComponent()
    }

    private fun createPresentationComponent() {
        presentationComponent = DaggerPresentationComponent.builder()
            .presentationModule(PresentationModule(this))
            .build()
    }
}

PresentationComponent

@Component(modules = arrayOf(PresentationModule::class))

@Singleton
interface PresentationComponent {

    fun inject(loginActivity: LoginView)
    fun loginUserPresenter(): LoginPresenter
}

PresentationModule

@Module
class PresentationModule(application: Application) {


    @Provides @Singleton fun provideLoginUserPresenter(signInInteractor: SignInInteractor): LoginPresenter {
        return LoginPresenter(signInInteractor)
    }

}

SignInInteractor

interface SignInInteractor {

    interface SignInCallBack {
        fun onSuccess()
        fun onFailure(errormsg:String)
    }

    fun signInWithEmailAndPassword(email:String,password:String,listener:SignInCallBack)
    fun firebaseAuthWithGoogle(account: GoogleSignInAccount, listener:SignInCallBack)

}

Now, I thinked that this is all I needed to inject the interactor into my presenter without any problems and then inject the presenter inside my view, but is giving me this error

error: [Dagger/MissingBinding] com.myapp.domain.interactor.logininteractor.SignInInteractor cannot be provided without an @Provides-annotated method.

I'm kinda confused because If I just provide the presentationModule that is responsible to bind my signInInteractor into my Presenter, it should be working, but is not.

Thanks in advance for any help


Solution

  • It's as the error message says, you're trying to pass a SignInInteractor in your PresentationModule to your LoginPresenter, yet you're not providing an implementation for it anywhere. A possible solution would be to add the following block of code to your PresentationModule:

    @Provides @Singleton fun provideSignInInteractor(): SignInInteractor {
      return TODO("Add an implementation of SignInInteractor here.")
    }
    

    Of course the TODO needs to be replaced by a SignInInteractor of your choosing (the myInteractor() function would work for example). Then that SignInInteractor will be used by your LoginPresenter. Hope that helps!