Add groups to stack to manage active group for main scene

This commit is contained in:
JD Layman 2021-09-13 21:03:32 -05:00
parent e23f3ff8ac
commit be24a19c8f
8 changed files with 229 additions and 210 deletions

View File

@ -7,10 +7,16 @@ function onKeyEvent(key as string, press as boolean) as boolean
if not press then return false
if key = "back"
m.top.backPressed = true
m.global.groupStack.callFunc("pop")
return true
else if key = "options"
m.top.optionsPressed = true
group = m.global.groupStack.callFunc("peek")
if group.optionsAvailable
group.lastFocus = group.focusedChild
panel = group.findNode("options")
panel.visible = true
panel.findNode("panelList").setFocus(true)
end if
return true
end if

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="JFScene" extends="Scene">
<children>
<JFOverhang id="overhang" />
</children>
<interface>
<field id="backPressed" type="boolean" alwaysNotify="true" />
<field id="optionsPressed" type="boolean" alwaysNotify="true" />
<field id="exit" type="boolean" alwaysNotify="true" />
</interface>
<script type="text/brightscript" uri="JFScene.brs" />
</component>

144
components/data/GroupStack.brs Executable file
View File

@ -0,0 +1,144 @@
sub init()
m.groups = []
m.scene = m.top.getScene()
m.overhang = m.scene.findNode("overhang")
end sub
'
' Push a new group onto the stack, replacing the existing group on the screen
sub push(newGroup)
currentGroup = m.groups.peek()
if currentGroup <> invalid
'Search through group and store off last focused item
if currentGroup.focusedChild <> invalid
focused = currentGroup.focusedChild
while focused.hasFocus() = false
focused = focused.focusedChild
end while
currentGroup.lastFocus = focused
currentGroup.setFocus(false)
else
currentGroup.lastFocus = invalid
currentGroup.setFocus(false)
end if
if currentGroup.isSubType("JFGroup")
unregisterOverhangData(currentGroup)
end if
currentGroup.visible = false
end if
m.groups.push(newGroup)
'observe info about new group, set overhang title, etc.
if newGroup.isSubType("JFGroup")
registerOverhangData(newGroup)
' Some groups set focus to a specific component within init(), so we don't want to
' change if that is the case.
if newGroup.isInFocusChain() = false
newGroup.setFocus(true)
end if
else if newGroup.isSubType("JFVideo")
newGroup.setFocus(true)
newGroup.control = "play"
m.overhang.visible = false
end if
' TODO: figure out a better way to do this without relying on indexing
if currentGroup <> invalid
m.scene.replaceChild(newGroup, 1)
else
m.scene.appendChild(newGroup)
end if
end sub
'
' Remove the current group and load the last group from the stack
sub pop()
group = m.groups.pop()
if group <> invalid
if group.isSubType("JFGroup")
unregisterOverhangData(group)
else if group.isSubType("JFVideo")
' Stop video to make sure app communicates stop playstate to server
group.control = "stop"
end if
else
' Exit app if for some reason we don't have anything on the stack
m.scene.exit = true
end if
group = m.groups.peek()
if group <> invalid
registerOverhangData(group)
'm.scene.callFunc("registerOverhangData")
if group.subtype() = "Home"
currentTime = CreateObject("roDateTime").AsSeconds()
if group.timeLastRefresh = invalid or (currentTime - group.timeLastRefresh) > 20
group.timeLastRefresh = currentTime
group.callFunc("refresh")
end if
end if
group.visible = true
m.scene.replaceChild(group, 1)
' Restore focus
if group.lastFocus <> invalid
group.lastFocus.setFocus(true)
else
group.setFocus(true)
end if
else
' Exit app if the stack is empty after removing group
m.scene.exit = true
end if
end sub
'
' Return group at top of stack without removing
function peek() as object
return m.groups.peek()
end function
'
' Register observers for overhang data
sub registerOverhangData(group)
if group.isSubType("JFGroup")
if group.overhangTitle <> invalid then m.overhang.title = group.overhangTitle
if group.optionsAvailable
m.overhang.showOptions = true
else
m.overhang.showOptions = false
end if
group.observeField("overhangTitle", "updateOverhangTitle")
m.overhang.visible = true
else if group.isSubType("JFVideo")
m.overhang.visible = false
else
print "registerOverhangData(): Unexpected group type."
end if
end sub
'
' Remove observers for overhang data
sub unregisterOverhangData(group)
group.unobserveField("overhangTitle")
end sub
'
' Update overhang title
sub updateOverhangTitle(msg)
m.overhang.title = msg.getData()
end sub

