Created
December 12, 2021 22:45
-
-
Save chao2zhang/720cbb116a82ef8ec8b8a06cf51d9acd to your computer and use it in GitHub Desktop.
Configure for OkHttp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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