mirror of
https://github.com/kalarina-bit/Fogwave.git
synced 2026-11-22 08:06:09 +00:00
Decode station logos directly at full size
This commit is contained in:
@@ -1,17 +1,24 @@
|
|||||||
package cc.skysparkle.fogwave.ui.components
|
package cc.skysparkle.fogwave.ui.components
|
||||||
|
|
||||||
|
import android.content.res.Resources
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import androidx.annotation.DrawableRes
|
||||||
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.ReadOnlyComposable
|
import androidx.compose.runtime.ReadOnlyComposable
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.produceState
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.ImageBitmap
|
||||||
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import cc.skysparkle.fogwave.data.model.RadioStation
|
import cc.skysparkle.fogwave.data.model.RadioStation
|
||||||
import cc.skysparkle.fogwave.ui.theme.LocalGeoColors
|
import cc.skysparkle.fogwave.ui.theme.LocalGeoColors
|
||||||
import coil.compose.AsyncImage
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import coil.request.ImageRequest
|
import kotlinx.coroutines.Dispatchers
|
||||||
import coil.size.Size
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
private val LightLogoBackdrop = Color(0xFFECE8F1)
|
private val LightLogoBackdrop = Color(0xFFECE8F1)
|
||||||
|
|
||||||
@@ -21,34 +28,49 @@ private val LightLogoBackdrop = Color(0xFFECE8F1)
|
|||||||
fun stationTileColor(station: RadioStation): Color =
|
fun stationTileColor(station: RadioStation): Color =
|
||||||
if (station.lightBackdrop) LightLogoBackdrop else LocalGeoColors.current.surfaceContainer
|
if (station.lightBackdrop) LightLogoBackdrop else LocalGeoColors.current.surfaceContainer
|
||||||
|
|
||||||
// Decoded off the main thread and kept in Coil's memory cache, so no logo is decoded during a frame.
|
// Station logos are small bundled files, so each is decoded once, at its full size and off the main
|
||||||
@Composable
|
// thread, and the same bitmap is shared by the station list, the mini player and the now-playing sheet.
|
||||||
fun StationLogoImage(station: RadioStation, modifier: Modifier = Modifier) {
|
// They are read straight from the resource with density scaling off: going through an image loader's
|
||||||
AsyncImage(
|
// resource fetcher let release builds decode them a few pixels wide, which showed as a smear.
|
||||||
model = station.iconResId,
|
private object StationLogoCache {
|
||||||
contentDescription = null,
|
private val bitmaps = ConcurrentHashMap<Int, ImageBitmap>()
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = modifier
|
fun get(@DrawableRes resId: Int): ImageBitmap? = bitmaps[resId]
|
||||||
)
|
|
||||||
|
fun load(resources: Resources, @DrawableRes resId: Int): ImageBitmap? {
|
||||||
|
bitmaps[resId]?.let { return it }
|
||||||
|
val options = BitmapFactory.Options().apply { inScaled = false }
|
||||||
|
val bitmap = resources.openRawResource(resId).use { input ->
|
||||||
|
BitmapFactory.decodeStream(input, null, options)
|
||||||
|
} ?: return null
|
||||||
|
return bitmap.asImageBitmap().also { bitmaps[resId] = it }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Player artwork is decoded at full size under its own cache key: the mini player and the
|
|
||||||
// now-playing sheet share one decode, which the station list never evicts, and show it without a fade.
|
|
||||||
@Composable
|
@Composable
|
||||||
fun StationArtwork(station: RadioStation, modifier: Modifier = Modifier) {
|
private fun rememberStationLogo(@DrawableRes resId: Int): ImageBitmap? {
|
||||||
val context = LocalContext.current
|
val resources = LocalContext.current.resources
|
||||||
val request = remember(context, station.iconResId) {
|
val logo by produceState(StationLogoCache.get(resId), resources, resId) {
|
||||||
ImageRequest.Builder(context)
|
value = StationLogoCache.get(resId)
|
||||||
.data(station.iconResId)
|
?: withContext(Dispatchers.IO) { StationLogoCache.load(resources, resId) }
|
||||||
.size(Size.ORIGINAL)
|
|
||||||
.memoryCacheKey("artwork_${station.iconResId}")
|
|
||||||
.crossfade(false)
|
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
AsyncImage(
|
return logo
|
||||||
model = request,
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun StationLogoImage(station: RadioStation, modifier: Modifier = Modifier) {
|
||||||
|
val logo = rememberStationLogo(station.iconResId)
|
||||||
|
if (logo != null) {
|
||||||
|
Image(
|
||||||
|
bitmap = logo,
|
||||||
contentDescription = null,
|
contentDescription = null,
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun StationArtwork(station: RadioStation, modifier: Modifier = Modifier) {
|
||||||
|
StationLogoImage(station, modifier)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user