Skip to content

Instantly share code, notes, and snippets.

@alex-y-su
Created September 18, 2017 21:11
Show Gist options
  • Save alex-y-su/50b4b18f7595342980c37c6ca0be541a to your computer and use it in GitHub Desktop.
Save alex-y-su/50b4b18f7595342980c37c6ca0be541a to your computer and use it in GitHub Desktop.
//------------------------------------------------------------------------------------
//In android library
//------------------------------------------------------------------------------------
@Module
@ProxyModule
class LibraryModule(val hostInjector: Injector) {
@Provides
fun proxyComponent() : IComponent = hostInjector.getInstance<IComponent>()
}
@Component
class LibraryComponent() {
@Provides
@Proxy
fun getLibraryModule(hostInjector: Injector) : LibraryModule(hostInjector)
}
class SomeFragmentInLibrary : Fragment {
override fun onCreate() {
//get
val appInjector = ... //somehow get app injector
val libInjector = Lightsaber.get().createChildInjector(appInjector, LibraryComponent())
//resolve everything we need in lib
}
}
//------------------------------------------------------------------------------------
//In anroid app
//------------------------------------------------------------------------------------
@Module
class ApplicationModule() {
@Provides
fun component() : IComponent = ComponentImpl()
}
@Component
class ApplicationComponent {
@Provides
fun appModule() = ApplicationModule()
}
//We should pass this instance to library
val appInjector = Lightsaber.get().createInjector(ApplicationComponent())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment