|
|
|
@ -17,6 +17,10 @@
|
|
|
|
|
package com.google.samples.apps.nowinandroid.core.network.di
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import coil.ImageLoader
|
|
|
|
|
import coil.decode.SvgDecoder
|
|
|
|
|
import coil.util.DebugLogger
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.BuildConfig
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager
|
|
|
|
|
import dagger.Module
|
|
|
|
|
import dagger.Provides
|
|
|
|
@ -24,6 +28,9 @@ import dagger.hilt.InstallIn
|
|
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
|
|
|
import dagger.hilt.components.SingletonComponent
|
|
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
import okhttp3.Call
|
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
|
import okhttp3.logging.HttpLoggingInterceptor
|
|
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
|
|
@Module
|
|
|
|
@ -41,4 +48,44 @@ object NetworkModule {
|
|
|
|
|
fun providesFakeAssetManager(
|
|
|
|
|
@ApplicationContext context: Context,
|
|
|
|
|
): FakeAssetManager = FakeAssetManager(context.assets::open)
|
|
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
|
@Singleton
|
|
|
|
|
fun okHttpCallFactory(): Call.Factory = OkHttpClient.Builder()
|
|
|
|
|
.addInterceptor(
|
|
|
|
|
HttpLoggingInterceptor()
|
|
|
|
|
.apply {
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
|
setLevel(HttpLoggingInterceptor.Level.BODY)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Since we're displaying SVGs in the app, Coil needs an ImageLoader which supports this
|
|
|
|
|
* format. During Coil's initialization it will call `applicationContext.newImageLoader()` to
|
|
|
|
|
* obtain an ImageLoader.
|
|
|
|
|
*
|
|
|
|
|
* @see <a href="https://github.com/coil-kt/coil/blob/main/coil-singleton/src/main/java/coil/Coil.kt">Coil</a>
|
|
|
|
|
*/
|
|
|
|
|
@Provides
|
|
|
|
|
@Singleton
|
|
|
|
|
fun imageLoader(
|
|
|
|
|
okHttpCallFactory: Call.Factory,
|
|
|
|
|
@ApplicationContext application: Context,
|
|
|
|
|
): ImageLoader = ImageLoader.Builder(application)
|
|
|
|
|
.callFactory(okHttpCallFactory)
|
|
|
|
|
.components {
|
|
|
|
|
add(SvgDecoder.Factory())
|
|
|
|
|
}
|
|
|
|
|
// Assume most content images are versioned urls
|
|
|
|
|
// but some problematic images are fetching each time
|
|
|
|
|
.respectCacheHeaders(false)
|
|
|
|
|
.apply {
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
|
logger(DebugLogger())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|