9
components/data/GroupStack.xml Executable file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="GroupStack" extends="ContentNode">
<interface>
<function name="push" />
<function name="pop" />
<function name="peek" />
</interface>
<script type="text/brightscript" uri="GroupStack.brs" />
</component>

View File

@ -14,6 +14,7 @@ sub Main (args as dynamic) as void
m.screen.setMessagePort(m.port)
m.scene = m.screen.CreateScene("JFScene")
m.screen.show()
m.overhang = m.scene.findNode("overhang")
' Set any initial Global Variables
m.global = m.screen.getGlobalNode()
@ -21,30 +22,27 @@ sub Main (args as dynamic) as void
playstateTask = CreateObject("roSGNode", "PlaystateTask")
playstateTask.id = "playstateTask"
m.global.addFields({ app_loaded: false, playstateTask: playstateTask })
groupStack = CreateObject("roSGNode", "GroupStack")
m.overhang = CreateObject("roSGNode", "JFOverhang")
m.scene.insertChild(m.overhang, 0)
m.global.addFields({ app_loaded: false, playstateTask: playstateTask, groupStack: groupStack })
app_start:
m.overhang.title = ""
' First thing to do is validate the ability to use the API
' TODO: Should eventually switch the loginflow over to using the pushGroup() code. It's
' safe to leave for now because wipe_groups() ensures the equivalent to an empty group
' stack.
if not LoginFlow() then return
wipe_groups()
' load home page
m.overhang.title = tr("Home")
m.overhang.currentUser = m.user.Name
m.overhang.showOptions = true
group = CreateHomeGroup()
group.userConfig = m.user.configuration
group.callFunc("loadLibraries")
m.scene.appendChild(group)
groupStack.callFunc("push", group)
m.scene.observeField("backPressed", m.port)
m.scene.observeField("optionsPressed", m.port)
m.scene.observeField("mutePressed", m.port)
m.scene.observeField("exit", m.port)
' Handle input messages
input = CreateObject("roInput")
@ -53,20 +51,14 @@ sub Main (args as dynamic) as void
m.device = CreateObject("roDeviceInfo")
m.device.setMessagePort(m.port)
m.device.EnableScreensaverExitedEvent(true)
m.device.EnableAppFocusEvent(false)
' Check if we were sent content to play with the startup command (Deep Link)
if (args.mediaType <> invalid) and (args.contentId <> invalid)
video = CreateVideoPlayerGroup(args.contentId)
if video <> invalid
if group.lastFocus = invalid then group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
else
dialog = createObject("roSGNode", "Dialog")
dialog.id = "OKDialog"
@ -87,23 +79,10 @@ sub Main (args as dynamic) as void
if type(msg) = "roSGScreenEvent" and msg.isScreenClosed()
print "CLOSING SCREEN"
return
else if isNodeEvent(msg, "backPressed")
n = m.scene.getChildCount() - 1
if msg.getRoSGNode().focusedChild <> invalid and msg.getRoSGNode().focusedChild.isSubtype("JFVideo")
stopPlayback()
RemoveCurrentGroup()
else
if n = 1 then return
RemoveCurrentGroup()
end if
group = m.scene.getChild(n - 1)
else if isNodeEvent(msg, "optionsPressed")
group.lastFocus = group.focusedChild
panel = group.findNode("options")
panel.visible = true
panel.findNode("panelList").setFocus(true)
else if isNodeEvent(msg, "exit")
return
else if isNodeEvent(msg, "closeSidePanel")
group = groupStack.callFunc("peek")
if group.lastFocus <> invalid
group.lastFocus.setFocus(true)
else
@ -116,67 +95,30 @@ sub Main (args as dynamic) as void
if itemNode.type = "Episode" or itemNode.type = "Movie" or itemNode.type = "Video"
video = CreateVideoPlayerGroup(itemNode.id)
if video <> invalid
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
end if
end if
else if isNodeEvent(msg, "selectedItem")
' If you select a library from ANYWHERE, follow this flow
selectedItem = msg.getData()
if selectedItem.type = "CollectionFolder" or selectedItem.type = "UserView" or selectedItem.type = "Folder" or selectedItem.type = "Channel" or selectedItem.type = "Boxset"
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.title = selectedItem.title
group = CreateItemGrid(selectedItem)
group.overhangTitle = selectedItem.title
m.scene.appendChild(group)
groupStack.callFunc("push", group)
else if selectedItem.type = "Episode"
' play episode
' todo: create an episode page to link here
video_id = selectedItem.id
m.scene.unobserveField("optionsPressed")
video = CreateVideoPlayerGroup(video_id)
if video <> invalid
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
end if
else if selectedItem.type = "Series"
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.title = selectedItem.title
m.overhang.showOptions = false
m.scene.unobserveField("optionsPressed")
group = CreateSeriesDetailsGroup(selectedItem.json)
group.overhangTitle = selectedItem.title
m.scene.appendChild(group)
groupStack.callFunc("push", group)
else if selectedItem.type = "Movie"
' open movie detail page
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.title = selectedItem.title
m.overhang.showOptions = false
m.scene.unobserveField("optionsPressed")
group = CreateMovieDetailsGroup(selectedItem)
group.overhangTitle = selectedItem.title
m.scene.appendChild(group)
groupStack.callFunc("push", group)
else if selectedItem.type = "TvChannel" or selectedItem.type = "Video"
' play channel feed
video_id = selectedItem.id
@ -186,19 +128,11 @@ sub Main (args as dynamic) as void
dialog.title = tr("Loading Channel Data")
m.scene.dialog = dialog
m.scene.unobserveField("optionsPressed")
video = CreateVideoPlayerGroup(video_id)
dialog.close = true
if video <> invalid
if group.lastFocus = invalid then group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
else
dialog = createObject("roSGNode", "Dialog")
dialog.id = "OKDialog"
@ -215,62 +149,28 @@ sub Main (args as dynamic) as void
else if isNodeEvent(msg, "movieSelected")
' If you select a movie from ANYWHERE, follow this flow
node = getMsgPicker(msg, "picker")
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.title = node.title
m.overhang.showOptions = false
m.scene.unobserveField("optionsPressed")
group = CreateMovieDetailsGroup(node)
group.overhangTitle = node.title
m.scene.appendChild(group)
groupStack.callFunc("push", group)
else if isNodeEvent(msg, "seriesSelected")
' If you select a TV Series from ANYWHERE, follow this flow
node = getMsgPicker(msg, "picker")
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.title = node.title
m.overhang.showOptions = false
m.scene.unobserveField("optionsPressed")
group = CreateSeriesDetailsGroup(node)
group.overhangTitle = node.title
m.scene.appendChild(group)
groupStack.callFunc("push", group)
else if isNodeEvent(msg, "seasonSelected")
' If you select a TV Season from ANYWHERE, follow this flow
ptr = msg.getData()
' ptr is for [row, col] of selected item... but we only have 1 row
series = msg.getRoSGNode()
node = series.seasonData.items[ptr[1]]
group.lastFocus = group.focusedChild.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.title = series.overhangTitle + " - " + node.title
m.overhang.showOptions = false
m.scene.unobserveField("optionsPressed")
group = CreateSeasonDetailsGroup(series.itemContent, node)
m.scene.appendChild(group)
groupStack.callFunc("push", group)
else if isNodeEvent(msg, "episodeSelected")
' If you select a TV Episode from ANYWHERE, follow this flow
node = getMsgPicker(msg, "picker")
video_id = node.id
m.scene.unobserveField("optionsPressed")
video = CreateVideoPlayerGroup(video_id)
if video <> invalid
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
end if
else if isNodeEvent(msg, "search_value")
query = msg.getRoSGNode().search_value
@ -289,10 +189,6 @@ sub Main (args as dynamic) as void
else if isNodeEvent(msg, "itemSelected")
' Search item selected
node = getMsgPicker(msg)
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
' TODO - swap this based on target.mediatype
' types: [ Series (Show), Episode, Movie, Audio, Person, Studio, MusicArtist ]
if node.type = "Series"
@ -300,9 +196,7 @@ sub Main (args as dynamic) as void
else
group = CreateMovieDetailsGroup(node)
end if
m.scene.appendChild(group)
m.overhang.title = group.overhangTitle
groupStack.callFunc("push", group)
else if isNodeEvent(msg, "buttonSelected")
' If a button is selected, we have some determining to do
btn = getButton(msg)
@ -318,14 +212,7 @@ sub Main (args as dynamic) as void
video_id = group.id
video = CreateVideoPlayerGroup(video_id, audio_stream_idx)
if video <> invalid
group.lastFocus = group.focusedChild.focusedChild.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
end if
else if btn <> invalid and btn.id = "watched-button"
movie = group.itemContent
@ -361,14 +248,8 @@ sub Main (args as dynamic) as void
else
group.setFocus(true)
end if
group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
m.overhang.showOptions = false
m.scene.unobserveField("optionsPressed")
group = CreateSearchPage()
m.scene.appendChild(group)
m.overhang.title = group.overhangTitle
groupStack.callFunc("push", group)
group.findNode("SearchBox").findNode("search-input").setFocus(true)
group.findNode("SearchBox").findNode("search-input").active = true
else if button.id = "change_server"
@ -403,12 +284,11 @@ sub Main (args as dynamic) as void
else if isNodeEvent(msg, "state")
node = msg.getRoSGNode()
if node.state = "finished"
stopPlayback()
node.control = "stop"
if node.showID = invalid
RemoveCurrentGroup()
groupStack.callFunc("pop")
else
nextEpisode = autoPlayNextEpisode(node.id, node.showID)
if nextEpisode <> invalid then group = nextEpisode
autoPlayNextEpisode(node.id, node.showID)
end if
end if
else if type(msg) = "roDeviceInfoEvent"
@ -431,14 +311,7 @@ sub Main (args as dynamic) as void
if info.DoesExist("mediatype") and info.DoesExist("contentid")
video = CreateVideoPlayerGroup(info.contentId)
if video <> invalid
if group.lastFocus = invalid then group.lastFocus = group.focusedChild
group.setFocus(false)
group.visible = false
group = video
m.scene.appendChild(group)
group.setFocus(true)
group.control = "play"
m.overhang.visible = false
groupStack.callFunc("push", video)
else
dialog = createObject("roSGNode", "Dialog")
dialog.id = "OKDialog"
@ -575,38 +448,6 @@ sub wipe_groups()
end while
end sub
sub RemoveCurrentGroup()
' Pop a group off the stack and expose what's below
n = m.scene.getChildCount() - 1
group = m.scene.focusedChild
m.scene.removeChildIndex(n)
prevOptionsAvailable = group.optionsAvailable
group = m.scene.getChild(n - 1)
m.overhang.title = group.overhangTitle
m.overhang.showOptions = group.optionsAvailable
if group.optionsAvailable <> prevOptionsAvailable
if group.optionsAvailable = false
m.scene.unobserveField("optionsPressed")
else
m.scene.observeField("optionsPressed", m.port)
end if
end if
m.overhang.visible = true
if group.lastFocus <> invalid
group.lastFocus.setFocus(true)
else
group.setFocus(true)
end if
if group.subtype() = "Home"
currentTime = CreateObject("roDateTime").AsSeconds()
if group.timeLastRefresh = invalid or (currentTime - group.timeLastRefresh) > 20
group.timeLastRefresh = currentTime
group.callFunc("refresh")
end if
end if
group.visible = true
end sub
' Roku Performance monitoring
sub SendPerformanceBeacon(signalName as string)
if m.global.app_loaded = false

