OkHttpClient DI
This commit is contained in:
parent
31fd54254f
commit
a1021c3c3f
|
@ -79,6 +79,9 @@ class App : Application(), Configuration.Provider {
|
||||||
@DispatcherDefault
|
@DispatcherDefault
|
||||||
lateinit var dispatcherDefault: CoroutineDispatcher
|
lateinit var dispatcherDefault: CoroutineDispatcher
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var okHttpClient: OkHttpClient
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var imageLoader: ImageLoader
|
lateinit var imageLoader: ImageLoader
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import me.ash.reader.cachingHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
|
@ -25,16 +25,11 @@ object ImageLoaderModule {
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideImageLoader(
|
fun provideImageLoader(
|
||||||
@ApplicationContext context: Context
|
@ApplicationContext context: Context,
|
||||||
|
okHttpClient: OkHttpClient,
|
||||||
): ImageLoader {
|
): ImageLoader {
|
||||||
return ImageLoader.Builder(context)
|
return ImageLoader.Builder(context)
|
||||||
.okHttpClient(
|
.okHttpClient(okHttpClient)
|
||||||
okHttpClient = cachingHttpClient(
|
|
||||||
cacheDirectory = context.cacheDir.resolve("http")
|
|
||||||
).newBuilder()
|
|
||||||
//.addNetworkInterceptor(UserAgentInterceptor)
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
.dispatcher(Dispatchers.Default) // This slightly improves scrolling performance
|
.dispatcher(Dispatchers.Default) // This slightly improves scrolling performance
|
||||||
.components{
|
.components{
|
||||||
add(SvgDecoder.Factory())
|
add(SvgDecoder.Factory())
|
||||||
|
|
|
@ -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})"
|
|
@ -31,6 +31,7 @@ class RssHelper @Inject constructor(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@DispatcherIO
|
@DispatcherIO
|
||||||
private val dispatcherIO: CoroutineDispatcher,
|
private val dispatcherIO: CoroutineDispatcher,
|
||||||
|
private val okHttpClient: OkHttpClient,
|
||||||
) {
|
) {
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
suspend fun searchFeed(feedLink: String): FeedWithArticle {
|
suspend fun searchFeed(feedLink: String): FeedWithArticle {
|
||||||
|
@ -58,7 +59,7 @@ class RssHelper @Inject constructor(
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
suspend fun parseFullContent(link: String, title: String): String {
|
suspend fun parseFullContent(link: String, title: String): String {
|
||||||
return withContext(dispatcherIO) {
|
return withContext(dispatcherIO) {
|
||||||
val response = OkHttpClient()
|
val response = okHttpClient
|
||||||
.newCall(Request.Builder().url(link).build())
|
.newCall(Request.Builder().url(link).build())
|
||||||
.execute()
|
.execute()
|
||||||
val content = response.body!!.string()
|
val content = response.body!!.string()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user