Files
retroLabs/roku/components/Guide.brs

162 lines
5.5 KiB
Plaintext

sub showPanel(mode as string, title as string, status as string, titles as object)
m.panelMode = mode
m.guideOverlay.visible = false
m.panelTitle.text = title
m.panelStatus.text = status
content = CreateObject("roSGNode", "ContentNode")
for each labelText in titles
item = content.CreateChild("ContentNode")
item.title = labelText
end for
m.menu.content = content
m.menu.jumpToItem = 0
m.panel.visible = true
if titles.Count() = 0
m.remoteFocus.SetFocus(true)
else
m.menu.SetFocus(true)
end if
end sub
sub closePanel()
m.panelMode = ""
m.panel.visible = false
m.guideOverlay.visible = false
m.remoteFocus.SetFocus(true)
end sub
sub onMenuSelect(event as object)
index = event.GetData()
if m.panelMode = "servers"
if index < m.servers.Count()
connectServer(m.servers[index])
else if index = m.servers.Count()
openAddressDialog()
else
findServer("", false)
end if
else if m.panelMode = "lineup"
if index = 0
loadLineup()
else
findServer("", false)
end if
else if m.panelMode = "options"
if index = 0
openGuide()
else if index = 1
m.channelIndex = -1
m.weather.visible = false
stopWeather()
m.videoChannelId = invalid
m.video.visible = false
m.video.control = "stop"
m.retryTimer.control = "stop"
loadLineup()
else
findServer("", false)
end if
end if
end sub
sub openGuide(offset = 0 as integer)
if m.guideRequest <> invalid
m.guideRequest.UnobserveFieldScoped("result")
m.guideRequest.control = "stop"
end if
m.guideOffset = offset
m.panelMode = "guideLoading"
m.panel.visible = false
m.banner.visible = false
m.guideOverlay.visible = true
m.guideRows.visible = false
m.guideStatus.text = "Loading the schedule…"
m.remoteFocus.SetFocus(true)
m.guideRequest = startRequest({url: m.server.baseUrl + "/api/v1/guide?hours=3"}, "onGuide")
end sub
sub onGuide(event as object)
if m.guideRequest = invalid or not m.guideRequest.IsSameNode(event.GetRoSGNode()) then return
m.guideRequest = invalid
if m.panelMode <> "guideLoading" then return
data = event.GetData().data
if data = invalid or GetInterface(data.channels, "ifArray") = invalid or data.channels.Count() = 0
m.panelMode = "guideError"
m.guideStatus.text = "Schedule unavailable · OK Retry"
return
end if
syncClock(data.serverTime)
m.guideChannels = data.channels
selected = 0
for i = 0 to m.guideChannels.Count() - 1
if m.channelIndex >= 0 and m.guideChannels[i].id = m.channels[m.channelIndex].id then selected = i
end for
count = m.guideChannels.Count()
m.guideSelected = (selected + m.guideOffset + count) mod count
m.guideFirst = 0
m.panelMode = "guide"
m.guideRows.visible = true
m.guideStatus.text = "PROGRAM GUIDE Up / Down Browse OK Watch Back Close"
renderGuide()
end sub
sub renderGuide()
if m.guideSelected < m.guideFirst then m.guideFirst = m.guideSelected
if m.guideSelected > m.guideFirst + 2 then m.guideFirst = m.guideSelected - 2
now = currentEpoch()
for i = 0 to 2
index = m.guideFirst + i
suffix = StrI(i).Trim()
row = m.top.FindNode("guideRow" + suffix)
row.visible = index < m.guideChannels.Count()
if row.visible
channel = m.guideChannels[index]
current = invalid
upcoming = invalid
for each program in channel.programs
if parseUtc(program.start) > now
upcoming = program
exit for
else if parseUtc(program.stop) > now
current = program
end if
end for
m.top.FindNode("guideFill" + suffix).color = "0x1B2B48FF"
if index = m.guideSelected then m.top.FindNode("guideFill" + suffix).color = "0x376C89FF"
m.top.FindNode("guideChannel" + suffix).text = channelLabel(channel)
m.top.FindNode("guideNow" + suffix).text = programTitle(current)
if channel.type = "weather" then m.top.FindNode("guideNow" + suffix).text = "Local weather"
m.top.FindNode("guideNext" + suffix).text = programTitle(upcoming)
end if
end for
end sub
function handleGuideKey(key as string) as boolean
if key = "back"
if m.channelIndex < 0 then return false
closePanel()
else if key = "options"
showPanel("options", "retroLabs", m.server.name + " " + m.server.baseUrl, ["Program guide", "Refresh channels", "Change server"])
else if key = "ok" and m.panelMode = "guideError"
openGuide()
else if m.panelMode = "guide"
if key = "up" or key = "down"
direction = 1
if key = "up" then direction = -1
count = m.guideChannels.Count()
m.guideSelected = (m.guideSelected + direction + count) mod count
renderGuide()
else if key = "ok"
id = m.guideChannels[m.guideSelected].id
for i = 0 to m.channels.Count() - 1
if m.channels[i].id = id
closePanel()
if i <> m.channelIndex then tune(i)
exit for
end if
end for
end if
end if
return true
end function