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
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="HomeGridScreen" extends="Group" initialFocus="RowList">
<interface>
<field id="content" type="node" alias="RowList.content" />
<field id="rowItemSelected" type="intarray" alwaysnotify="true" alias="RowList.rowItemSelected" />
<field id="rowItemFocused" type="intarray" alwaysnotify="true" alias="RowList.rowItemFocused" />
<field id="jumpToRowItem" type="intarray" alias="RowList.jumpToRowItem" />
<field id="escape" type="string" alwaysNotify="true" />
<field id="focus" type="boolean" alwaysNotify="true" />
</interface>
<children>
<!-- TODO: Fix layout where viewCount label disappears if title is two lines -->
<RowList
itemComponentName="VideoRowCell"
id="rowList"
translation="[0,50]"
numRows="2"
rowitemSize="[[350,196]]"
rowItemSpacing="[[25,0]]"
itemSize="[1280,326]"
itemSpacing="[0,65]"
rowLabelOffset="[[95,20]]"
focusXOffset="[95]"
showRowLabel="[true]"
rowFocusAnimationStyle="floatingfocus"
focusBitmapUri="pkg:/images/focus_grid.9.png"
/>
</children>
</component>
@@ -0,0 +1,82 @@
import "pkg:/source/services/InvidiousService.bs"
function Init()
m.top.functionName = "TaskMain"
end function
function TaskMain()
rootChildren = []
invidious = new InvidiousService()
input = m.top.getField("input")
requestData = input.requestData
response = invidious.MakeRequest(requestData)
if response <> invalid
if type(response) = "roAssociativeArray"
for each key in response
rowContent = GetCategoryContent(key, response[key])
if rowContent <> invalid
rootChildren.Push(rowContent)
end if
end for
else
rowContent = GetCategoryContent(requestData.title, response)
if rowContent <> invalid
rootChildren.Push(rowContent)
end if
end if
end if
m.top.setField("output", {
index: input.index,
rootChildren: rootChildren
})
end function
function GetCategoryContent(category as string, json as object) as object
if json <> invalid and json.Count() > 0
row = {}
row.title = category
row.children = []
for each item in json ' parse items and push them to row
itemData = GetItemData(item)
if itemData <> invalid
row.children.Push(itemData)
end if
end for
return row
end if
return invalid
end function
function GetItemData(video as object) as object
' TODO: handle playlists and channels
if video.videoId = invalid
return invalid
end if
item = {}
item.title = video.title
item.description = video.description
item.hdPosterURL = GetThumbnailUrl(video.videoThumbnails)
item.releaseDate = video.publishedText
item.id = video.videoId
item.length = video.lengthSeconds
item.author = video.author
item.viewCount = video.viewCount
return item
end function
function GetThumbnailUrl(videoThumbnails as object, quality = "medium" as string) as string
if videoThumbnails.Count() = 0
return ""
end if
for each thumbnail in videoThumbnails
if thumbnail.quality = quality
return thumbnail.url
end if
end for
return videoThumbnails[0].url
end function
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="HomeGridScreenTask" extends="Task">
<interface>
<field id="input" type="assocarray" />
<field id="output" type="assocarray" />
</interface>
</component>