Trying to understand the following example and do something similar.
In my application there is a module, in which I want to use Dagger.
To do this I need an Application class in which I initialize and store AppComponent
Judging from the documentation I need to create an interface with a component from my module:
interface PasscodeSetupComponentProvider {
fun providePasscodeSetupComponent(): PasscodeComponent
}
Then I will implement this interface for my Application class:
open class FenturyApplication : PasscodeSetupComponentProvider {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder()
.appModule(AppModule(applicationContext))
.build()
}
override fun providePasscodeSetupComponent(): PasscodeComponent {
return appComponent.passcodeComponent
}
}
But judging by the example from the documentation I don't understand what should be in my interface Appcomponent
namely passcodeComponent
.
In the example, it looks like this:
class MyApplication: Application(), LoginComponentProvider {
// Reference to the application graph that is used across the whole app
val appComponent = DaggerApplicationComponent.create()
override fun provideLoginComponent(): LoginComponent {
return appComponent.loginComponent().create()
}
}
I added the following code to my AppComponent:
@Component(modules = [AppModule::class])
@Singleton
interface AppComponent {
val applicationContext: Context
fun passcodeComponent(): PasscodeComponent
}
And if I understood correctly, then in the fragment that is in my module, I can write the following:
lateinit var passcodeComponent: PasscodeComponent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = activity?.applicationContext ?: return
passcodeComponent = (context as PasscodeSetupComponentProvider).providePasscodeSetupComponent()
passcodeComponent.inject(this)
After that I hope I can use dagger in my module.