diff --git a/app/src/main/java/me/ash/reader/App.kt b/app/src/main/java/me/ash/reader/App.kt index 0e463be..2b194b8 100644 --- a/app/src/main/java/me/ash/reader/App.kt +++ b/app/src/main/java/me/ash/reader/App.kt @@ -79,6 +79,9 @@ class App : Application(), Configuration.Provider { @DispatcherDefault lateinit var dispatcherDefault: CoroutineDispatcher + @Inject + lateinit var okHttpClient: OkHttpClient + @Inject lateinit var imageLoader: ImageLoader diff --git a/app/src/main/java/me/ash/reader/data/module/ImageLoaderModule.kt b/app/src/main/java/me/ash/reader/data/module/ImageLoaderModule.kt index bd79879..795c2f1 100644 --- a/app/src/main/java/me/ash/reader/data/module/ImageLoaderModule.kt +++ b/app/src/main/java/me/ash/reader/data/module/ImageLoaderModule.kt @@ -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() } -} \ No newline at end of file +} diff --git a/app/src/main/java/me/ash/reader/data/module/OkHttpClientModule.kt b/app/src/main/java/me/ash/reader/data/module/OkHttpClientModule.kt new file mode 100644 index 0000000..dcd9224 --- /dev/null +++ b/app/src/main/java/me/ash/reader/data/module/OkHttpClientModule.kt @@ -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})" \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt index 55e1b11..18df76c 100644 --- a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt +++ b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt @@ -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()