Skip to content

Instantly share code, notes, and snippets.

@jmfayard
Last active February 15, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmfayard/3c60d5491029b8ff1f8f94503db57825 to your computer and use it in GitHub Desktop.
Save jmfayard/3c60d5491029b8ff1f8f94503db57825 to your computer and use it in GitHub Desktop.
How To Not Use Dagger2
fun app() = AppComponent.instance
interface AppComponent {
val context: Context
val retrofit: Retrofit
val okHttpClient: OkHttpClient
val catRepository: CatRepository
val catService: CatService
val mainThreadScheduler: Scheduler
val backgroundThreadScheduler: Scheduler
companion object {
lateinit var instance: AppComponent
}
}
fun app() = AppComponent.instance
@Component(modules = [CatModule::class])
interface AppComponent {
// ... properties
companion object {
lateinit var instance: AppComponent
}
}
class CustomApplication : Application() {
override fun onCreate() {
super.onCreate()
AppComponent.instance = DaggerAppComponent.builder()...build()
}
}
@Component(modules = [CatModule::class])
interface DaggerComponent {
fun context(): Context
fun retrofit(): Retrofit
fun okHttpClient(): OkHttpClient
fun catRepository(): CatRepository
fun catService(): CatService
@Named("MAIN") fun mainThreadScheduler(): Scheduler
@Named("BACKGROUND") fun backgroundThreadScheduler(): Scheduler
}
@Component(modules = [CatModule::class])
interface AppComponent {
val context: Context
val retrofit: Retrofit
val okHttpClient: OkHttpClient
val catRepository: CatRepository
val catService: CatService
@Named("MAIN") val mainThreadScheduler: Scheduler
@Named("Background")val backgroundThreadScheduler: Scheduler
}
// Common Module
fun common() = AppComponent.instance
interface CommonComponent {
val retrofit: Retrofit
companion object {
lateinit var instance: CommonComponent
}
}
interface AppComponent {
val context: Context
val retrofit: Retrofit
companion object {
lateinit var instance: AppComponent
}
}
// App Module
fun app() = AppComponent.instance
interface AppComponent {
val context: Context
val retrofit: Retrofit
companion object {
lateinit var instance: AppComponent
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment