james
02/24/2020, 4:47 AMbind<Retrofit.Builder>(tag = AuthType.OAUTH) with provider {
Retrofit.Builder()
.client(instance<OkHttpClient>(tag = /* How do I access the tag used above on provider line? */))
.addConverterFactory(JsonConverterFactory())
}
the use case here is that for some cases, the provider block might be many lines, and I don't want to have to repeat the code for each new AuthType
, so I was wondering can I just pass that through, rather than repeat the code each time.
is something like this possible?romainbsl
02/24/2020, 8:09 AMromainbsl
02/24/2020, 8:10 AMfactory
binding, this is like provider, but with parameter.romainbsl
02/24/2020, 8:11 AMbind<Retrofit.Builder>(tag = AuthType.OAUTH) with factory { param ->
Retrofit.Builder()
.client(instance<OkHttpClient>(tag = param))
.addConverterFactory(JsonConverterFactory())
}
romainbsl
02/24/2020, 8:12 AMromainbsl
02/24/2020, 8:14 AMmultiton
that could alos be called are singleton factoryjames
02/24/2020, 11:54 PMjames
02/24/2020, 11:55 PMjames
02/24/2020, 11:55 PMorg.kodein.di.Kodein$NotFoundException: No binding found for bind<Retrofit.Builder>(tag = "OAUTH") with ? { ? }
Available bindings for this type:
module com.myframework.NetworkModule {
bind<Retrofit.Builder>(tag = "OAUTH") with factory { AuthType -> Retrofit.Builder }
bind<Retrofit.Builder>(tag = "EMAIL") with factory { AuthType -> Retrofit.Builder }
}
james
02/24/2020, 11:55 PMromainbsl
02/25/2020, 6:10 AMjames
02/25/2020, 6:18 AMjames
02/25/2020, 6:18 AMbind<Retrofit.Builder>(tag = AuthType.OAUTH) with provider {
getRetrofitBuilder(okHttpClient = instance<OkHttpClient>(tag = AuthType.OAUTH))
}
james
02/25/2020, 6:18 AMbind<Retrofit.Builder>(tag = AuthType.OAUTH) with factory { authType ->
getRetrofitBuilder(okHttpClient = instance<OkHttpClient>(authType))
}
james
02/25/2020, 6:19 AMromainbsl
02/25/2020, 9:29 AMjames
02/25/2020, 11:01 PMjames
02/25/2020, 11:01 PMbind<ApiService>() with provider {
instance<Retrofit.Builder>(tag = AuthType.OAUTH)
.baseUrl(AuthModule.BaseUrl)
.build()
.create(ApiService::class.java)
}
romainbsl
02/26/2020, 6:56 AMauthType
, the tag
parameter of the instance()
isn't a factory parameter, but a way to manage different instances of the same type. In your case I'm not sure that you need to tag your bindings.romainbsl
02/26/2020, 6:57 AMinstance<OkHttpClient>(authType))
, this means that you also have another factory for the type OkHttpClient
?romainbsl
02/26/2020, 6:57 AMromainbsl
02/26/2020, 7:02 AMbind<OkHttpClient>() with factory { authType: AuthType ->
OkHttpClient>(authType)
}
Retrofit.Builder factory
bind<Retrofit.Builder>() with factory { authType: AuthType ->
getRetrofitBuilder(okHttpClient = instance<OkHttpClient>(arg = authType))
}
ApiService provider
bind<ApiService>() with provider {
instance<Retrofit.Builder>(arg = AuthType.OAUTH)
.baseUrl(AuthModule.BaseUrl)
.build()
.create(ApiService::class.java)
}
romainbsl
02/26/2020, 7:02 AM