Created
February 5, 2017 10:11
-
-
Save xanderblinov/0bb39fcba3b236e023f449062a8709f9 to your computer and use it in GitHub Desktop.
Component Dependency Injection
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 App : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
MvpFacade.init() | |
DI.init(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
@InjectViewState | |
class AuthPresenter : BasePresenter<AuthView>() { | |
@Inject | |
lateinit var authRepository: AuthRepository | |
@Inject | |
lateinit var infoRepository: InfoRepository | |
init { | |
DI.componentManager().businessLogicComponent().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
class ComponentManager(val context: Context) { | |
val internalAppComponent: AppComponent by lazy { | |
DaggerAppComponent.builder() | |
.modelModule(ModelModule(context)) | |
.busModule(BusModule()) | |
.build() | |
} | |
val internalBusinessLogicComponent by lazy { | |
internalAppComponent.plus(BusinessLogicModule()) | |
} | |
val networkSubComponentHolder = ComponentHolder<NetworkSubComponent, Guid> { | |
internalAppComponent.plus(NetworkModule(it)) | |
} | |
fun networkComponent(guid: Guid): NetworkSubComponent { | |
return networkSubComponentHolder.provideComponent(guid) | |
} | |
fun onNetworkComponentReleased(guid: Guid) { | |
networkSubComponentHolder.onDependencyReleased(guid) | |
} | |
fun businessLogicComponent() = internalBusinessLogicComponent | |
fun appComponent() = internalAppComponent | |
} |
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
object DI { | |
private lateinit var componentManager: ComponentManager | |
fun init(context: Context) { | |
componentManager = ComponentManager(context) | |
} | |
fun componentManager(): ComponentManager = componentManager | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment