Files
retroLabs/roku/components/Guide.brs

144 lines
4.6 KiB
Plaintext

sub showPanel(mode as string, title as string, status as string, titles as object)
m.panelMode = mode
m.guideDetails.visible = false
m.menu.itemSize = [1150, 50]
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.top.SetFocus(true)
else
m.menu.SetFocus(true)
end if
end sub
sub closePanel()
m.panelMode = ""
m.panel.visible = false
m.top.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
else if m.panelMode = "guide"
if index < 0 or index >= m.guideIds.Count() then return
id = m.guideIds[index]
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
else if m.panelMode = "guideError"
openGuide()
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
showPanel("guideLoading", "Program guide", "Loading the schedule…", [])
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
showPanel("guideError", "Program guide", "Could not load the schedule.", ["Retry"])
return
end if
syncClock(data.serverTime)
m.guideIds = []
m.guideChannels = data.channels
titles = []
selected = 0
for each channel in m.guideChannels
if m.channelIndex >= 0 and channel.id = m.channels[m.channelIndex].id then selected = titles.Count()
titles.Push(channelLabel(channel))
m.guideIds.Push(channel.id)
end for
selected = (selected + m.guideOffset + titles.Count()) mod titles.Count()
showPanel("guide", "Program guide", "Up / Down Browse OK Watch Star Options", titles)
m.menu.itemSize = [430, 50]
m.guideDetails.visible = true
m.menu.jumpToItem = selected
showGuideDetails(selected)
end sub
sub onGuideFocus(event as object)
if m.panelMode = "guide" then showGuideDetails(event.GetData())
end sub
sub showGuideDetails(index as integer)
if index < 0 or index >= m.guideChannels.Count() then return
channel = m.guideChannels[index]
m.guideChannelTitle.text = channelLabel(channel)
m.guideNow.text = ""
m.guideNext.text = ""
m.guideLater.text = ""
if channel.type = "weather"
m.guideNow.text = "LOCAL WEATHER"
m.guideNext.text = "Current conditions, forecasts and alerts"
return
end if
now = currentEpoch()
upcoming = []
current = invalid
for each program in channel.programs
if parseUtc(program.start) > now
upcoming.Push(program)
else if parseUtc(program.stop) > now
current = program
end if
end for
m.guideNow.text = "NOW" + Chr(10) + programTitle(current)
nextProgram = invalid
if upcoming.Count() > 0 then nextProgram = upcoming[0]
m.guideNext.text = "NEXT" + Chr(10) + programTitle(nextProgram)
if upcoming.Count() > 1 then m.guideLater.text = "LATER" + Chr(10) + programTitle(upcoming[1])
end sub