diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 80bb9b814a..26e6afc6b9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -126,6 +126,7 @@ dependencies { /* Retrofit and Kotlinx Serialization */ implementation(libs.square.retrofit) + implementation(libs.logging.interceptor) implementation(libs.kotlinx.serialization) implementation(libs.kotlinx.datetime) implementation(libs.kotlinx.serialization.retrofit) diff --git a/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt b/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt index 2a6abcc3ab..372b926ed3 100644 --- a/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt +++ b/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt @@ -1,9 +1,12 @@ package com.github.libretube.api +import com.github.libretube.BuildConfig import com.github.libretube.constants.PreferenceKeys import com.github.libretube.helpers.PreferenceHelper import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit import retrofit2.create @@ -28,10 +31,13 @@ object RetrofitInstance { private val kotlinxConverterFactory = JsonHelper.json .asConverterFactory("application/json".toMediaType()) + private val httpClient by lazy { buildClient() } + val api by resettableLazy(lazyMgr) { Retrofit.Builder() .baseUrl(apiUrl) .callFactory(CronetHelper.callFactory) + .client(httpClient) .addConverterFactory(kotlinxConverterFactory) .build() .create() @@ -41,6 +47,7 @@ object RetrofitInstance { Retrofit.Builder() .baseUrl(authUrl) .callFactory(CronetHelper.callFactory) + .client(httpClient) .addConverterFactory(kotlinxConverterFactory) .build() .create() @@ -50,8 +57,23 @@ object RetrofitInstance { Retrofit.Builder() .baseUrl(apiUrl) .callFactory(CronetHelper.callFactory) + .client(httpClient) .addConverterFactory(kotlinxConverterFactory) .build() .create() } + + private fun buildClient(): OkHttpClient { + val httpClient = OkHttpClient().newBuilder() + + if (BuildConfig.DEBUG) { + val loggingInterceptor = HttpLoggingInterceptor().apply { + level = HttpLoggingInterceptor.Level.BASIC + } + + httpClient.addInterceptor(loggingInterceptor) + } + + return httpClient.build() + } } diff --git a/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt b/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt index b95fd7514f..a16d2dc885 100644 --- a/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt @@ -11,15 +11,18 @@ import coil.ImageLoader import coil.disk.DiskCache import coil.request.CachePolicy import coil.request.ImageRequest +import com.github.libretube.BuildConfig import com.github.libretube.api.CronetHelper import com.github.libretube.constants.PreferenceKeys import com.github.libretube.extensions.toAndroidUri import com.github.libretube.extensions.toAndroidUriOrNull import com.github.libretube.util.DataSaverMode -import java.io.File -import java.nio.file.Path import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import java.io.File +import java.nio.file.Path object ImageHelper { lateinit var imageLoader: ImageLoader @@ -32,9 +35,22 @@ object ImageHelper { fun initializeImageLoader(context: Context) { val maxCacheSize = PreferenceHelper.getString(PreferenceKeys.MAX_IMAGE_CACHE, "128") + val httpClient = OkHttpClient().newBuilder() + + if (BuildConfig.DEBUG) { + val loggingInterceptor = HttpLoggingInterceptor().apply { + level = HttpLoggingInterceptor.Level.BASIC + } + + httpClient.addInterceptor(loggingInterceptor) + } + imageLoader = ImageLoader.Builder(context) .callFactory(CronetHelper.callFactory) .crossfade(true) + .okHttpClient { + httpClient.build() + } .apply { if (maxCacheSize.isEmpty()) { diskCachePolicy(CachePolicy.DISABLED) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 501c4c0810..990ef6c77b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,6 +6,7 @@ kotlin = "1.9.23" ksp = "1.9.23-1.0.19" lifecycle = "2.7.0" constraintlayout = "2.1.4" +loggingInterceptor = "4.12.0" material = "1.11.0" navigation = "2.7.7" preference = "1.2.1" @@ -43,6 +44,7 @@ androidx-fragment = { group = "androidx.fragment", name = "fragment-ktx", versio gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" } kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" } +logging-interceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "loggingInterceptor" } material = { group = "com.google.android.material", name = "material", version.ref = "material" } androidx-navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation" } androidx-navigation-ui = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation" }