Skip to content

Instantly share code, notes, and snippets.

@chao2zhang
Created December 12, 2021 22:45
Show Gist options
  • Save chao2zhang/720cbb116a82ef8ec8b8a06cf51d9acd to your computer and use it in GitHub Desktop.
Save chao2zhang/720cbb116a82ef8ec8b8a06cf51d9acd to your computer and use it in GitHub Desktop.
Configure for OkHttp
@Provides
@Singleton
fun okhttpClient(
threadPoolExecutor: ThreadPoolExecutor,
@ApplicationContext applicationContext: Context
): OkHttpClient {
val client = OkHttpClient.Builder()
.dispatcher(Dispatcher(threadPoolExecutor))
.build()
Coil.setImageLoader {
ImageLoader.Builder(applicationContext).okHttpClient(client).build()
}
return client
}
@Provides
@Singleton
fun retrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl("https://www.reddit.com")
.addConverterFactory(MoshiConverterFactory.create())
.client(okHttpClient)
.build()
@Provides
@Singleton
fun redditService(retrofit: Retrofit): RedditService = retrofit.create(RedditService::class.java)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment