OkHttpClient DI

This commit is contained in:
Ash 2022-05-15 23:42:53 +08:00
parent 31fd54254f
commit a1021c3c3f
4 changed files with 52 additions and 11 deletions

View File

@ -79,6 +79,9 @@ class App : Application(), Configuration.Provider {
@DispatcherDefault
lateinit var dispatcherDefault: CoroutineDispatcher
@Inject
lateinit var okHttpClient: OkHttpClient
@Inject
lateinit var imageLoader: ImageLoader

View File

@ -15,7 +15,7 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.Dispatchers
import me.ash.reader.cachingHttpClient
import okhttp3.OkHttpClient
import javax.inject.Singleton
@Module
@ -25,16 +25,11 @@ object ImageLoaderModule {
@Provides
@Singleton
fun provideImageLoader(
@ApplicationContext context: Context
@ApplicationContext context: Context,
okHttpClient: OkHttpClient,
): ImageLoader {
return ImageLoader.Builder(context)
.okHttpClient(
okHttpClient = cachingHttpClient(
cacheDirectory = context.cacheDir.resolve("http")
).newBuilder()
//.addNetworkInterceptor(UserAgentInterceptor)
.build()
)
.okHttpClient(okHttpClient)
.dispatcher(Dispatchers.Default) // This slightly improves scrolling performance
.components{
add(SvgDecoder.Factory())
@ -59,4 +54,4 @@ object ImageLoaderModule {
)
.build()
}
}
}

View File

@ -0,0 +1,42 @@
package me.ash.reader.data.module
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import me.ash.reader.BuildConfig
import me.ash.reader.cachingHttpClient
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object OkHttpClientModule {
@Provides
@Singleton
fun provideOkHttpClient(
@ApplicationContext context: Context
): OkHttpClient = cachingHttpClient(
cacheDirectory = context.cacheDir.resolve("http")
).newBuilder()
.addNetworkInterceptor(UserAgentInterceptor)
.build()
}
object UserAgentInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
return chain.proceed(
chain.request()
.newBuilder()
.header("User-Agent", USER_AGENT_STRING)
.build()
)
}
}
const val USER_AGENT_STRING = "ReadYou / ${BuildConfig.VERSION_NAME}(${BuildConfig.VERSION_CODE})"

View File

@ -31,6 +31,7 @@ class RssHelper @Inject constructor(
private val context: Context,
@DispatcherIO
private val dispatcherIO: CoroutineDispatcher,
private val okHttpClient: OkHttpClient,
) {
@Throws(Exception::class)
suspend fun searchFeed(feedLink: String): FeedWithArticle {
@ -58,7 +59,7 @@ class RssHelper @Inject constructor(
@Throws(Exception::class)
suspend fun parseFullContent(link: String, title: String): String {
return withContext(dispatcherIO) {
val response = OkHttpClient()
val response = okHttpClient
.newCall(Request.Builder().url(link).build())
.execute()
val content = response.body!!.string()