Last active
January 31, 2018 08:57
-
-
Save LuigiPapino/df61489a4aa3e70f0bda0f957fc35ac4 to your computer and use it in GitHub Desktop.
Modular Architecture - Dagger2 - Browser SubComponent
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, 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 | |
} | |
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(){ | |
@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() | |
} | |
} |
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 | |
@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