Skip to content

Instantly share code, notes, and snippets.

@LuigiPapino
Last active February 2, 2018 08:26
Show Gist options
  • Save LuigiPapino/ac3c110b4838a7bd77853b2d9fc6d99d to your computer and use it in GitHub Desktop.
Save LuigiPapino/ac3c110b4838a7bd77853b2d9fc6d99d to your computer and use it in GitHub Desktop.
Modular Architecture - Dagger2 - Browser Component
@Singleton
@Component(modules = arrayOf(NetworkModule::class, RepositoryModule::class))
interface ApplicationComponent : AndroidInjector<MyApplication> {
val userRepository: UserRepository
val apiService: ApiService
/**
* explicitly declare okHttp
**/
val okHttp: OkHttp
}
class BrowserActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DaggerBrowserComponent
.builder()
/**
* we have to provide an instance of the AppComponent or
* any other dependency for this component
**/
.plus((application as MyApplication).component)
.build()
.inject(this)
}
}
@Browser
@Component(modules = [(BrowserModule::class)],
dependencies = [(AppComponent::class)])
interface BrowserComponent : AndroidInjector<AppCompatActivity> {
@Component.Builder
abstract class Builder: AndroidInjector.Builder<AppCompatActivity>(){
/**
* explicity declare to Dagger2
* that this builder will accept an AppComponent instance
**/
abstract fun plus(component: AppComponent): Builder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment