Refactor project

This commit is contained in:
Brahim Hadriche committed 2022-11-18 13:04:20 -05:00
1 parent 9c1a2b987b
commit 2f2516eade
115 files changed
+12522

No files matched your search

@@ -0,0 +1,181 @@
import "pkg:/source/roku_modules/rokupromise/Promise.brs"
import "pkg:/components/VideoPlayer/Video.bs"
function Init()
m.rowList = m.top.FindNode("rowList")
m.top.ObserveField("rowItemSelected", "OnHomeGridScreenVideoSelected")
m.top.ObserveField("focus", "OnFocusChange")
m.top.focus = true
InitContent()
end function
function OnFocusChange()
if m.top.focus = true
m.rowList.SetFocus(true)
end if
end function
function InitContent()
m.sortedContent = []
contentNode = CreateObject("roSGNode", "ContentNode")
contentNode.Update({
children: []
}, true)
m.top.content = contentNode
m.defaultLayout = ParseJson(ReadAsciiFile("pkg:/config/default_home_layout.json"))
if m.defaultLayout.Count() > 0
m.currentLoadingIndex = 0
' 3 parallel tasks
for x = 1 to 3
StartFetchTask()
end for
end if
end function
function StartFetchTask() as void
currentLoadingIndex = m.currentLoadingIndex
m.currentLoadingIndex++
if currentLoadingIndex >= m.defaultLayout.Count()
if m.sortedContent.Count() = m.defaultLayout.Count()
' All tasks are done
m.Delete("sortedContent")
' TODO: uncommment if auto-play
' m.top.ObserveField("rowItemFocused", "OnHomeGridScreenVideoFocused")
end if
return
end if
createTaskPromise("HomeGridScreenTask", {
input: {
index: currentLoadingIndex,
requestData: m.defaultLayout[currentLoadingIndex]
}
}).then(function(task as object)
' TODO: this whole logic is not good.
' The view should have placeholder, and child nodes get replaced as
' They get ready.
m.sortedContent.Push(task.output)
if task.output.rootChildren.Count() > 0
m.sortedContent.SortBy("index")
newChildren = []
for each item in m.sortedContent
if item.rootChildren.Count() > 0
newChildren.append(item.rootChildren)
end if
end for
currentChildrenCount = m.top.content.children.Count()
while m.top.content.getChildCount() > 0
m.top.content.removeChildIndex(0)
end while
m.top.content.Update({
children: newChildren
}, true)
if currentChildrenCount = 0 and newChildren.Count() > 0
OnHomeGridScreenContentLoaded()
end if
end if
StartFetchTask()
end function)
end function
function OnHomeGridScreenContentLoaded()
m.top.SetFocus(true)
m.global.loadingIndicator.visible = false
end function
function OnHomeGridScreenVideoSelected(event as object) as void
grid = event.GetRoSGNode()
selectedPair = event.GetData()
selectedRow = selectedPair[0]
selectedRowItem = selectedPair[1]
rowContent = grid.content.GetChild(selectedRow)
itemContent = rowContent.GetChild(selectedRowItem)
videoId = itemContent.id
if m.videoPlayer <> invalid
if m.videoPlayer.videoId = videoId
ExpandVideoScreenFull()
return
else
m.videoPlayer.control = "stop"
m.videoPlayer.visible = false
m.videoPlayer.getParent().removeChild(m.videoPlayer)
m.videoPlayer = invalid
end if
end if
ShowVideoScreenFull(videoId)
end function
function CloseVideo()
if m.videoPlayer <> invalid
m.videoPlayer.control = "stop"
m.videoPlayer.visible = false
m.videoPlayer.getParent().removeChild(m.videoPlayer)
m.videoPlayer = invalid
end if
end function
function OnHomeGridScreenVideoFocused(event as object) as void
' Skip loading the video after first load
if m.firstFocus = invalid
m.firstFocus = true
return
end if
if m.videoPlayer <> invalid
m.videoPlayer.control = "stop"
m.videoPlayer.visible = false
m.videoPlayer.getParent().removeChild(m.videoPlayer)
m.videoPlayer = invalid
end if
grid = event.GetRoSGNode()
selectedPair = event.GetData()
selectedRow = selectedPair[0]
selectedRowItem = selectedPair[1]
rowContent = grid.content.GetChild(selectedRow)
itemContent = rowContent.GetChild(selectedRowItem)
videoId = itemContent.id
thumbnail = GetThumbnailPoster(videoId)
ShowVideoScreenEmbedded(videoId, thumbnail)
end function
function GetThumbnailPoster(videoId as string) as object
' TODO: make this faster
all = m.top.getAll()
for each item in all
if item.id = `thumbnail-${videoId}`
return item
end if
end for
return invalid
end function
function OnkeyEvent(key as string, press as boolean) as boolean
if press = false
return false
end if
if key = "left" or key = "back"
m.top.escape = key
return true
end if
return false
end function