View File

@ -174,6 +174,8 @@ end function
function CreateHomeGroup()
' Main screen after logging in. Shows the user's libraries
group = CreateObject("roSGNode", "Home")
group.overhangTitle = tr("Home")
group.optionsAvailable = true
group.observeField("selectedItem", m.port)
group.observeField("quickPlayNode", m.port)
@ -230,6 +232,8 @@ end function
function CreateMovieDetailsGroup(movie)
group = CreateObject("roSGNode", "MovieDetails")
group.overhangTitle = movie.title
group.optionsAvailable = false
movie = ItemMetaData(movie.id)
group.itemContent = movie
@ -244,6 +248,8 @@ end function
function CreateSeriesDetailsGroup(series)
group = CreateObject("roSGNode", "TVShowDetails")
group.overhangTitle = series.title
group.optionsAvailable = false
group.itemContent = ItemMetaData(series.id)
group.seasonData = TVSeasons(series.id)
@ -255,6 +261,8 @@ end function
function CreateSeasonDetailsGroup(series, season)
group = CreateObject("roSGNode", "TVEpisodes")
group.overhangTitle = series.title + " " + season.title
group.optionsAvailable = false
group.seasonData = ItemMetaData(season.id).json
group.objects = TVEpisodes(series.id, season.id)
@ -268,6 +276,8 @@ end function
function CreateItemGrid(libraryItem)
group = CreateObject("roSGNode", "ItemGrid")
group.parentItem = libraryItem
group.overhangTitle = libraryItem.title
group.optionsAvailable = true
group.observeField("selectedItem", m.port)
return group
end function

