@@ -0,0 +1,313 @@
package tv.orbit.classic
import android.content.Context
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusProperties
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
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.dp
import androidx.compose.ui.unit.sp
import androidx.tv.material3.Text
import kotlinx.coroutines.delay
/**
* Orbit's layouts (Paul, 11 and 12 November 2026, from design/layouts): how Home is laid out, separately from the look's
* colours, so any layout wears any of the ten looks. Orbit Original stays exactly as it was. Each layout only changes Home;
* the guide, Catch Up, the Drive-In and watching full screen are shared, and every layout plays at most one stream, as
* before (the guide picture's rules, including its 10-minute rest, apply wherever a picture shows).
*/
internal enum class OrbitLayout ( val title : String , val line : String ) {
Original ( " Orbit Original " , " The category menu, as it has always been " ) ,
JustWatch ( " Just Watch " , " Opens on the last channel; OK for the menu " ) ,
Simple ( " Orbit Simple " , " Big type and four big choices " ) ,
RetroCable ( " Retro Cable " , " The picture is home; surf with Up and Down " ) ,
Mosaic ( " Mosaic " , " Nine channels at a glance; one plays " ) ,
Teletext ( " Teletext " , " Orbit as OrbiFax pages " ) ,
Kids ( " Kids " , " Big picture cards for the kids' channels " )
}
internal object OrbitLayouts {
var current by mutableStateOf ( OrbitLayout . Original )
private set
private fun prefs ( context : Context ) = context . getSharedPreferences ( " orbit_settings " , Context . MODE _PRIVATE )
fun load ( context : Context ) { current = prefs ( context ) . getString ( " layout " , null ) ?. let { n -> OrbitLayout . entries . firstOrNull { it . name == n } } ?: OrbitLayout . Original }
fun choose ( context : Context , chosen : OrbitLayout ) { prefs ( context ) . edit ( ) . putString ( " layout " , chosen . name ) . apply ( ) ; current = chosen }
/** For tests and previews. */
internal fun use ( chosen : OrbitLayout ) { current = chosen }
}
internal val Comfortaa = androidx . compose . ui . text . font . FontFamily ( androidx . compose . ui . text . font . Font ( R . font . comfortaa ) )
/** Kids' channels for the Kids layout: Kids and Cartoons, or every channel if there are none. */
internal fun kidsChannels ( channels : List < SampleChannel > ) : List < SampleChannel > = kidsNumbers ( channels . map { it . number } ) . let { keep -> channels . filter { it . number in keep } }
internal fun kidsNumbers ( numbers : List < Int > ) : List < Int > =
numbers . filter { ChannelBrands . forNumber ( it ) ?. category . let { c -> c == ChannelCategory . Kids || c == ChannelCategory . Cartoons } } . ifEmpty { numbers }
/** Everything the layouts need from the main screen. */
internal class LayoutHome (
val session : EmbySampleSession , val channels : List < SampleChannel > , val selected : SampleChannel ,
val picture : @Composable ( Modifier ) -> Unit ,
val onSelect : ( Int ) -> Unit , val onWatch : ( Int ) -> Unit , val onSection : ( String ) -> Unit , val kids : Boolean )
@Composable
internal fun LayoutHomeScreen ( layout : OrbitLayout , home : LayoutHome , modifier : Modifier ) {
when ( layout ) {
OrbitLayout . JustWatch -> JustWatchHome ( home , modifier )
OrbitLayout . Simple -> SimpleHome ( home , modifier )
OrbitLayout . RetroCable -> CableHome ( home , modifier )
OrbitLayout . Mosaic -> MosaicHome ( home , modifier )
OrbitLayout . Kids -> KidsHome ( home , modifier )
else -> Unit // Original and Teletext are drawn by the main screen.
}
}
private val Round = RoundedCornerShape ( 10 . dp )
/** A focusable card: gold fill and a ring when focused, never colour alone. */
@Composable
private fun LayoutCard ( modifier : Modifier , focus : FocusRequester ? = null , onFocused : ( ) -> Unit = { } , onClick : ( ) -> Unit , shape : RoundedCornerShape = Round ,
content : @Composable BoxScope . ( Boolean ) -> Unit ) {
var focused by remember { mutableStateOf ( false ) }
Box ( modifier . then ( if ( focus != null ) Modifier . focusRequester ( focus ) else Modifier )
. onFocusChanged { focused = it . isFocused ; if ( it . isFocused ) onFocused ( ) }
. border ( if ( focused ) 3 . dp else 0 . dp , if ( focused ) Yellow else Color . Transparent , shape ) . padding ( if ( focused ) 4 . dp else 0 . dp )
. clip ( shape ) . background ( focusColor ( if ( focused ) Yellow else Panel ) ) . clickable ( onClick = onClick ) ) { content ( focused ) }
}
@Composable
private fun firstFocus ( ) : FocusRequester {
val f = remember { FocusRequester ( ) }
LaunchedEffect ( Unit ) { repeat ( 20 ) { if ( runCatching { f . requestFocus ( ) } . isSuccess ) return @LaunchedEffect ; delay ( 50 ) } }
return f
}
private fun nowAt ( channel : SampleChannel ) = runCatching { channel . at ( System . currentTimeMillis ( ) ) } . getOrNull ( )
@Composable
private fun ChannelLogo ( number : Int , modifier : Modifier ) {
ChannelBrands . forNumber ( number ) ?. let { Image ( cachedPainter ( it . logo ) , it . displayName , modifier . logoPlate ( ) ) }
}
@Composable
private fun NowLines ( channel : SampleChannel , onAccent : Boolean , titleSize : Int = 16 , lineSize : Int = 12 ) {
val show = nowAt ( channel ) ?: return
val now = System . currentTimeMillis ( )
Text ( " ${channel.number} ${guideTitle(channel.number, show).first} " , color = if ( onAccent ) OnAccent else Ink , fontSize = titleSize . sp , fontWeight = FontWeight . Bold ,
maxLines = 1 , overflow = TextOverflow . Ellipsis )
Text ( leftLabel ( show . endMs - now ) , color = if ( onAccent ) OnAccent else Muted , fontSize = lineSize . sp , maxLines = 1 )
Box ( Modifier . padding ( top = 4 . dp ) . fillMaxWidth ( ) . height ( 4 . dp ) . clip ( RoundedCornerShape ( 2 . dp ) ) . background ( if ( onAccent ) OnAccent . copy ( alpha = .25f ) else Blue ) ) {
Box ( Modifier . fillMaxWidth ( programmeProgress ( show . startMs , show . endMs , now ) ) . fillMaxHeight ( ) . background ( if ( onAccent ) OnAccent else Yellow ) )
}
}
// ---------- Just Watch ----------
/**
* Just Watch: Orbit opens playing (the main screen tunes the last channel as Orbit opens), and Back from full screen comes
* here: the picture fills the screen behind one row of choices, or a strip of channels.
*/
@Composable
private fun JustWatchHome ( h : LayoutHome , modifier : Modifier ) {
var channels by remember { mutableStateOf ( false ) }
Box ( modifier . fillMaxSize ( ) ) {
h . picture ( Modifier . fillMaxSize ( ) )
Column ( Modifier . align ( Alignment . BottomStart ) . fillMaxWidth ( ) . background ( Navy . copy ( alpha = .88f ) , RoundedCornerShape ( 12 . dp ) ) . padding ( 16 . dp ) ,
verticalArrangement = Arrangement . spacedBy ( 10 . dp ) ) {
nowAt ( h . selected ) ?. let { show -> Text ( " ${h.selected.number} ${guideTitle(h.selected.number, show).first} " , color = Ink , fontSize = 18 . sp , fontWeight = FontWeight . Bold ) }
if ( ! channels ) {
val first = firstFocus ( )
Row ( horizontalArrangement = Arrangement . spacedBy ( 12 . dp ) ) {
listOf ( " Watch " to { h . onWatch ( h . selected . number ) } , " Channels " to { channels = true } , " TV Guide " to { h . onSection ( " TV Guide " ) } ,
" Catch Up " to { h . onSection ( " Catch Up " ) } , " Drive-In " to { h . onSection ( " Drive-In " ) } ) . forEachIndexed { i , ( label , go ) ->
LayoutCard ( Modifier . width ( 140 . dp ) . height ( 48 . dp ) , if ( i == 0 ) first else null , onClick = go ) { f ->
Text ( label , Modifier . align ( Alignment . Center ) , color = if ( f ) OnAccent else Ink , fontSize = 16 . sp , fontWeight = FontWeight . Bold )
}
}
}
} else {
androidx . activity . compose . BackHandler { channels = false }
val first = firstFocus ( )
LazyRow ( horizontalArrangement = Arrangement . spacedBy ( 12 . dp ) ) {
itemsIndexed ( h . channels , key = { _ , c -> c . number } ) { i , c ->
LayoutCard ( Modifier . width ( 170 . dp ) . height ( 116 . dp ) , if ( c . number == h . selected . number || ( i == 0 && h . channels . none { it . number == h . selected . number } ) ) first else null ,
onClick = { h . onWatch ( c . number ) } ) { f ->
Column ( Modifier . padding ( 10 . dp ) , verticalArrangement = Arrangement . spacedBy ( 2 . dp ) ) {
ChannelLogo ( c . number , Modifier . height ( 32 . dp ) )
NowLines ( c , f , 13 , 11 )
}
}
}
}
}
}
}
}
// ---------- Orbit Simple ----------
/** Orbit Simple: what's on now, then four big choices in one row. Left and Right only. */
@Composable
private fun SimpleHome ( h : LayoutHome , modifier : Modifier ) {
val first = firstFocus ( )
Column ( modifier . fillMaxSize ( ) , verticalArrangement = Arrangement . spacedBy ( 16 . dp ) ) {
Row ( Modifier . fillMaxWidth ( ) . height ( 150 . dp ) . clip ( RoundedCornerShape ( 12 . dp ) ) . background ( Panel ) . padding ( 12 . dp ) , horizontalArrangement = Arrangement . spacedBy ( 16 . dp ) ) {
h . picture ( Modifier . width ( 226 . dp ) . fillMaxHeight ( ) . clip ( RoundedCornerShape ( 8 . dp ) ) )
Column ( Modifier . weight ( 1f ) , verticalArrangement = Arrangement . spacedBy ( 4 . dp ) ) {
Text ( " On now " , color = Muted , fontSize = 16 . sp )
NowLines ( h . selected , false , 26 , 16 )
}
}
Row ( horizontalArrangement = Arrangement . spacedBy ( 16 . dp ) ) {
val choices = listOfNotNull ( " Watch TV " to { h . onWatch ( h . selected . number ) } , " TV Guide " to { h . onSection ( " TV Guide " ) } , " Catch Up " to { h . onSection ( " Catch Up " ) } ,
if ( h . kids ) null else " Settings " to { h . onSection ( " Connect Emby " ) } )
choices . forEachIndexed { i , ( label , go ) ->
LayoutCard ( Modifier . weight ( 1f ) . height ( 150 . dp ) , if ( i == 0 ) first else null , onClick = go , shape = RoundedCornerShape ( 14 . dp ) ) { f ->
Text ( label , Modifier . align ( Alignment . BottomStart ) . padding ( 18 . dp ) , color = if ( f ) OnAccent else Ink , fontSize = 26 . sp , fontWeight = FontWeight . Bold )
}
}
}
Text ( " ◀ ▶ Choose OK Open Back Leave Orbit " , color = Muted , fontSize = 15 . sp )
}
}
// ---------- Retro Cable ----------
/** Retro Cable: the picture is home. A surf list of every channel on the left (Up and Down, the picture follows after 600 ms), Now and Next below. */
@Composable
private fun CableHome ( h : LayoutHome , modifier : Modifier ) {
val numbers = h . channels . map { it . number }
Box ( modifier . fillMaxSize ( ) ) {
h . picture ( Modifier . fillMaxSize ( ) )
Box ( Modifier . align ( Alignment . TopEnd ) . background ( Color . Black . copy ( alpha = .75f ) , RoundedCornerShape ( 6 . dp ) ) . padding ( horizontal = 14 . dp , vertical = 4 . dp ) ) {
Text ( " %03d " . format ( h . selected . number ) , color = Mint , fontFamily = BarlowCondensed , fontSize = 40 . sp )
}
val first = firstFocus ( )
// Every channel in a list that scrolls; the focused one becomes the channel after 600 ms, so surfing past opens nothing.
var focusedNumber by remember { mutableStateOf < Int ? > ( null ) }
LaunchedEffect ( focusedNumber ) { val n = focusedNumber ?: return @LaunchedEffect ; delay ( 600 ) ; h . onSelect ( n ) }
androidx . compose . foundation . lazy . LazyColumn ( Modifier . align ( Alignment . CenterStart ) . width ( 280 . dp ) . height ( 240 . dp ) , verticalArrangement = Arrangement . spacedBy ( 6 . dp ) ) {
itemsIndexed ( h . channels , key = { _ , c -> c . number } ) { i , c ->
val n = c . number
LayoutCard ( Modifier . fillMaxWidth ( ) . height ( 40 . dp ) , if ( n == h . selected . number || ( i == 0 && numbers . none { it == h . selected . number } ) ) first else null ,
onFocused = { focusedNumber = n } , onClick = { h . onWatch ( n ) } , shape = RoundedCornerShape ( 6 . dp ) ) { f ->
Row ( Modifier . fillMaxSize ( ) . padding ( horizontal = 10 . dp ) , verticalAlignment = Alignment . CenterVertically , horizontalArrangement = Arrangement . spacedBy ( 10 . dp ) ) {
Text ( " $n " , color = if ( f ) OnAccent else Mint , fontFamily = BarlowCondensed , fontSize = 18 . sp )
Text ( nowAt ( c ) ?. let { guideTitle ( n , it ) . first } . orEmpty ( ) , color = if ( f ) OnAccent else Ink , fontSize = 14 . sp , maxLines = 1 , overflow = TextOverflow . Ellipsis )
}
}
}
}
val show = nowAt ( h . selected )
if ( show != null ) Column ( Modifier . align ( Alignment . BottomCenter ) . fillMaxWidth ( ) . background ( Panel . copy ( alpha = .94f ) , RoundedCornerShape ( 6 . dp ) ) . padding ( 14 . dp ) ) {
Row ( verticalAlignment = Alignment . CenterVertically , horizontalArrangement = Arrangement . spacedBy ( 12 . dp ) ) {
ChannelLogo ( h . selected . number , Modifier . height ( 36 . dp ) )
Text ( " NOW " , color = Yellow , fontFamily = Anton , fontSize = 14 . sp )
Text ( guideTitle ( h . selected . number , show ) . first , color = Ink , fontSize = 18 . sp , fontWeight = FontWeight . Bold , maxLines = 1 )
Spacer ( Modifier . weight ( 1f ) )
Text ( " ▲▼ surf · OK watch " , color = Muted , fontSize = 13 . sp )
}
runCatching { h . selected . at ( show . endMs + 1 ) } . getOrNull ( ) ?. let { next ->
Row ( horizontalArrangement = Arrangement . spacedBy ( 12 . dp ) ) {
Text ( " NEXT " , color = Muted , fontFamily = Anton , fontSize = 14 . sp )
Text ( guideTitle ( h . selected . number , next ) . first , color = Ink , fontSize = 15 . sp , maxLines = 1 )
}
}
}
}
}
// ---------- Mosaic ----------
/** Mosaic: a wall of channels with their programmes' artwork. The focused tile becomes the channel after 600 ms, and only it plays. */
@Composable
private fun MosaicHome ( h : LayoutHome , modifier : Modifier ) {
val first = firstFocus ( )
var focusedNumber by remember { mutableStateOf < Int ? > ( null ) }
LaunchedEffect ( focusedNumber ) { val n = focusedNumber ?: return @LaunchedEffect ; delay ( 600 ) ; h . onSelect ( n ) }
LazyVerticalGrid ( GridCells . Fixed ( 3 ) , modifier . fillMaxSize ( ) , horizontalArrangement = Arrangement . spacedBy ( 10 . dp ) , verticalArrangement = Arrangement . spacedBy ( 10 . dp ) ) {
itemsIndexed ( h . channels , key = { _ , c -> c . number } ) { i , c ->
LayoutCard ( Modifier . fillMaxWidth ( ) . aspectRatio ( 16f / 10f ) , if ( c . number == h . selected . number || ( i == 0 && h . channels . none { it . number == h . selected . number } ) ) first else null ,
onFocused = { focusedNumber = c . number } , onClick = { h . onWatch ( c . number ) } ) { f ->
Column ( Modifier . fillMaxSize ( ) ) {
Box ( Modifier . weight ( 1f ) . fillMaxWidth ( ) ) {
val show = nowAt ( c )
if ( f && c . number == h . selected . number ) h . picture ( Modifier . fillMaxSize ( ) )
else if ( show != null ) ProgrammeArtwork ( h . session , c , show , Modifier . fillMaxSize ( ) )
ChannelLogo ( c . number , Modifier . padding ( 6 . dp ) . height ( 22 . dp ) )
}
Column ( Modifier . padding ( horizontal = 8 . dp , vertical = 4 . dp ) ) { NowLines ( c , f , 13 , 10 ) }
}
}
}
}
}
// ---------- Kids ----------
/** Kids: big picture cards, one row, a word each. Only the kids' channels; no guide grid and no settings. */
@Composable
private fun KidsHome ( h : LayoutHome , modifier : Modifier ) {
val first = firstFocus ( )
val kids = kidsChannels ( h . channels )
Column ( modifier . fillMaxSize ( ) , verticalArrangement = Arrangement . spacedBy ( 16 . dp ) ) {
Text ( " What shall we watch? " , color = Ink , fontFamily = Comfortaa , fontSize = 30 . sp , fontWeight = FontWeight . Bold )
LazyRow ( horizontalArrangement = Arrangement . spacedBy ( 18 . dp ) ) {
itemsIndexed ( kids , key = { _ , c -> c . number } ) { i , c ->
LayoutCard ( Modifier . width ( 200 . dp ) . height ( 300 . dp ) , if ( i == 0 ) first else null , onClick = { h . onWatch ( c . number ) } , shape = RoundedCornerShape ( 20 . dp ) ) { f ->
Column ( Modifier . fillMaxSize ( ) ) {
Box ( Modifier . weight ( 1f ) . fillMaxWidth ( ) ) {
nowAt ( c ) ?. let { ProgrammeArtwork ( h . session , c , it , Modifier . fillMaxSize ( ) ) }
ChannelLogo ( c . number , Modifier . padding ( 10 . dp ) . height ( 36 . dp ) )
}
Column ( Modifier . padding ( 14 . dp ) , verticalArrangement = Arrangement . spacedBy ( 2 . dp ) ) {
Text ( ChannelBrands . forNumber ( c . number ) ?. displayName ?. removePrefix ( " Orbit " ) ?: c . name , color = if ( f ) OnAccent else Ink , fontFamily = Comfortaa ,
fontSize = 24 . sp , fontWeight = FontWeight . Bold , maxLines = 1 )
nowAt ( c ) ?. let { Text ( guideTitle ( c . number , it ) . first , color = if ( f ) OnAccent else Muted , fontFamily = Comfortaa , fontSize = 14 . sp , maxLines = 1 , overflow = TextOverflow . Ellipsis ) }
}
}
}
}
}
Text ( " ◀ ▶ pick · OK watch " , color = Muted , fontFamily = Comfortaa , fontSize = 18 . sp , fontWeight = FontWeight . Bold )
}
}
/** The layouts as a list, for Settings › On screen. */
@Composable
internal fun LayoutPicker ( context : Context = androidx . compose . ui . platform . LocalContext . current ) {
Column ( verticalArrangement = Arrangement . spacedBy ( 6 . dp ) ) {
OrbitLayout . entries . chunked ( 4 ) . forEach { row ->
Row ( horizontalArrangement = Arrangement . spacedBy ( 10 . dp ) ) {
row . forEach { layout ->
val inUse = OrbitLayouts . current == layout
LayoutCard ( Modifier . width ( 150 . dp ) . height ( 56 . dp ) , onClick = { OrbitLayouts . choose ( context , layout ) } , shape = RoundedCornerShape ( 8 . dp ) ) { f ->
Column ( Modifier . padding ( horizontal = 10 . dp , vertical = 6 . dp ) ) {
Text ( layout . title + if ( inUse ) " ✓ " else " " , color = if ( f ) OnAccent else if ( inUse ) Yellow else Ink , fontSize = 13 . sp , fontWeight = FontWeight . SemiBold , maxLines = 1 )
Text ( layout . line , color = if ( f ) OnAccent else Muted , fontSize = 10 . sp , maxLines = 2 , overflow = TextOverflow . Ellipsis )
}
}
}
}
}
}
}