Skip to content

Commit

Permalink
Twelve: Add HTTP cache
Browse files Browse the repository at this point in the history
Change-Id: Ic8a423f6295b4dc9b121b98c3f65e2f32767a380
  • Loading branch information
luca020400 authored and SebaUbuntu committed Nov 27, 2024
1 parent 370814b commit 25c50ae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,15 +27,19 @@ 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,
private val username: String,
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand All @@ -80,7 +87,7 @@ class MediaRepository(
ProviderType.SUBSONIC,
it.id,
it.name,
) to SubsonicDataSource(arguments)
) to SubsonicDataSource(arguments, cache)
}
}
) { providers -> providers.toList().flatten() }
Expand Down

0 comments on commit 25c50ae

Please sign in to comment.