import "pkg:/components/ChannelView/ChannelUtils.bs" import "pkg:/components/ContextMenu/ContextMenuUtils.bs" import "pkg:/components/Navigation/LongPress.bs" import "pkg:/components/PlaylistView/PlaylistUtils.bs" import "pkg:/components/VideoFeed/VideoRowListContentTask.bs" import "pkg:/components/VideoFeed/VideoRowListRowContentTask.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/utils/DisposableObserve.bs" import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Types.bs" ' TODO:P1 add the option to remove a row if it does not have any content ' E.g. NBC News does not return any related chnanels ' The "Continue Watching" feed source might return nothing. In this case, ' it should be removed. function Init() m.pendingLoadTasks = {} end function function OnNodeReady() DisposableObserveField(m.top, "itemFocused", FuncName(OnItemFocused)) DisposableObserveField(m.top, "rowItemFocused", FuncName(OnRowItemFocused)) DisposableObserveField(m.top, "focusedChild", FuncName(OnFocusedChildChange)) DisposableObserveField(m.top, "visible", FuncName(OnVisibilityChange)) ' TODO:P1 Reload when subscriptions change DisposableObserveFieldScoped(m.invidious, "authToken", FuncName(OnAuthTokenChange)) InitializeLongPress(["OK"]) end function function OnFocusedChildChange() as void if m.top.hasFocus() LoadRowsIfNeeded() end if end function function OnVisibilityChange() if m.top.visible = true LoadRowsIfNeeded() end if end function function OnFeedsChange() as void InitContent() end function function OnItemFocused() LoadRowsIfNeeded() end function function OnRowItemFocused(event as object) as void index = event.GetData() if index = invalid or index.count() <> 2 LogWarn("Invalid index:", index) return end if rowIndex = index[0] rowItemIndex = index[1] row = m.top.content.GetChild(rowIndex) if not row.loadState = FeedLoadState.LoadedPage return end if ' As we get close to the end of the row, load more content if rowItemIndex > row.getChildCount() - 5 LoadRowContent(row) end if end function function OnRowItemSelected(index as object) as void content = m.top.content if content = invalid return end if if index = invalid or index.count() <> 2 LogWarn("Invalid index:", index) return end if rowIndex = index[0] rowItemIndex = index[1] if rowIndex < 0 or rowIndex >= content.GetChildCount() LogWarn("Invalid row index:", rowIndex) return end if row = content.GetChild(rowIndex) if rowItemIndex < 0 or rowItemIndex >= row.GetChildCount() LogWarn("Invalid row item index:", rowItemIndex) return end if rowItem = row.GetChild(rowItemIndex) if rowItem.type = "video" videoId = rowItem.videoId LogInfo("Play video: " + videoId) m.playQueue@.Play(rowItem, -1) else if rowItem.type = "playlist" playlistId = rowItem.playlistId LogInfo("Open playlist: " + playlistId) OpenPlaylist(rowItem) else if rowItem.type = "channel" authorId = rowItem.authorId LogInfo("Open channel: " + authorId) OpenChannel(rowItem) else if rowItem.type = "action" if rowItem.action = "LoginDialog" dialog = CreateObject("roSGNode", "LoginDialog") dialog@.BindNode() m.top.getScene().dialog = dialog end if end if end function function OnRowItemLongPressed(index as object) as void content = m.top.content if content = invalid return end if if not IsArray(index) or index.count() <> 2 LogWarn("Invalid index:", index) return end if rowIndex = index[0] rowItemIndex = index[1] if not IsInt(rowIndex) or rowIndex < 0 or rowIndex >= content.GetChildCount() LogWarn("Invalid row index:", rowIndex) return end if row = content.GetChild(rowIndex) if not IsInt(rowItemIndex) or rowItemIndex < 0 or rowItemIndex >= row.GetChildCount() LogWarn("Invalid row item index:", rowItemIndex) return end if rowItem = row.GetChild(rowItemIndex) contextMenuProviders = [m.top, m.bookmarks, m.top.screen] ContextMenuUtils.Open(contextMenuProviders, m.appController, rowItem) end function function GetContextMenuOptionsForItem(rowItem as object) as object options = [] playAction = ContextMenuUtils.CreateOption("Play", m.playQueue, "Play", [rowItem, -1]) queueAction = ContextMenuUtils.CreateOption("Queue", m.playQueue, "AddToQueue", [rowItem]) if rowItem.type = "video" options.push(playAction) options.push(queueAction) item = ContextMenuUtils.CreateOption("Open channel", m.top, "OpenVideoChannel", [rowItem]) options.push(item) else if rowItem.type = "playlist" options.push(playAction) options.push(queueAction) item = ContextMenuUtils.CreateOption("Open", m.top, "OpenPlaylist", [rowItem]) options.push(item) else if rowItem.type = "channel" item = ContextMenuUtils.CreateOption("Open", m.top, "OpenChannel", [rowItem]) options.push(item) else LogWarn("Unknown long pressed item type:", rowItem.type) return [] end if return options end function function OpenPlaylist(playlist as object) PlaylistUtils.Open(playlist) end function function OpenChannel(channel as object) ChannelUtils.Open(channel) end function function OpenVideoChannel(video as object) as void authorId = video.authorId if StringUtils.IsNullOrEmpty(authorId) LogWarn("Invalid authorId:", authorId) return end if channel = InvidiousContent.ToChannelContentNode(invalid, { authorId: authorId }, invalid) ChannelUtils.Open(channel) end function function Refresh(unsued as dynamic) InitContent() end function function OnAuthTokenChange() ' TODO:P2: Reload only content that is authenticated ' Should not reload if the page is not visible. Instead, capture state and reload when the page becomes visible InitContent() end function function InitContent() as void CancelCurrentTasks() m.top.someContentReady = false if m.top.feeds = invalid m.top.content = invalid return end if task = StartAsyncTask(VideoRowListContentTask, { node: m.top feeds: m.top.feeds }, OnVideoRowListContentTaskResult) m.pendingLoadTasks[task.id] = task end function function CancelCurrentTasks() pendingLoadTasks = m.pendingLoadTasks for each id in pendingLoadTasks task = pendingLoadTasks[id] task.cancel = true end for m.pendingLoadTasks = {} end function function OnVideoRowListContentTaskResult(output as object) as void m.pendingLoadTasks.Delete(output.task.id) if output.cancelled return end if LoadRowsIfNeeded() end function function LoadRowsIfNeeded() as void if m.top.content = invalid or not m.top.visible return end if itemsToLoad = GetRowsToLoad() for each item in itemsToLoad LoadRowContent(item) end for end function function GetRowsToLoad() as object focus = m.top.itemFocused if focus = -1 focus = 0 end if contentNode = m.top.content rowCount = contentNode.getChildCount() if rowCount = 0 return [] end if subContentNodes = [] range = m.top.numRows + 1 for i = -range to range index = focus + i if index < 0 continue for end if if index >= rowCount continue for end if subContentNode = contentNode.getChild(index) if subContentNode.loadState <> FeedLoadState.None continue for end if subContentNodes.push(subContentNode) end for return subContentNodes end function function LoadRowContent(contentNode as object) LogInfo("Loading row content:", contentNode.title) contentNode.loadState = FeedLoadState.Loading task = StartAsyncTask(VideoRowListRowContentTask, { rowList: m.top feedContentNode: contentNode invidious: m.invidious }, OnVideoRowListRowContentTaskResult) m.pendingLoadTasks[task.id] = task end function function OnVideoRowListRowContentTaskResult(output as object) as void m.pendingLoadTasks.Delete(output.task.id) if output.cancelled return end if LoadRowsIfNeeded() end function function OnkeyEvent(key as string, press as boolean) as boolean if LongPressHandler(key, press) return true end if if key = OK_LONG_PRESS_KEY and press OnRowItemLongPressed(m.top.rowItemFocused) return true end if if key = "OK" and not press OnRowItemSelected(m.top.rowItemFocused) return true end if return false end function function OnDispose() CancelCurrentTasks() DisposeObservers() m.top.screen = invalid m.top.content = invalid m.top.itemComponentName = invalid end function