View File

@ -208,13 +208,7 @@ function getAudioInfo(meta as object) as object
return results
end function
sub StopPlayback()
video = m.scene.focusedchild
video.control = "stop"
m.device.EnableAppFocusEvent(False)
end sub
function autoPlayNextEpisode(videoID as string, showID as string)
sub autoPlayNextEpisode(videoID as string, showID as string)
' use web client setting
if m.user.Configuration.EnableNextEpisodeAutoPlay
' query API for next episode ID
@ -227,20 +221,19 @@ function autoPlayNextEpisode(videoID as string, showID as string)
if data <> invalid and data.Items.Count() = 2
' remove finished video node
n = m.scene.getChildCount() - 1
m.scene.removeChildIndex(n)
m.global.groupStack.callFunc("pop")
' setup new video node
nextVideo = CreateVideoPlayerGroup(data.Items[1].Id)
m.scene.appendChild(nextVideo)
nextVideo.setFocus(true)
nextVideo.control = "play"
return nextVideo
if nextVideo <> invalid
m.global.groupStack.callFunc("push", nextVideo)
else
m.global.groupStack.callFunc("pop")
end if
else
' can't play next episode
RemoveCurrentGroup()
m.global.groupStack.callFunc("pop")
end if
else
RemoveCurrentGroup()
m.global.groupStack.callFunc("pop")
end if
return invalid
end function
end sub

14
source/utils/sceneManager.brs Executable file
View File

@ -0,0 +1,14 @@
'
' Create a SceneManager instance to help manage application groups
function InitSceneManager(scene as object) as object
' Create a node object so data is passed by reference and to avoid
' having to re-save associative array in global variable.
groupStack = CreateObject("roSGNode", "GroupStack")
obj = {
groupStack: groupStack,
pushGroup: sub(group) : m.groupStack.callFunc("push", group) : end sub,
popGroup: sub() : m.groupStack.callFunc("pop") : end sub,
peekGroup: function() : return m.groupStack.callFunc("peek") : end function
}
return obj
end function