|
|
|
@@ -0,0 +1,145 @@
|
|
|
|
|
package tv.orbit.classic
|
|
|
|
|
|
|
|
|
|
import android.view.KeyEvent
|
|
|
|
|
import androidx.activity.compose.BackHandler
|
|
|
|
|
import androidx.compose.foundation.Image
|
|
|
|
|
import androidx.compose.foundation.background
|
|
|
|
|
import androidx.compose.foundation.focusable
|
|
|
|
|
import androidx.compose.foundation.layout.*
|
|
|
|
|
import androidx.compose.runtime.*
|
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
|
import androidx.compose.ui.focus.FocusRequester
|
|
|
|
|
import androidx.compose.ui.focus.focusRequester
|
|
|
|
|
import androidx.compose.ui.focus.onFocusChanged
|
|
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
|
|
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
|
|
|
|
import androidx.compose.ui.layout.ContentScale
|
|
|
|
|
import androidx.compose.ui.res.painterResource
|
|
|
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
|
|
|
import androidx.compose.ui.text.style.TextOverflow
|
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
|
import androidx.compose.ui.unit.sp
|
|
|
|
|
import androidx.tv.material3.Text
|
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
|
|
|
|
|
|
internal const val GUIDE_SLOT_MS = 30 * 60_000L
|
|
|
|
|
internal const val GUIDE_SLOTS = 4
|
|
|
|
|
private const val GUIDE_MAX_SLOTS_AHEAD = 48
|
|
|
|
|
|
|
|
|
|
internal data class GuideCell(val show: SampleBroadcast, val startFraction: Float, val widthFraction: Float)
|
|
|
|
|
|
|
|
|
|
/** The programmes a channel airs inside a window, clipped to it. The channel's own clock decides everything. */
|
|
|
|
|
internal fun guideCells(channel: SampleChannel, windowStart: Long, windowEnd: Long): List<GuideCell> {
|
|
|
|
|
require(windowEnd > windowStart)
|
|
|
|
|
val span = (windowEnd - windowStart).toFloat()
|
|
|
|
|
val cells = mutableListOf<GuideCell>()
|
|
|
|
|
var show = channel.at(windowStart)
|
|
|
|
|
while (show.startMs < windowEnd) {
|
|
|
|
|
val from = maxOf(show.startMs, windowStart)
|
|
|
|
|
val to = minOf(show.endMs, windowEnd)
|
|
|
|
|
cells += GuideCell(show, (from - windowStart) / span, (to - from) / span)
|
|
|
|
|
show = channel.at(show.endMs)
|
|
|
|
|
}
|
|
|
|
|
return cells
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal fun guideWindowStart(nowMs: Long, slotsAhead: Int) = Math.floorDiv(nowMs, GUIDE_SLOT_MS) * GUIDE_SLOT_MS + slotsAhead * GUIDE_SLOT_MS
|
|
|
|
|
|
|
|
|
|
/** Cursor time after moving one programme along a channel, never earlier than now. */
|
|
|
|
|
internal fun guideStep(channel: SampleChannel, cursorMs: Long, nowMs: Long, forward: Boolean): Long {
|
|
|
|
|
val show = channel.at(maxOf(cursorMs, nowMs))
|
|
|
|
|
return if (forward) show.endMs else maxOf(show.startMs - 1, nowMs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
internal fun GuideGrid(channels: List<SampleChannel>, selectedNumber: Int, modifier: Modifier = Modifier, onWatch: (Int, Long) -> Unit, onBack: () -> Unit) {
|
|
|
|
|
var now by remember { mutableLongStateOf(System.currentTimeMillis()) }
|
|
|
|
|
LaunchedEffect(Unit) { while (true) { delay(1000); now = System.currentTimeMillis() } }
|
|
|
|
|
var row by remember { mutableIntStateOf(channels.indexOfFirst { it.number == selectedNumber }.coerceAtLeast(0)) }
|
|
|
|
|
var cursor by remember { mutableLongStateOf(now) }
|
|
|
|
|
var slotsAhead by remember { mutableIntStateOf(0) }
|
|
|
|
|
val gridFocus = remember { FocusRequester() }
|
|
|
|
|
val toolsFocus = remember { FocusRequester() }
|
|
|
|
|
var gridFocused by remember { mutableStateOf(true) }
|
|
|
|
|
LaunchedEffect(Unit) { gridFocus.requestFocus() }
|
|
|
|
|
BackHandler(onBack = onBack)
|
|
|
|
|
val windowStart = guideWindowStart(now, slotsAhead)
|
|
|
|
|
val windowEnd = windowStart + GUIDE_SLOTS * GUIDE_SLOT_MS
|
|
|
|
|
val channel = channels[row.coerceIn(channels.indices)]
|
|
|
|
|
val focused = channel.at(maxOf(cursor, now))
|
|
|
|
|
fun keepCursorInWindow(time: Long) {
|
|
|
|
|
cursor = time
|
|
|
|
|
val base = guideWindowStart(now, 0)
|
|
|
|
|
var ahead = slotsAhead
|
|
|
|
|
while (time >= base + (ahead + GUIDE_SLOTS) * GUIDE_SLOT_MS && ahead < GUIDE_MAX_SLOTS_AHEAD) ahead++
|
|
|
|
|
while (time < base + ahead * GUIDE_SLOT_MS && ahead > 0) ahead--
|
|
|
|
|
slotsAhead = ahead
|
|
|
|
|
}
|
|
|
|
|
Column(modifier.fillMaxWidth().background(Navy).padding(horizontal = 14.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(5.dp)) {
|
|
|
|
|
Row(Modifier.fillMaxWidth().height(126.dp), horizontalArrangement = Arrangement.spacedBy(12.dp)) {
|
|
|
|
|
val branding = ChannelBrands.forNumber(channel.number)
|
|
|
|
|
Box(Modifier.width(224.dp).fillMaxHeight().background(Panel)) {
|
|
|
|
|
if (branding != null) Image(painterResource(branding.background), null, Modifier.matchParentSize(), contentScale = ContentScale.Crop)
|
|
|
|
|
}
|
|
|
|
|
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) {
|
|
|
|
|
if (branding != null) Image(painterResource(branding.logo), branding.displayName, Modifier.width(112.dp).height(35.dp))
|
|
|
|
|
else Text(ChannelBrands.displayName(channel), color = Color.White, fontSize = 16.sp)
|
|
|
|
|
Text(focused.media.item.title, color = Color.White, fontSize = 21.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
|
|
|
|
Text("${clock(focused.startMs)} to ${clock(focused.endMs)} • ${channel.number} ${ChannelBrands.displayName(channel)}", color = Muted, fontSize = 13.sp, maxLines = 1)
|
|
|
|
|
Text(if (focused.startMs <= now) "OK Watch" else "On later", color = Muted, fontSize = 12.sp)
|
|
|
|
|
}
|
|
|
|
|
Column(verticalArrangement = Arrangement.Top) {
|
|
|
|
|
OrbitButton("Test transition", Modifier.focusRequester(toolsFocus).onPreviewKeyEvent { event ->
|
|
|
|
|
if (event.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) { gridFocus.requestFocus(); true } else false
|
|
|
|
|
}, onClick = { onWatch(channel.number, channel.transitionCheckOffset(System.currentTimeMillis())) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Row(Modifier.fillMaxWidth().height(24.dp).background(Panel), verticalAlignment = Alignment.CenterVertically) {
|
|
|
|
|
Text("All Channels", Modifier.width(150.dp).padding(start = 8.dp), color = Color.White, fontSize = 13.sp, fontWeight = FontWeight.SemiBold)
|
|
|
|
|
Row(Modifier.weight(1f)) {
|
|
|
|
|
repeat(GUIDE_SLOTS) { Text(clock(windowStart + it * GUIDE_SLOT_MS), Modifier.weight(1f).padding(start = 4.dp), color = Color.White, fontSize = 13.sp, fontWeight = FontWeight.SemiBold) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Column(Modifier.fillMaxWidth().weight(1f).focusRequester(gridFocus).onFocusChanged { gridFocused = it.isFocused }.focusable().onPreviewKeyEvent { event ->
|
|
|
|
|
val native = event.nativeKeyEvent
|
|
|
|
|
if (native.action != KeyEvent.ACTION_DOWN) return@onPreviewKeyEvent false
|
|
|
|
|
when (native.keyCode) {
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_UP -> { if (row > 0) row-- else toolsFocus.requestFocus(); true }
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_DOWN -> { if (row < channels.lastIndex) row++; true }
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_RIGHT -> {
|
|
|
|
|
if (slotsAhead < GUIDE_MAX_SLOTS_AHEAD) keepCursorInWindow(guideStep(channel, cursor, now, true)); true
|
|
|
|
|
}
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_LEFT -> { keepCursorInWindow(guideStep(channel, cursor, now, false)); true }
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_ENTER -> {
|
|
|
|
|
if (native.repeatCount == 0 && focused.startMs <= now) onWatch(channel.number, 0); true
|
|
|
|
|
}
|
|
|
|
|
else -> false
|
|
|
|
|
}
|
|
|
|
|
}, verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
|
|
|
|
channels.forEachIndexed { index, line ->
|
|
|
|
|
Row(Modifier.fillMaxWidth().weight(1f).widthIn(max = 2000.dp), horizontalArrangement = Arrangement.spacedBy(0.dp)) {
|
|
|
|
|
val mark = ChannelBrands.forNumber(line.number)
|
|
|
|
|
Row(Modifier.width(150.dp).fillMaxHeight().background(Panel).padding(start = 6.dp), verticalAlignment = Alignment.CenterVertically) {
|
|
|
|
|
Text(line.number.toString(), Modifier.width(34.dp), color = Muted, fontSize = 13.sp, fontWeight = FontWeight.SemiBold)
|
|
|
|
|
if (mark != null) Image(painterResource(mark.logo), mark.displayName, Modifier.width(104.dp).height(32.dp))
|
|
|
|
|
else Text(ChannelBrands.displayName(line), color = Color.White, fontSize = 13.sp, maxLines = 1)
|
|
|
|
|
}
|
|
|
|
|
BoxWithConstraints(Modifier.weight(1f).fillMaxHeight()) {
|
|
|
|
|
guideCells(line, windowStart, windowEnd).forEach { cell ->
|
|
|
|
|
val isFocus = gridFocused && index == row && cell.show.startMs == focused.startMs
|
|
|
|
|
val onNow = cell.show.startMs <= now
|
|
|
|
|
Box(Modifier.offset(x = maxWidth * cell.startFraction).width((maxWidth * cell.widthFraction - 2.dp).coerceAtLeast(1.dp)).fillMaxHeight()
|
|
|
|
|
.background(if (isFocus) Yellow else if (onNow) Blue else Panel).padding(horizontal = 6.dp), contentAlignment = Alignment.CenterStart) {
|
|
|
|
|
Text(cell.show.media.item.title, color = if (isFocus) Navy else Color.White, fontSize = 14.sp, fontWeight = if (isFocus) FontWeight.SemiBold else FontWeight.Normal, maxLines = 2, overflow = TextOverflow.Ellipsis)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (now in windowStart until windowEnd)
|
|
|
|
|
Box(Modifier.offset(x = maxWidth * ((now - windowStart).toFloat() / (windowEnd - windowStart))).width(2.dp).fillMaxHeight().background(Color.White))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Text("▲▼ Channel ◄► Time OK Watch Back Menu", color = Muted, fontSize = 12.sp)
|
|
|
|
|
}
|
|
|
|
|
}
|