Suppose you have an application where user logs in on the first screen and, from that moment on, you have access to an User
object. I would like to provide this dependency under a @SessionScope
- it means, when user logs out, all dependencies provided through a component annotated as @SessionScope
would die.
Despite the dependencies provides via @SessionScope
I would like to have dependencies provided via @ActivityScope
, an ActivityPresenter
for instance and, of course, I would have to provide dependencies from @SessionScope
and @ActivityScope
together to an Activity
consumer class.
What is the best to do it using Dagger 2 new AndroidInjector
feature?
So far I'm able to provide dependencies under @ActivityScope
like shown below:
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
@Module
abstract class ActivitiesBuilder {
@ActivityScope
@ContributesAndroidInjector(modules = arrayOf(HomepageModule::class, FacebookModule::class))
abstract fun providesHomepageViewImpl(): HomepageViewImpl
}
@Module
abstract class AppModule {
@Provides
@Singleton
fun provides (application: Application) : Context = application
}
@Singleton
@Component(
modules = arrayOf(
AndroidInjectionModule::class,
ActivitiesBuilder::class,
AppModule::class
)
)
interface AppComponent : AndroidInjector<App> {
@Component.Builder
abstract class Builder : AndroidInjector.Builder<App>()
}
class App : DaggerApplication() {
override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
}
override fun applicationInjector(): AndroidInjector<out DaggerApplication> = DaggerAppComponent.builder().create(this)
}
@SessionScope
;SessionActivityBinding
:
SessionComponent
:
@Subcomponent.Builder
and add a method that will receive a User
object as parameter;
@BindsInstance
;fun build(): SessionComponent
in order to allow the provision of this SessionComponent
- this will be necessary in order to release this component when user loggs out;SessionActivityBinding
class into the module array;SessionModule
class to provide dependencies that should be provided under @SessionScope
;AppModule
add SessionComponent
as a subcomponent;AppModule
in the list of module's from AppComponent
;SessionComponent
that must remain live until user logs out;SessionComponent
like: sessionComponent = null
- this will discard all dependencies provided under @SessionScope
;Further details and examples can be found here;