Create a channel: the clash check

On Review, the phone lists picks your other channels already show, with the
channels, and a Take out for each. A title counts by its item or its name, as a
library can hold more than one copy of a show.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-07 08:14:29 +00:00
co-authored by Claude Opus 5.5
parent db88faaf77
commit b456b4fba4
9 changed files with 69 additions and 8 deletions
@@ -48,7 +48,9 @@ internal class ChannelMaker(
/** Smart setup: browse the library, its genres and networks, and how the Director would lay chosen titles out. */
val browse: suspend (genre: String?, studio: String?, ratings: Set<String>, type: String?) -> List<ShapeRow> = { _, _, _, _ -> emptyList() },
val facets: suspend () -> Pair<List<String>, List<String>> = { emptyList<String>() to emptyList() },
val shapeOf: suspend (List<String>) -> List<ShapeRow> = { 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() })
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") }
@@ -62,9 +62,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
}
/** The sign-in headers for this kind of server. */
internal fun authHeaders(token: String?): Map<String, String> = when (kind) {
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.82\"") +
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.83\"") +
(token?.let { mapOf("X-Emby-Token" to it) } ?: emptyMap())
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.82\"" +
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.83\"" +
(token?.let { ", Token=\"$it\"" } ?: ""))
}
/** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */
@@ -544,6 +544,7 @@ 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) } },
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.
val channel = if (asked.style == PlayStyle.Shaped && asked.slots.isEmpty()) withContext(Dispatchers.IO) {
@@ -324,6 +324,13 @@ internal object PhoneLink {
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))
}
"POST /api/clashes" -> {
val o = runCatching { JSONObject(r.body) }.getOrNull() ?: return json(out, 400, JSONObject())
val picks = runCatching { o.getJSONArray("shows").let { a -> (0 until minOf(a.length(), 200)).map { a.getJSONObject(it) }
.filter { EmbyClient.validId(it.optString("id")) }.associate { it.getString("id") to it.optString("title").take(160) } } }.getOrDefault(emptyMap())
val carried = runBlocking { withContext(mainDispatcher) { h.maker.carriedBy(picks, o.optInt("number").takeIf { it > 0 }) } }
json(out, 200, JSONObject().put("clashes", JSONObject(carried.mapValues { JSONArray(it.value) })))
}
"POST /api/channels" -> {
if (!OrbitCompanion.grownUp) return json(out, 403, JSONObject().put("error", "Kids is watching. Switch to Adult to make channels."))
val o = runCatching { JSONObject(r.body) }.getOrNull() ?: return json(out, 400, JSONObject().put("error", "Unreadable"))
@@ -39,7 +39,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.108.82")
next.setRequestProperty("User-Agent", "OrbitTV/0.108.83")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
@@ -51,3 +51,24 @@ internal fun shapedParts(slots: Map<String, Slot>): List<DayPart> = Slot.entries
slots.filterValues { it == s }.keys.toList().takeIf { it.isNotEmpty() }?.let { DayPart(s.fromHour, s.toHour, it) }
}
internal fun shapedWeights(core: Set<String>): Map<String, Double> = core.associate { seriesKey(it) to TIER_CORE }
/**
* Which of the [picks] (item id to title) the [channels] already show, by their plans: id to channel names. A title counts by
* its item or by its name, since a library can hold more than one copy of a show. Orbit never airs a shared title on two
* channels at once, so this is for the viewer to know, not a problem to fix.
*/
internal fun titlesCarriedBy(picks: Map<String, String>, channels: Collection<ScheduledChannel>): Map<String, List<String>> {
if (picks.isEmpty()) return emptyMap()
val byKey = picks.entries.groupBy({ seriesKey(it.value) }, { it.key })
val out = LinkedHashMap<String, MutableList<String>>()
channels.sortedBy { it.number }.forEach { channel ->
val label = "${channel.number} ${ChannelBrands.displayName(channel)}"
val hit = HashSet<String>()
channel.plan.entries.forEach { e ->
if (e.seriesId in picks) hit += e.seriesId
byKey[seriesKey(guideTitleParts(e.media.item.title).first)]?.let(hit::addAll)
}
hit.forEach { out.getOrPut(it) { mutableListOf() } += label }
}
return out
}