diff --git a/android/app/src/main/java/tv/orbit/classic/CustomChannels.kt b/android/app/src/main/java/tv/orbit/classic/CustomChannels.kt index bb558a3e..05425892 100644 --- a/android/app/src/main/java/tv/orbit/classic/CustomChannels.kt +++ b/android/app/src/main/java/tv/orbit/classic/CustomChannels.kt @@ -105,11 +105,13 @@ internal object CustomChannels { /** Roughly how long a first plan takes, from what it has to read: each series' episodes, a quick look for each film. */ fun planMinutes(series: Int, films: Int) = ((series * 9 + films) / 60.0).let { kotlin.math.ceil(it).toInt() }.coerceAtLeast(1) + @Volatile private var cachedWeek = -1L @Volatile private var cached: Pair, List> = emptyList() to emptyList() /** Every channel of your own as the planner sees it. */ val programmings: List get() { - val now = list - if (cached.first !== now) cached = now to now.map(::programmingOf) + val now = list; val week = rotationWeek() + // Remade when the list changes, and each week for the evening's rotation. + if (cached.first !== now || cachedWeek != week) { cached = now to now.map(::programmingOf); cachedWeek = week } return cached.second } } @@ -125,7 +127,7 @@ internal fun programmingOf(channel: CustomChannel): ChannelProgramming = Channel // You chose these titles yourself, so they play in the sound they have: Sakura's anime is mostly Japanese with subtitles, // and asking for English sound left nearly all of it out (7 November 2026). englishAudio = false, - dayParts = when (channel.style) { PlayStyle.Blocks -> timetableParts(channel.shows); PlayStyle.Shaped -> shapedParts(channel.slots); else -> emptyList() }, + dayParts = when (channel.style) { PlayStyle.Blocks -> timetableParts(channel.shows); PlayStyle.Shaped -> shapedParts(channel.slots, eveningPage = rotationWeek()); else -> emptyList() }, storyGrouping = channel.style == PlayStyle.Standard || channel.style == PlayStyle.Shaped, shorts = if (channel.style == PlayStyle.Standard || channel.style == PlayStyle.Shaped) YoursShorts else null, tierWeights = if (channel.style == PlayStyle.Shaped && channel.core.isNotEmpty()) shapedWeights(channel.core) else null, diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt b/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt index 4d7d7e82..96df5efe 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt @@ -160,6 +160,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea var guideCategory by ui::guideCategory val liveHandoff = remember { LiveHandoff() } val scheduleStore = remember(workspace) { workspace.schedules } + remember(scheduleStore) { runCatching { scheduleStore.retryFailedOnce() }; true } // Your own channels (Create your own) are this server's, read before anything plans or restores. remember(workspace) { CustomChannels.load(workspace.scheduleDir); true } val scheduler = remember(workspace) { ChannelScheduler(scheduleStore, copies = { if (catalogue.complete()) catalogue.idsByImdb(it) else null }, original = workspace.original) } diff --git a/android/app/src/main/java/tv/orbit/classic/ScheduleStore.kt b/android/app/src/main/java/tv/orbit/classic/ScheduleStore.kt index 2fa74715..3631fea4 100644 --- a/android/app/src/main/java/tv/orbit/classic/ScheduleStore.kt +++ b/android/app/src/main/java/tv/orbit/classic/ScheduleStore.kt @@ -168,6 +168,14 @@ internal class ScheduleStore(private val directory: File) { /** Files that wouldn't play on this box, across every channel. Never planned again. */ fun saveFailed(userId: String, ids: Set) = write(file("failed", 0), root(userId, 0).put("items", JSONArray(ids.toList()))) + /** + * Once, after the player learnt zlib subtitles (0.108.96, Paul, 7 November 2026: The Twilight Zone S1 E3), the files marked + * unplayable get one fresh try: the list is set aside, and any that still fail go straight back on it. + */ + fun retryFailedOnce() { + val list = file("failed", 0); val aside = File(directory, "failed-0.json.before-zlib") + if (!aside.exists()) { if (list.exists()) list.renameTo(aside) else aside.writeText("") } + } fun loadFailed(userId: String): Set = read(file("failed", 0), userId, 0) { root -> val items = root.getJSONArray("items") (0 until items.length()).map { validId(items.getString(it)) }.toSet() diff --git a/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt b/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt index 20a23b00..c5aabbdb 100644 --- a/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt +++ b/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt @@ -230,9 +230,12 @@ internal fun weekCheck(series: Int, films: Int): String? = when { * never the late ones, so a late title stays late (Paul, 7 November 2026: the creator's choices and the profile's rules win). * With nothing laid out, everything plays all day. */ -internal fun shapedParts(slots: Map): List { +internal fun shapedParts(slots: Map, eveningPage: Long = 0): List { if (slots.isEmpty()) return emptyList() - val bySlot = Slot.entries.associateWith { s -> slots.filterValues { it == s }.keys.toList() } + val bySlot = Slot.entries.associateWith { s -> slots.filterValues { it == s }.keys.toList() }.toMutableMap() + // Evenings are appointments (Paul, 7 November 2026: Sakura's evening had 85 shows, each on once a week): at most + // EVENING_ROTATION shows a week, so each comes back most nights, and a different set the next week until all have had theirs. + bySlot[Slot.Evening] = eveningRotation(bySlot.getValue(Slot.Evening), eveningPage) val gentle = bySlot.getValue(Slot.Daytime) + bySlot.getValue(Slot.Evening) return Slot.entries.mapNotNull { s -> val titles = bySlot.getValue(s).ifEmpty { if (s == Slot.Late) gentle + bySlot.getValue(Slot.Late) else gentle } @@ -240,6 +243,24 @@ internal fun shapedParts(slots: Map): List { } } +/** How many shows a shaped channel's evening carries in a week. */ +internal const val EVENING_ROTATION = 12 + +/** This week's evening shows from [titles]: all of them when there are few, else the week's turn of [EVENING_ROTATION]. */ +internal fun eveningRotation(titles: List, page: Long): List { + if (titles.size <= EVENING_ROTATION) return titles + val sorted = titles.sortedBy { seriesKey(it) } + val pages = (sorted.size + EVENING_ROTATION - 1) / EVENING_ROTATION + // Spread evenly, so the last week isn't a handful of leftovers. + val per = (sorted.size + pages - 1) / pages + val k = Math.floorMod(page, pages.toLong()).toInt() + return sorted.drop(k * per).take(per) +} + +/** Which week it is for the evening's rotation: Mondays turn it over. */ +internal fun rotationWeek(nowMs: Long = System.currentTimeMillis(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): Long = + java.time.Instant.ofEpochMilli(nowMs).atZone(zone).toLocalDate().with(java.time.DayOfWeek.MONDAY).toEpochDay() / 7 + /** "About 42 hours", for the week check: how long the picks run before anything repeats. */ internal fun hoursLine(totalMs: Long): String = (totalMs / 3_600_000.0).let { h -> when { h < 1 -> "under an hour"; h < 10 -> "about %.0f hours".format(h).replace("about 1 hours", "about an hour"); else -> "about ${(h / 5).toInt() * 5} hours" } diff --git a/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt b/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt index 9521445e..5dca249d 100644 --- a/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt @@ -65,8 +65,11 @@ class SmartSetupRegressionTest { val lateBySeries = r.plan.entries.filter { it.startMs < now + week && java.time.Instant.ofEpochMilli(it.startMs).atZone(zone).hour.let { h -> h >= 22 || h < 6 } } .filter { e -> r.rows.first { it.id == e.seriesId }.type == "Series" }.groupingBy { it.seriesId }.eachCount() assertTrue(lateBySeries.toString(), lateBySeries.values.max() <= 15) - // Every title gets airtime in the week. - assertEquals(r.rows.size, r.plan.entries.filter { it.startMs < now + week }.map { it.seriesId }.toSet().size) + // Every title gets airtime in the week, but for the evening's: those take turns, at most twelve a week (7 November 2026). + val evening = r.rows.filter { r.shape.slots[it.id] == Slot.Evening } + val aired = r.plan.entries.filter { it.startMs < now + week }.map { it.seriesId }.toSet() + assertTrue(r.rows.filter { it !in evening }.all { it.id in aired }) + assertTrue(evening.count { it.id in aired } <= EVENING_ROTATION) } @Test fun ratingsFromEveryCountryAndUnknownIsNeverKids() { diff --git a/android/app/src/test/java/tv/orbit/classic/SmartSetupTest.kt b/android/app/src/test/java/tv/orbit/classic/SmartSetupTest.kt index 684a0284..26acf794 100644 --- a/android/app/src/test/java/tv/orbit/classic/SmartSetupTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/SmartSetupTest.kt @@ -123,4 +123,21 @@ class SmartSetupTest { assertTrue(filmNumbers.any { it > 75 } && filmNumbers.any { it <= 25 }) assertEquals(400, order.size) } + + // Sakura's evening had 85 shows, each on once a week (Paul, 7 November 2026): evenings now carry a week's turn of at most + // twelve, a different set each week until every show has had its evenings. + @Test fun eveningsRotateAFewShowsAtATime() { + val shows = (1..85).map { "Show %02d".format(it) } + assertEquals(listOf("A", "B"), eveningRotation(listOf("A", "B"), 5)) + val weeks = (0L until 8L).map { eveningRotation(shows, it) } + assertTrue(weeks.all { it.size in 1..EVENING_ROTATION }) + assertEquals(shows.toSet(), weeks.flatten().toSet()) + assertEquals(85, weeks.flatten().size) + assertEquals(weeks[0], eveningRotation(shows, 8)) + val parts = shapedParts(shows.associateWith { Slot.Evening } + ("Bluey" to Slot.Daytime), eveningPage = 3) + assertEquals(weeks[3], parts.first { it.fromHour == Slot.Evening.fromHour }.titles) + val monday = java.time.ZonedDateTime.of(2026, 11, 9, 0, 30, 0, 0, java.time.ZoneId.of("Europe/London")).toInstant().toEpochMilli() + val sunday = monday - 60 * 60_000L + assertEquals(rotationWeek(sunday, java.time.ZoneId.of("Europe/London")) + 1, rotationWeek(monday, java.time.ZoneId.of("Europe/London"))) + } }