Smart setup: slots are a suggestion, late stays late, and an hours estimate
A channel that's nearly all one kind plays all day instead of being split. An empty slot shares the daytime and evening titles, never the late ones. Review says how many hours the picks give before anything repeats, from the library's running times, for the channel's own hours. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -187,6 +187,12 @@ internal class CatalogueStore(context: Context, private val file: File) : Catalo
|
||||
buildList { while (c.moveToNext()) add(ShapeRow(c.getString(0), c.getString(1), c.getString(2), if (c.isNull(3)) null else c.getInt(3), c.getString(4), c.getString(5).split('|').filter { it.isNotBlank() }.toSet())) }
|
||||
}
|
||||
}
|
||||
/** Films' running times by item, for Smart setup's hours estimate. Read only. */
|
||||
@Synchronized fun runTimes(ids: Collection<String>): Map<String, Long> = ids.distinct().chunked(400).flatMap { chunk ->
|
||||
readableDatabase.rawQuery("SELECT id, runtime_ms FROM items WHERE runtime_ms IS NOT NULL AND id IN (${chunk.joinToString(",") { "?" }})", chunk.toTypedArray()).use { c ->
|
||||
buildList { while (c.moveToNext()) add(c.getString(0) to c.getLong(1)) }
|
||||
}
|
||||
}.toMap()
|
||||
/** What Smart setup reads of chosen titles: kind, rating, genres and year. Read only. */
|
||||
@Synchronized fun shapeRows(ids: Collection<String>): List<ShapeRow> = ids.distinct().chunked(400).flatMap { chunk ->
|
||||
readableDatabase.rawQuery("SELECT id, type, title, year, rating, genres FROM items WHERE id IN (${chunk.joinToString(",") { "?" }})", chunk.toTypedArray()).use { c ->
|
||||
|
||||
@@ -50,7 +50,9 @@ internal class ChannelMaker(
|
||||
val facets: suspend () -> Pair<List<String>, List<String>> = { emptyList<String>() to emptyList() },
|
||||
val shapeOf: suspend (List<String>) -> List<ShapeRow> = { emptyList() },
|
||||
/** For each title, the other channels already showing it (Paul, 7 November 2026: "check conflicts with the rest of the channels"). */
|
||||
val carriedBy: (picks: Map<String, String>, number: Int?) -> Map<String, List<String>> = { _, _ -> emptyMap() })
|
||||
val carriedBy: (picks: Map<String, String>, number: Int?) -> Map<String, List<String>> = { _, _ -> emptyMap() },
|
||||
/** How long the picks run all told, in milliseconds, or null when the library can't say. */
|
||||
val runTime: suspend (List<ShapeRow>) -> Long? = { null })
|
||||
|
||||
internal enum class CreateStep(val title: String) { Start("Create a channel"), Name("Name and number"), Shows("Shows and films"), Style("How it plays"), Hours("When it's on") }
|
||||
|
||||
|
||||
@@ -544,6 +544,11 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
||||
browse = { genre, studio, ratings, type -> withContext(Dispatchers.IO) { catalogue.browseRows(genre, studio, ratings, type) } },
|
||||
facets = { withContext(Dispatchers.IO) { catalogue.facets() } },
|
||||
shapeOf = { ids -> withContext(Dispatchers.IO) { catalogue.shapeRows(ids) } },
|
||||
runTime = { rows -> withContext(Dispatchers.IO) {
|
||||
val films = rows.filter { it.type == "Movie" }.let { f -> catalogue.runTimes(f.map { it.id }).values.sum() }
|
||||
val series = rows.filter { it.type == "Series" }.take(40).map { active.seriesRunTime(it.id) }
|
||||
if (series.any { it == null }) null else films + series.sumOf { it ?: 0L }
|
||||
} },
|
||||
carriedBy = { picks, number -> titlesCarriedBy(picks, scheduledChannels.values.filter { it.number != number }) },
|
||||
create = { asked, shows ->
|
||||
// Shaped on the TV, or from the phone without a layout: the Director lays it out from the catalogue.
|
||||
|
||||
@@ -211,6 +211,16 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
|
||||
return "inspect cast=${people?.length() ?: 0} [$cast]"
|
||||
}
|
||||
|
||||
/** Smart setup's hours estimate: a series' episodes and their running time all told, read as metadata only. Null if it can't be read. */
|
||||
fun seriesRunTime(seriesId: String): Long? = runCatching {
|
||||
require(EmbyClient.validId(seriesId))
|
||||
val reply = transport.execute(EmbyRequest("GET", "/Shows/$seriesId/Episodes?UserId=$userId&StartIndex=0&Limit=3000&EnableImages=false&EnableUserData=false",
|
||||
token = currentToken(), readTimeoutMs = WHOLE_SERIES_TIMEOUT_MS))
|
||||
EmbyClient.expectSuccess(reply.status, false)
|
||||
val rows = EmbyClient.parse(reply.body).optJSONArray("Items") ?: return null
|
||||
(0 until rows.length()).sumOf { (rows.optJSONObject(it)?.optLong("RunTimeTicks", 0) ?: 0L) / 10_000 }
|
||||
}.getOrNull()
|
||||
|
||||
fun inspectSeries(title: String, failed: Set<String> = emptySet()): List<String> {
|
||||
val match = searchSeries(transport, userId, ::currentToken, title).candidates.filter { seriesKey(it.title) == seriesKey(title) }
|
||||
if (match.isEmpty()) return listOf("inspect \"$title\": no series of that name")
|
||||
|
||||
@@ -321,8 +321,13 @@ internal object PhoneLink {
|
||||
val rows = runBlocking { h.maker.shapeOf(ids) }
|
||||
val shape = shapeChannel(rows)
|
||||
val series = rows.count { it.type == "Series" }; val films = rows.count { it.type == "Movie" }
|
||||
// The real running time when the library can say, or the rough check from how many titles there are.
|
||||
val hoursPerDay = runCatching { JSONObject(r.body).optJSONObject("hours")?.let { h -> ((h.getInt("to").let { if (it == 0) 24 else it } - h.getInt("from") + 24) % 24).takeIf { it > 0 } } }.getOrNull() ?: 24
|
||||
val total = runCatching { runBlocking { h.maker.runTime(rows) } }.getOrNull()
|
||||
json(out, 200, JSONObject().put("slots", JSONObject(shape.slots.mapValues { it.value.name })).put("core", JSONArray(shape.core.toList()))
|
||||
.put("week", weekCheck(series, films) ?: JSONObject.NULL))
|
||||
.put("mixed", shape.slots.isEmpty() && rows.size >= 3)
|
||||
.put("week", total?.takeIf { it > 0 }?.let { weekLine(it, hoursPerDay) } ?: weekCheck(series, films) ?: JSONObject.NULL)
|
||||
.put("hours", total?.let { it / 3_600_000.0 } ?: JSONObject.NULL))
|
||||
}
|
||||
"POST /api/clashes" -> {
|
||||
val o = runCatching { JSONObject(r.body) }.getOrNull() ?: return json(out, 400, JSONObject())
|
||||
|
||||
@@ -31,14 +31,34 @@ internal fun slotFor(row: ShapeRow): Slot = when {
|
||||
else -> Slot.Evening
|
||||
}
|
||||
|
||||
/** The whole layout: every title's slot, and up to three core titles, the newest of the evening's (or of all, with none there). */
|
||||
/**
|
||||
* The whole layout: every title's slot, and up to three core titles, the newest of the evening's (or of all, with none there).
|
||||
* The slots are a starting suggestion, not a rule (Paul, 7 November 2026): a channel that's nearly all one kind, an
|
||||
* all-horror channel or a kids' one, isn't split up, and everything plays all day.
|
||||
*/
|
||||
internal fun shapeChannel(rows: List<ShapeRow>): Shape {
|
||||
val slots = rows.associate { it.id to slotFor(it) }
|
||||
val suggested = rows.associate { it.id to slotFor(it) }
|
||||
val largest = suggested.values.groupingBy { it }.eachCount().maxOfOrNull { it.value } ?: 0
|
||||
val slots = if (rows.size >= 3 && largest >= rows.size * .75) emptyMap() else suggested
|
||||
val evening = rows.filter { slots[it.id] == Slot.Evening }.ifEmpty { rows }
|
||||
return Shape(slots, evening.sortedByDescending { it.year ?: 0 }.take(3).map { it.id }.toSet())
|
||||
}
|
||||
|
||||
/** How the week looks: a gentle warning when the picks are few enough that episodes would repeat within days. */
|
||||
/**
|
||||
* How the week looks, from the picks' real running time when it's known (Paul, 7 November 2026: "your selection provides
|
||||
* roughly 42 hours"): how long before anything repeats, and a gentle warning when that's under a week of [hoursPerDay].
|
||||
*/
|
||||
internal fun weekLine(totalMs: Long, hoursPerDay: Int = 24): String {
|
||||
val days = totalMs / 3_600_000.0 / hoursPerDay
|
||||
val hours = hoursLine(totalMs)
|
||||
return when {
|
||||
days >= 7 -> "Your picks give $hours of programmes: more than a week before anything repeats."
|
||||
days >= 2 -> "Your picks give $hours of programmes, so repeats start after about ${days.toInt()} days. A few more would fill the week."
|
||||
else -> "Your picks give $hours of programmes, so you'd be watching repeats ${if (days < 1) "later today" else "by tomorrow"}. A few more would fill the week."
|
||||
}
|
||||
}
|
||||
|
||||
/** How the week looks without running times: a gentle warning when the picks are few enough that episodes would repeat within days. */
|
||||
internal fun weekCheck(series: Int, films: Int): String? = when {
|
||||
series + films == 0 -> null
|
||||
series == 0 && films < 21 -> "$films films is under three a day, so they'd repeat within the week. Add a few more, or keep it as it is."
|
||||
@@ -46,9 +66,24 @@ internal fun weekCheck(series: Int, films: Int): String? = when {
|
||||
else -> null
|
||||
}
|
||||
|
||||
/** A layout as the planner reads it: each slot's titles for its hours, and the core titles weighted up. */
|
||||
internal fun shapedParts(slots: Map<String, Slot>): List<DayPart> = Slot.entries.mapNotNull { s ->
|
||||
slots.filterValues { it == s }.keys.toList().takeIf { it.isNotEmpty() }?.let { DayPart(s.fromHour, s.toHour, it) }
|
||||
/**
|
||||
* A layout as the planner reads it: each slot's titles for its hours. An empty slot shares the daytime and evening titles,
|
||||
* 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<String, Slot>): List<DayPart> {
|
||||
if (slots.isEmpty()) return emptyList()
|
||||
val bySlot = Slot.entries.associateWith { s -> slots.filterValues { it == s }.keys.toList() }
|
||||
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 }
|
||||
titles.takeIf { it.isNotEmpty() }?.let { DayPart(s.fromHour, s.toHour, it) }
|
||||
}
|
||||
}
|
||||
|
||||
/** "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" }
|
||||
}
|
||||
internal fun shapedWeights(core: Set<String>): Map<String, Double> = core.associate { seriesKey(it) to TIER_CORE }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user