Quicker open; Tonight on Orbit 2 with a picture (from release 0.108.55)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-06 13:11:40 +00:00
co-authored by Claude Opus 5.5
parent 9bb5f43070
commit f8c3191600
7 changed files with 25 additions and 8 deletions
@@ -17,9 +17,12 @@ internal fun catalogueIdKey(type: String, providerIds: String): String? {
return "$type|$match"
}
// Compiled once: this runs for every title in the catalogue.
private val TitleSpaces = Regex(" +")
/** Case, punctuation and a leading article don't make a different title. The year keeps remakes apart. */
internal fun catalogueTitleKey(type: String, title: String, year: Int?): String {
val letters = title.lowercase(Locale.ROOT).map { if (it.isLetterOrDigit()) it else ' ' }.joinToString("").trim().replace(Regex(" +"), " ")
val letters = title.lowercase(Locale.ROOT).map { if (it.isLetterOrDigit()) it else ' ' }.joinToString("").trim().replace(TitleSpaces, " ")
return "$type|${letters.removePrefix("the ")}|${year ?: "?"}"
}
@@ -263,13 +263,13 @@ internal fun liveTickets(tickets: Map<Long, Int>, nowMs: Long) = tickets.filterK
private val Marquee = FontFamily(Font(R.font.anton_regular))
@Composable
internal fun rememberFilmPicture(session: EmbySampleSession, itemId: String?): Bitmap? {
internal fun rememberFilmPicture(session: EmbySampleSession, itemId: String?, seriesId: String? = null): Bitmap? {
val context = LocalContext.current
val picture by produceState<Bitmap?>(null, itemId) {
value = null
if (itemId != null) value = withContext(Dispatchers.IO) {
runCatching {
loadArtwork(ArtworkCache(Workspaces.current(context).artworkDir), itemId, null, session::fetchArtwork)?.let { BitmapFactory.decodeByteArray(it, 0, it.size) }
loadArtwork(ArtworkCache(Workspaces.current(context).artworkDir), itemId, seriesId, session::fetchArtwork)?.let { BitmapFactory.decodeByteArray(it, 0, it.size) }
}.getOrNull()
}
}
@@ -196,7 +196,13 @@ internal class EmbyClient(private val transport: EmbyTransport = HttpsEmbyTransp
return EmbySampleSession(transport, token, identity.userId, saved.sample).also { it.restoreRemembered(saved) }
}
companion object {
internal fun validId(value: String) = Regex("[A-Za-z0-9_-]{1,128}").matches(value)
// A plain loop, not a Regex built on every call: it's checked thousands of times as Orbit opens, and building the
// pattern each time cost seconds on the box (6 November 2026).
internal fun validId(value: String): Boolean {
if (value.isEmpty() || value.length > 128) return false
for (c in value) if (!(c in 'A'..'Z' || c in 'a'..'z' || c in '0'..'9' || c == '_' || c == '-')) return false
return true
}
internal fun parse(body: String): JSONObject = try { JSONObject(body) }
catch (_: Exception) { throw EmbyCheckException("The provider returned a response this version cannot read.") }
internal fun clean(value: String, limit: Int) = value.filter { !it.isISOControl() }.take(limit)
@@ -20,7 +20,8 @@ private const val ARTWORK_KEEP_FILES = 150
/** Where a programme's picture may be: its show's wide artwork first, as that suits a full screen, then its own. */
internal fun artworkPaths(itemId: String, seriesId: String?): List<String> =
(listOfNotNull(seriesId, itemId).distinct().filter { EmbyClient.validId(it) }.map { "/Items/$it/Images/Backdrop?maxWidth=1280&quality=80" } +
listOf(itemId).filter { EmbyClient.validId(it) }.map { "/Items/$it/Images/Primary?maxWidth=1280&quality=80" })
// Then the episode's own still, and last the show's poster: a promo or guide card is never left empty if the show has any picture.
listOfNotNull(itemId, seriesId).distinct().filter { EmbyClient.validId(it) }.map { "/Items/$it/Images/Primary?maxWidth=1280&quality=80" })
/** Keeps artwork on the box between visits, within a fixed number of files, so a show's picture is only fetched once. */
internal class ArtworkCache(private val directory: File) {
@@ -8,6 +8,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
@@ -65,7 +66,10 @@ internal fun promoHeadline(item: WhatsOnItem, nowMs: Long, zone: ZoneId = ZoneId
val hour = Instant.ofEpochMilli(item.startMs).atZone(zone).hour
val evening = day == today || (day == today.plusDays(1) && hour < 6)
val whenWord = if (evening) (if (hour in 6..16 && day == today) "LATER" else "TONIGHT") else "TOMORROW"
val where = if (item.channel == DRIVE_IN_NUMBER) "AT THE DRIVE-IN" else "ON " + (ChannelBrands.forNumber(item.channel)?.displayName ?: "ORBIT").removePrefix("Orbit ").uppercase()
// "On Gold", "On Paradox", but "On Orbit 2" (Paul, 6 November 2026: "Tonight on 2" didn't look right).
val name = ChannelBrands.forNumber(item.channel)?.displayName ?: "Orbit"
val short = name.removePrefix("Orbit ")
val where = if (item.channel == DRIVE_IN_NUMBER) "AT THE DRIVE-IN" else "ON " + (if (short.length <= 3 || short.all { it.isDigit() }) name else short).uppercase()
return "$whenWord $where"
}
@@ -77,7 +81,7 @@ internal fun PromoOverlay(session: EmbySampleSession, item: WhatsOnItem) {
val castOn = remember { settings.castOn }
var t by remember { mutableLongStateOf(if (calm) 5_000L else 0L) }
LaunchedEffect(calm) { if (!calm) { val start = withFrameMillis { it }; while (true) withFrameMillis { t = it - start } } }
val picture = rememberFilmPicture(session, item.entry.media.item.id)
val picture = rememberFilmPicture(session, item.entry.media.item.id, item.entry.seriesId)
val now = remember { System.currentTimeMillis() }
Box(Modifier.fillMaxSize().background(Navy)) {
StarrySky()
@@ -86,6 +90,8 @@ internal fun PromoOverlay(session: EmbySampleSession, item: WhatsOnItem) {
Text(promoHeadline(item, now), color = Yellow, fontFamily = Anton, fontSize = 34.sp)
Box(Modifier.width(500.dp).height(190.dp).clip(RoundedCornerShape(12.dp)).background(Panel)) {
if (picture != null) Image(picture.asImageBitmap(), null, Modifier.fillMaxSize(), contentScale = ContentScale.Crop)
// No picture at all: the channel's logo, large and soft, rather than an empty panel.
else ChannelBrands.forNumber(item.channel)?.let { Image(painterResource(it.logo), null, Modifier.align(Alignment.Center).width(260.dp).height(80.dp).alpha(.35f)) }
Box(Modifier.fillMaxSize().background(Color.Black.copy(alpha = .3f)))
Text(item.title.uppercase(), Modifier.align(Alignment.BottomStart).padding(14.dp), color = Color.White, fontFamily = Anton, fontSize = 26.sp, maxLines = 1, overflow = TextOverflow.Ellipsis)
}
@@ -12,7 +12,7 @@ class ProgrammeArtworkTest {
private val png = byteArrayOf(1, 2, 3)
@Test fun theShowsWideArtworkIsTriedFirst() {
assertEquals(listOf("/Items/show/Images/Backdrop?maxWidth=1280&quality=80", "/Items/ep/Images/Backdrop?maxWidth=1280&quality=80", "/Items/ep/Images/Primary?maxWidth=1280&quality=80"), artworkPaths("ep", "show"))
assertEquals(listOf("/Items/show/Images/Backdrop?maxWidth=1280&quality=80", "/Items/ep/Images/Backdrop?maxWidth=1280&quality=80", "/Items/ep/Images/Primary?maxWidth=1280&quality=80", "/Items/show/Images/Primary?maxWidth=1280&quality=80"), artworkPaths("ep", "show"))
}
@Test fun aFilmOnlyAsksForItsOwnArtwork() {
@@ -32,6 +32,7 @@ class PromosTest {
val now = at(6, 18)
assertEquals("TONIGHT AT THE DRIVE-IN", promoHeadline(item(DRIVE_IN_NUMBER, at(6, 20)), now, london))
assertEquals("TONIGHT ON ACTION", promoHeadline(item(302, at(6, 21)), now, london))
assertEquals("TONIGHT ON ORBIT 2", promoHeadline(item(102, at(6, 21)), now, london)) // not "on 2"
assertEquals("TONIGHT ON ACTION", promoHeadline(item(302, at(7, 1)), now, london))
assertEquals("TOMORROW ON ACTION", promoHeadline(item(302, at(7, 20)), now, london))
assertEquals("LATER ON ACTION", promoHeadline(item(302, at(6, 15)), at(6, 12), london))