Last active
February 2, 2018 08:26
-
-
Save LuigiPapino/ac3c110b4838a7bd77853b2d9fc6d99d to your computer and use it in GitHub Desktop.
Modular Architecture - Dagger2 - Browser Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
@Component(modules = arrayOf(NetworkModule::class, RepositoryModule::class)) | |
interface ApplicationComponent : AndroidInjector<MyApplication> { | |
val userRepository: UserRepository | |
val apiService: ApiService | |
/** | |
* explicitly declare okHttp | |
**/ | |
val okHttp: OkHttp | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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