From 25c50ae074a2c9f62b4d75024cabb819306bef3b Mon Sep 17 00:00:00 2001 From: Luca Stefani Date: Tue, 26 Nov 2024 15:14:12 +0100 Subject: [PATCH] Twelve: Add HTTP cache Change-Id: Ic8a423f6295b4dc9b121b98c3f65e2f32767a380 --- .../lineageos/twelve/datasources/SubsonicDataSource.kt | 5 +++-- .../twelve/datasources/subsonic/SubsonicClient.kt | 7 ++++++- .../org/lineageos/twelve/repositories/MediaRepository.kt | 9 ++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/lineageos/twelve/datasources/SubsonicDataSource.kt b/app/src/main/java/org/lineageos/twelve/datasources/SubsonicDataSource.kt index ea34f72a..9e22f294 100644 --- a/app/src/main/java/org/lineageos/twelve/datasources/SubsonicDataSource.kt +++ b/app/src/main/java/org/lineageos/twelve/datasources/SubsonicDataSource.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.mapLatest +import okhttp3.Cache import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import org.lineageos.twelve.R import org.lineageos.twelve.datasources.subsonic.SubsonicClient @@ -39,14 +40,14 @@ import org.lineageos.twelve.models.Thumbnail * Subsonic based data source. */ @OptIn(ExperimentalCoroutinesApi::class) -class SubsonicDataSource(arguments: Bundle) : MediaDataSource { +class SubsonicDataSource(arguments: Bundle, cache: Cache? = null) : MediaDataSource { private val server = arguments.requireArgument(ARG_SERVER) private val username = arguments.requireArgument(ARG_USERNAME) private val password = arguments.requireArgument(ARG_PASSWORD) private val useLegacyAuthentication = arguments.requireArgument(ARG_USE_LEGACY_AUTHENTICATION) private val subsonicClient = SubsonicClient( - server, username, password, "Twelve", useLegacyAuthentication + server, username, password, "Twelve", useLegacyAuthentication, cache ) private val dataSourceBaseUri = Uri.parse(server) diff --git a/app/src/main/java/org/lineageos/twelve/datasources/subsonic/SubsonicClient.kt b/app/src/main/java/org/lineageos/twelve/datasources/subsonic/SubsonicClient.kt index db4e2960..e92dbe64 100644 --- a/app/src/main/java/org/lineageos/twelve/datasources/subsonic/SubsonicClient.kt +++ b/app/src/main/java/org/lineageos/twelve/datasources/subsonic/SubsonicClient.kt @@ -7,6 +7,7 @@ package org.lineageos.twelve.datasources.subsonic import android.net.Uri import kotlinx.serialization.json.Json +import okhttp3.Cache import okhttp3.OkHttpClient import okhttp3.Request import org.lineageos.twelve.datasources.subsonic.SubsonicClient.Companion.SUBSONIC_API_VERSION @@ -26,6 +27,7 @@ import java.security.MessageDigest * @param password The password to use * @param clientName The name of the client to use for requests * @param useLegacyAuthentication Whether to use legacy authentication or not (token authentication) + * @param cache OkHttp's [Cache] */ class SubsonicClient( private val server: String, @@ -33,8 +35,11 @@ class SubsonicClient( private val password: String, private val clientName: String, private val useLegacyAuthentication: Boolean, + private val cache: Cache? = null, ) { - private val okHttpClient = OkHttpClient() + private val okHttpClient = OkHttpClient.Builder() + .cache(cache) + .build() private val serverUri = Uri.parse(server) diff --git a/app/src/main/java/org/lineageos/twelve/repositories/MediaRepository.kt b/app/src/main/java/org/lineageos/twelve/repositories/MediaRepository.kt index b542cbcb..8a8e7fab 100644 --- a/app/src/main/java/org/lineageos/twelve/repositories/MediaRepository.kt +++ b/app/src/main/java/org/lineageos/twelve/repositories/MediaRepository.kt @@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.stateIn +import okhttp3.Cache import org.lineageos.twelve.database.TwelveDatabase import org.lineageos.twelve.datasources.LocalDataSource import org.lineageos.twelve.datasources.MediaDataSource @@ -61,6 +62,12 @@ class MediaRepository( Build.MODEL, ) + /** + * HTTP cache + * 50 MB should be enough for most cases. + */ + private val cache = Cache(context.cacheDir, 50 * 1024 * 1024) + /** * All the providers. This is our single point of truth for the providers. */ @@ -80,7 +87,7 @@ class MediaRepository( ProviderType.SUBSONIC, it.id, it.name, - ) to SubsonicDataSource(arguments) + ) to SubsonicDataSource(arguments, cache) } } ) { providers -> providers.toList().flatten() }