Share
Embed
Dagger Party Tricks
Neat things you can do with dagger and common libraries/patterns in modern android development.
Given at Londroid on 2019/02/19 and Droidcon NYC on 2019/08/26
Zac Sweers
February 19, 2019
Transcript
75 slides
-
@Provides fun provideClient(): OkHttpClient { return OkHttpClient.Builder() .build() }b @Provides
fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a -
@Provides fun provideCache(ctx: Context): Cache { return Cache(ctx.cacheDir, CACHE_SIZE) }c
@Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a -
@Provides fun provideCache(ctx: Context): Cache { return Cache(ctx.cacheDir, CACHE_SIZE) }c
@Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Retrofit )e -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Retrofit )e -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Retrofit )e -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Retrofit )e -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a 100-150ms! @Inject constructor( retrofit: Retrofit )e -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a 100-150ms! @Inject constructor( retrofit: Retrofit )e -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Lazy<Retrofit> )e 100-150ms! -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Lazy<Retrofit> )e 100-150ms! retrofit.get().create(...) -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Lazy<Retrofit> )e 100-150ms! retrofit.get().create(...) -
@Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .callFactory(object :
Call.Factory by client {}) .build() }a -
@Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .callFactory {
client.newCall(it) } .build() }a -
@Provides fun provideRetrofit(client: Lazy<OkHttpClient>): Retrofit { return Retrofit.Builder() .callFactory {
client.get().newCall(it) } .build() }a -
@Provides fun provideRetrofit(client: Lazy<OkHttpClient>): Retrofit { return Retrofit.Builder() .callFactory {
client.get().newCall(it) } .build() }a -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Lazy<Retrofit> )e 100-150ms! retrofit.get().create(...) -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit(client: OkHttpClient): Retrofit { return Retrofit.Builder() .client(client) .build() }a @Inject constructor( retrofit: Lazy<Retrofit> )e 100-150ms! retrofit.get().create(...) Main thread! -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit( client: Lazy<OkHttpClient>): Retrofit { return Retrofit.Builder() .callFactory { client.get().newCall(it) } .build() }a @Inject constructor( retrofit: Retrofit )e 100-150ms! Background thread -
Courtesy of Nick Butcher (@crafty) @Provides fun provideCache(ctx: Context): Cache
{ checkMainThread() return Cache(ctx.cacheDir, CACHE_SIZE) }c @Provides fun provideClient(cache: Cache): OkHttpClient { checkMainThread() return OkHttpClient.Builder() .cache(cache) .build() }b @Provides fun provideRetrofit( client: Lazy<OkHttpClient>): Retrofit { return Retrofit.Builder() .callFactory { client.get().newCall(it) } .build() }a @Inject constructor( retrofit: Retrofit )e 100-150ms! Background thread -
@Module class MyGiantModule { @Provides fun provideSeasoning() = Seasoning() @Provides
fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }a -
@Module class MyGiantModuleBase { @Provides fun provideSeasoning() = Seasoning() }a
@Module(includes = MyGiantModuleBase::class) class MyGiantModule { @Provides fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }b -
@Module(includes = MyGiantModuleBase::class) class MyGiantModule { @Provides fun provideTaco(seasoning: Seasoning)
= Taco(seasoning) }b @Module class MyGiantModuleBase { @Provides fun provideSeasoning() = Seasoning() }a -
@Module class MyGiantModule { @Provides fun provideSeasoning() = Seasoning() @Provides
fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }a -
@Module class SeasoningModule { @Provides fun provideSeasoning() = Seasoning() }b
@Module(includes = SeasoningModule::class) class MyGiantModule { @Provides fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }a -
@Module class SeasoningModule { @Provides fun provideSeasoning() = Seasoning() }b
@Module(includes = [SeasoningModule::class, TacoModule::class]) class MyGiantModule @Module class TacoModule { @Provides fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }c -
@Module class SeasoningModule { @Provides fun provideSeasoning() = Seasoning() }b
@Module(includes = TacoModule::class) class MyGiantModule @Module(includes = SeasoningModule::class) class TacoModule { @Provides fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }c -
@Module class SeasoningModule { @Provides fun provideSeasoning() = Seasoning() }b
@Module(includes = SeasoningModule::class) class TacoModule { @Provides fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }c -
class Taco @Inject constructor(seasoning: Seasoning) Food subproject Feature A class
Taco_Factory Feature B class Taco_Factory -
class Taco @Inject constructor(seasoning: Seasoning) Food subproject Feature A class
Taco_Factory Feature B class Taco_Factory App -
class Taco @Inject constructor(seasoning: Seasoning) Food subproject Feature A class
Taco_Factory Feature B class Taco_Factory App -
class Taco @Inject constructor(seasoning: Seasoning) Food subproject Feature A class
Taco_Factory Feature B App -
@Module class FoodModule { @Provides fun provideSeasoning() = Seasoning() @Provides
fun provideTaco(seasoning: Seasoning) = Taco(seasoning) } -
@Qualifier private annotation class InternalApi @Module class FoodModule { @Provides
fun provideSeasoning() = Seasoning() @Provides fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }a -
@Qualifier private annotation class InternalApi @Module class FoodModule { @InternalApi
@Provides fun provideSeasoning() = Seasoning() @Provides fun provideTaco(@InternalApi seasoning: Seasoning) = Taco(seasoning) }a -
@Module class FoodModule { @Provides fun provideSeasoning() = Seasoning() @Provides
fun provideTaco(seasoning: Seasoning) = Taco(seasoning) }a -
@Module class FoodModule { @Provides fun provideSpicy() = SpicySeasoning() @Provides
fun provideElPaso() = ElPasoSeasoning() @Provides fun provideTaco( spicy: SpicySeasoning, elpaso: ElPasoSeasoning ) = Taco(setOf(spicy, elpaso)) }a -
@Module abstract class FoodModule { @Multibinds abstract fun seasonings(): Set<Seasoning>
@Provides fun provideSpicy() = SpicySeasoning() @Provides fun provideElPaso() = ElPasoSeasoning() @Provides fun provideTaco( spicy: SpicySeasoning, elpaso: ElPasoSeasoning ) = Taco(setOf(spicy, elpaso)) }a -
@Module abstract class FoodModule { @Multibinds abstract fun seasonings(): Set<Seasoning>
@Provides @IntoSet fun provideSpicy() = SpicySeasoning() @Provides @IntoSet fun provideElPaso() = ElPasoSeasoning() @Provides fun provideTaco(seasonings: Set<Seasoning>) = Taco(seasonings) }a -
@Module(includes = SeasoningModule::class) abstract class FoodModule { @Multibinds abstract fun
seasonings(): Set<Seasoning> @Provides fun provideTaco(seasonings: Set<Seasoning>) = Taco(seasonings) }a @Module class SeasoningModule { @Provides @IntoSet fun provideSpicy() = SpicySeasoning() @Provides @IntoSet fun provideElPaso() = ElPasoSeasoning() } -
@Module abstract class FoodModule { @Multibinds abstract fun seasonings(): Set<Seasoning>
@Provides fun provideTaco(seasonings: Set<Seasoning>) = Taco(seasonings) }a -
@Module abstract class NetworkModule { @Multibinds abstract fun interceptors(): Set<Interceptor>
@Provides fun provideOkHttp(interceptors: Set<Interceptors>) = // ... }a -
@Module abstract class DataModule { @Multibinds abstract fun adapters(): Set<JsonAdapter>
@Provides fun provideMoshi(adapters: Set<JsonAdapter>) = // ... }a -
data class ScreenKey( val id: String, val xpId: String )a
class ScreenPresenter @Inject constructor( screens: Map<ScreenKey, Provider<Screen>>, private val xpManager: XpManager ) { private val finalScreens = screens.filterKeys { xpManager.isEnabled(it.xpId) } } -
Dagger Party Tricks • github.com/ZacSweers/CatchUp • github.com/JakeWharton/sdksearch • github.com/JakeWharton/u2020 •
Dagger Powered Plugin System post: https:// bit.ly/2T2Yqkl • Dependency Injection in a multi module project: https://bit.ly/2Gz5YFR @ZacSweers