Skip to content

Instantly share code, notes, and snippets.

@LuigiPapino
Last active January 31, 2018 08:57
Show Gist options
  • Save LuigiPapino/df61489a4aa3e70f0bda0f957fc35ac4 to your computer and use it in GitHub Desktop.
Save LuigiPapino/df61489a4aa3e70f0bda0f957fc35ac4 to your computer and use it in GitHub Desktop.
Modular Architecture - Dagger2 - Browser SubComponent
@Singleton
@Component(modules = arrayOf(NetworkModule::class, RepositoryModule::class, SubcomponentModule::class))
interface ApplicationComponent : AndroidInjector<MyApplication> {
//here we declare that BrowserSubComponent is a subcomponent for ApplicationComponent,
//exposing an instance of the builder
fun browserBuilder(): BrowserSubComponent.Builder
}
class BrowserActivity: AppCompatActivity(){
@Inject // instruct dagger that this field has to be injected
internal lateinit var presenter: BrowserPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(application as MyApplication).appComponent //retrieve appComponent from MyApplication
.browserBuilder()
.build()
.inject(this) // after this the presenter instance will be injected and available
presenter.iHateAndroid()
}
}
@Browser
@Subcomponent(modules = [(BrowserModule::class)])
interface BrowserSubComponent : AndroidInjector<AppCompatActivity> {
val browserService: BrowserService
//builder for this subcomponent to be used in the parent component
@Subcomponent.Builder
abstract class Builder: AndroidInjector.Builder<AppCompatActivity>()
}
@Module
object BrowserModule{
@Browser
@Provides
@JvmStatic
fun provideBrowserService(okHttp: OkHttp): BrowserService {
return BrowserService(okHttp)
}
@Browser
@Provides
@JvmStatic
fun provideBrowserInteractor(browserService: BrowserService): BrowserInteractor {
return BrowserInteractor(browserService)
}
@Browser
@Provides
@JvmStatic
fun provideBrowserPresenter(interactor: BrowserInteractor): BrowserPresenter {
return BrowserPresenter(interactor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment