Multiroom: copy the other room's schedules, or make this room's own (from release 0.108.53)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-06 12:29:18 +00:00
co-authored by Claude Opus 5.5
parent c5a209809e
commit 4989e71f16
4 changed files with 50 additions and 11 deletions
@@ -234,7 +234,7 @@ private fun ChannelsStep(onNext: () -> Unit) {
LaunchedEffect(RoomArrival.bundle) {
val arrived = RoomArrival.bundle ?: return@LaunchedEffect
if (Onboarding.tour) return@LaunchedEffect
val lineup = withContext(Dispatchers.IO) { runCatching { RoomBundle.applyToWorkspace(Workspaces.current(context).scheduleDir, arrived, RoomArrival.parts) }.getOrDefault(emptyList()) }
val lineup = withContext(Dispatchers.IO) { runCatching { RoomBundle.applyToWorkspace(Workspaces.current(context).scheduleDir, arrived, RoomArrival.parts, RoomArrival.sameSchedules) }.getOrDefault(emptyList()) }
RoomArrival.done()
if (lineup.isNotEmpty()) ChannelSetup.begin(SmartChannels.map { it.number }.filter { it in lineup })
}
@@ -74,6 +74,8 @@ private val LookKeys = listOf("look", "subtitles_on", "channel_logo", "idents",
private val ArcadeKeys = listOf("snake_best", "block_drop_best", "brick_break_best", "pairs_best", "patience_wins", "orb_man_best", "blast_best", "skydoku_solved", "snake_music")
private val DirectorFiles = listOf("director-decisions-0.json", "director-learning-0.json", "event-ideas-history-0.json")
private const val BUNDLE_LIMIT = 12 * 1024 * 1024
/** The files that make a room's schedules: plans, progress, the Drive-In's bookings and tickets, and what couldn't play. */
private val ScheduleFiles = Regex("(schedule|progress|booked|tickets|failed)-\\d+\\.json")
/** What one box sends another: plain settings and small files, read from the open workspace. Never a password or token. */
internal object RoomBundle {
@@ -114,6 +116,9 @@ internal object RoomBundle {
val pools = files(dir, lineup.map { "episodes-$it.json" })
return JSONObject().put("version", 1).put("room", room).put("server", workspace.origin)
.put(RoomPart.Channels.name, JSONObject().put("lineup", JSONArray(lineup)).put("files", files(dir, choices + "yours.json")).put("pools", gzip(pools.toString())))
// Same schedules as this room, if the new one chooses: the plans, where each channel is up to, and the Drive-In's
// bookings and tickets, copied once, so the same programme is on in both rooms.
.put("schedules", gzip(files(dir, dir.list().orEmpty().filter { ScheduleFiles.matches(it) }.sorted()).toString()))
.put(RoomPart.Family.name, JSONObject().put("profiles", prefsJson(context, "orbit_profiles", null)).put("lock", prefsJson(context, "orbit_lock", listOf("after_dark_salt", "after_dark_pin"))))
.put(RoomPart.Look.name, prefsJson(context, "orbit_settings", LookKeys))
.put(RoomPart.Director.name, files(dir, DirectorFiles))
@@ -132,7 +137,7 @@ internal object RoomBundle {
}
/** What belongs to a server's workspace, once this box has signed in to it. Returns the channels to set up. */
fun applyToWorkspace(dir: File, bundle: JSONObject, parts: Set<RoomPart>): List<Int> {
fun applyToWorkspace(dir: File, bundle: JSONObject, parts: Set<RoomPart>, sameSchedules: Boolean = false): List<Int> {
dir.mkdirs()
fun write(files: JSONObject?) = files?.keys()?.forEach { n ->
if (Regex("(choices-\\d+|episodes-\\d+|yours|director-decisions-0|director-learning-0|event-ideas-history-0)\\.json").matches(n) && !File(dir, n).exists())
@@ -145,6 +150,11 @@ internal object RoomBundle {
lineup = c.optJSONArray("lineup")?.let { a -> (0 until minOf(a.length(), 200)).map { a.getInt(it) } }.orEmpty()
}
if (RoomPart.Director in parts) write(bundle.optJSONObject(RoomPart.Director.name))
// Only with the channels: a schedule is for a channel that's on. Never over a schedule this box already has.
if (sameSchedules && RoomPart.Channels in parts) bundle.optString("schedules").takeIf { it.isNotBlank() }?.let { packed ->
runCatching { JSONObject(gunzip(packed)).let { files -> files.keys().forEach { n ->
if (ScheduleFiles.matches(n) && !File(dir, n).exists()) File(dir, n).writeText(files.getString(n)) } } }
}
return lineup
}
@@ -6,6 +6,8 @@ import android.net.nsd.NsdServiceInfo
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
@@ -159,9 +161,11 @@ internal object RoomArrival {
private set
var parts by mutableStateOf<Set<RoomPart>>(emptySet())
private set
var sameSchedules by mutableStateOf(false)
private set
val server get() = bundle?.optString("server")?.takeIf { it.isNotBlank() }
fun keep(copy: JSONObject, chosen: Set<RoomPart>) { bundle = copy; parts = chosen }
fun done() { bundle = null; parts = emptySet() }
fun keep(copy: JSONObject, chosen: Set<RoomPart>, schedules: Boolean) { bundle = copy; parts = chosen; sameSchedules = schedules }
fun done() { bundle = null; parts = emptySet(); sameSchedules = false }
}
/**
@@ -243,19 +247,22 @@ private enum class JoinPhase { Find, Code, Waiting, What, Name }
* wait for this box to sign in, which it does for itself.
*/
@Composable
internal fun JoinRoomFlow(onJoined: () -> Unit, onBack: () -> Unit) {
internal fun JoinRoomFlow(onJoined: () -> Unit, onBack: () -> Unit,
/** For previews: open at What comes across, with this copy from this room. */
preview: Pair<String, JSONObject>? = null) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
val finder = remember { RoomFinder(context) }
DisposableEffect(finder) { finder.start(); onDispose { finder.stop() } }
var phase by remember { mutableStateOf(JoinPhase.Find) }
var phase by remember { mutableStateOf(if (preview != null) JoinPhase.What else JoinPhase.Find) }
var address by remember { mutableStateOf("") }
var roomName by remember { mutableStateOf<String?>(null) }
var roomName by remember { mutableStateOf(preview?.first) }
var code by remember { mutableStateOf("") }
var note by remember { mutableStateOf<String?>(null) }
var join by remember { mutableStateOf<RoomJoin?>(null) }
var copy by remember { mutableStateOf<JSONObject?>(null) }
var copy by remember { mutableStateOf(preview?.second) }
var parts by remember { mutableStateOf(RoomPart.entries.toSet()) }
var sameSchedules by remember { mutableStateOf(true) }
var name by remember { mutableStateOf("Bedroom") }
var working by remember { mutableStateOf(false) }
val first = remember { FocusRequester() }
@@ -282,7 +289,7 @@ internal fun JoinRoomFlow(onJoined: () -> Unit, onBack: () -> Unit) {
delay(2_000)
}
}
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(12.dp)) {
Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState()), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) {
Text(when (phase) { JoinPhase.Find -> "Bring Orbit to this room"; JoinPhase.Code -> "Enter the code"; JoinPhase.Waiting -> "Waiting for ${roomName ?: "the other TV"}"
JoinPhase.What -> "What comes across"; JoinPhase.Name -> "Name this room" }, color = Ink, fontFamily = Anton, fontSize = 32.sp)
note?.let { Text(it, color = Yellow, fontSize = 14.sp) }
@@ -329,7 +336,15 @@ internal fun JoinRoomFlow(onJoined: () -> Unit, onBack: () -> Unit) {
OnOff(part in parts, if (i == 0) Modifier.focusRequester(first) else Modifier) { parts = if (part in parts) parts - part else parts + part }
}
}
Text("Your sign-in doesn't come across: this TV signs in for itself next. Schedules are this room's own.", color = Muted, fontSize = 12.sp)
// Paul, 6 November 2026: copy the other room's schedules, or make this room's own.
Text("SCHEDULES", color = Muted, fontSize = 11.sp, fontWeight = FontWeight.SemiBold)
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
ChoiceCard("Copy from ${roomName ?: "the other TV"}", listOf("The same programme on each channel in both rooms"), Modifier.width(305.dp),
selected = sameSchedules, enabled = RoomPart.Channels in parts) { sameSchedules = true }
ChoiceCard("Its own schedules", listOf("The same channels, with this room's own week"), Modifier.width(305.dp),
selected = !sameSchedules || RoomPart.Channels !in parts, enabled = RoomPart.Channels in parts) { sameSchedules = false }
}
Text("Your sign-in doesn't come across: this TV signs in for itself next. Copied schedules match today; after that each room plans its own next week.", color = Muted, fontSize = 12.sp)
OrbitButton("Next", Modifier.width(200.dp), centred = true) { phase = JoinPhase.Name }
}
JoinPhase.Name -> {
@@ -343,7 +358,7 @@ internal fun JoinRoomFlow(onJoined: () -> Unit, onBack: () -> Unit) {
val got = copy ?: return@OrbitButton
Rooms.rename(context, name)
RoomBundle.applyNow(context, got, parts)
RoomArrival.keep(got, parts)
RoomArrival.keep(got, parts, sameSchedules && RoomPart.Channels in parts)
android.util.Log.i("OrbitRooms", "joined parts=${parts.size}")
onJoined()
}
@@ -128,6 +128,20 @@ class RoomsTest {
assertEquals(listOf("episodes-101.json"), dir.list()!!.toList())
}
@Test fun sameSchedulesCopiesThePlansOnlyWhenChosen() {
val plans = JSONObject().put("schedule-101.json", "plan").put("progress-101.json", "{}").put("booked-0.json", "b").put("catalogue.db", "no").put("orbit-connection.v1", "no")
val bundle = JSONObject().put("version", 1).put(RoomPart.Channels.name, JSONObject().put("lineup", JSONArray(listOf(101)))).put("schedules", RoomBundle.gzip(plans.toString()))
val own = folder.newFolder(); RoomBundle.applyToWorkspace(own, bundle, setOf(RoomPart.Channels), sameSchedules = false)
assertFalse(java.io.File(own, "schedule-101.json").exists())
val same = folder.newFolder(); java.io.File(same, "progress-101.json").writeText("mine")
RoomBundle.applyToWorkspace(same, bundle, setOf(RoomPart.Channels), sameSchedules = true)
assertEquals("plan", java.io.File(same, "schedule-101.json").readText()); assertEquals("b", java.io.File(same, "booked-0.json").readText())
assertEquals("mine", java.io.File(same, "progress-101.json").readText()) // never over what's there
assertFalse(java.io.File(same, "catalogue.db").exists()); assertFalse(java.io.File(same, "orbit-connection.v1").exists())
val none = folder.newFolder(); RoomBundle.applyToWorkspace(none, bundle, emptySet(), sameSchedules = true)
assertTrue(none.list()!!.isEmpty()) // no channels, no schedules
}
@Test fun onlyHomeAddressesCanBeJoined() {
assertNotNull(roomTransport("192.168.0.20")); assertNull(roomTransport("8.8.8.8")); assertNull(roomTransport("living-room.example"))
}