Reduce card image transfers and list metadata payloads

This commit is contained in:
justinzeus 2026-11-06 23:54:16 +01:00
parent f4fb6e22c9
commit e340d470c1
10 changed files with 152 additions and 31 deletions

View file

@ -1,6 +1,8 @@
package org.justinvisser.wolfskin.design
import android.content.Context
import android.graphics.ImageDecoder
import android.os.Build
import android.net.Uri
import coil3.ComponentRegistry
import coil3.ImageLoader
@ -23,6 +25,8 @@ object WolfskinImages {
private var networkConfiguration: (ComponentRegistry.Builder.() -> Unit)? = null
private const val MEBIBYTE = 1024L * 1024L
private const val IMAGE_READS = 4
private const val CARD_QUALITY = "80"
private const val HERO_QUALITY = "96"
private val readPermits = Semaphore(IMAGE_READS)
/** The app supplies authenticated networking without exposing its server/session to design code. */
@ -78,32 +82,34 @@ object WolfskinImages {
private val jellyfinImagePath = Regex("/Items/[^/]+/Images/[^/]+(?:/\\d+)?", RegexOption.IGNORE_CASE)
private val tmdbSizePath = Regex("/t/p/(?:w\\d+|original)/")
private val dimensionParameters = setOf("width", "height", "maxwidth", "maxheight", "fillwidth", "fillheight")
private val imageParameters = setOf("width", "height", "maxwidth", "maxheight", "fillwidth", "fillheight", "quality", "format")
// Lossy WebP is mandatory from API 14; minSdk is 23. Newer platforms expose a decoder capability query.
private val supportsWebp by lazy { Build.VERSION.SDK_INT < 28 || ImageDecoder.isMimeTypeSupported("image/webp") }
private fun sourceForSize(source: Any, width: Int, height: Int): Any {
if (source !is String) return source
val uri = Uri.parse(source)
if (uri.scheme != "https" && uri.scheme != "http") return source
if (uri.host == "image.tmdb.org") {
val size = if (height > width) when {
width <= 500 -> "w500"
width <= 780 -> "w780"
else -> "original"
} else when {
width <= 780 -> "w780"
width <= 1280 -> "w1280"
else -> "original"
val size = when {
height >= width -> "w342"
width > WolfskinTokens.Image.wideRequestWidth -> "w1280"
else -> "w780"
}
return uri.buildUpon().path(uri.path?.replace(tmdbSizePath, "/t/p/$size/")).build().toString()
}
if (!jellyfinImagePath.matches(uri.path.orEmpty())) return source
val hero = width == WolfskinTokens.Image.heroRequestWidth && height == WolfskinTokens.Image.heroRequestHeight ||
width == WolfskinTokens.Image.heroPosterRequestWidth && height == WolfskinTokens.Image.heroPosterRequestHeight
val builder = uri.buildUpon().clearQuery()
for (name in uri.queryParameterNames) {
if (name.lowercase() !in dimensionParameters) {
if (name.lowercase() !in imageParameters) {
uri.getQueryParameters(name).forEach { value -> builder.appendQueryParameter(name, value) }
}
}
return builder.appendQueryParameter("fillWidth", width.toString())
.appendQueryParameter("fillHeight", height.toString()).build().toString()
.appendQueryParameter("fillHeight", height.toString())
.appendQueryParameter("format", if (hero || !supportsWebp) "Jpg" else "Webp")
.appendQueryParameter("quality", if (hero) HERO_QUALITY else CARD_QUALITY).build().toString()
}
}

View file

@ -220,10 +220,10 @@ object WolfskinTokens {
object Image {
const val posterRequestWidth = 256
const val posterRequestHeight = 384
const val tileRequestWidth = 416
const val tileRequestHeight = 234
const val wideRequestWidth = 560
const val wideRequestHeight = 315
const val tileRequestWidth = 400
const val tileRequestHeight = 225
const val wideRequestWidth = 544
const val wideRequestHeight = 306
const val heroRequestWidth = 1920
const val heroRequestHeight = 1080
const val heroPosterRequestWidth = 420