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.ObserveField("playvideo", "OnPlayVideoCommand") m.top.ObserveField("refresh", "OnRefresh") m.top.focus = true InitContent() end function function OnFocusChange() if m.top.focus = true m.rowList.SetFocus(true) end if end function function OnRefresh() InitContent() 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") 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.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.videoId PlayVideo(videoId, m.top) end function function OnPlayVideoCommand() videoId = m.top.playvideo PlayVideo(videoId) end function function OnkeyEvent(key as string, press as boolean) as boolean if press = false return false end if if key = "options" if ToggleVideoPictureInPicture(m.top) return true end if end if if key = "left" or key = "back" m.top.escape = key return true end if return false end function