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/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs" import "pkg:/source/AsyncTask/AsyncTask.bs" import "pkg:/source/AsyncTask/Tasks.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. Subscriptions, Watch History and Playlists can be empty 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.videoQueue.playVideo = rowItem 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 LogWarn("Unknown row item type:", rowItem.type) 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.bookmarksService, m.top.screen] ContextMenuUtils.Open(contextMenuProviders, m.appController, rowItem) end function function GetContextMenuOptionsForItem(rowItem as object) as object options = [] if rowItem.type = "video" options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.PlayVideo), m.videoQueue, "FuncPlayVideo", [rowItem])) options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.QueueVideo), m.videoQueue, "FuncAddToQueueVideo", [rowItem])) authorId = rowItem.authorId if not StringUtils.IsNullOrEmpty(authorId) options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.OpenChannel), m.top, "OpenVideoChannel", [rowItem])) end if else if rowItem.type = "playlist" options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.PlayPlaylist), m.videoQueue, "FuncPlayPlaylist", [rowItem, 0])) options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.QueuePlaylist), m.videoQueue, "FuncAddToQueuePlaylist", [rowItem])) options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.OpenPlaylist), m.top, "OpenPlaylist", [rowItem])) authorId = rowItem.authorId if not StringUtils.IsNullOrEmpty(authorId) options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.OpenChannel), m.top, "OpenPlaylistChannel", [rowItem])) end if else if rowItem.type = "channel" options.push(ContextMenuUtils.CreateOption(Tr(Locale.ContextMenu.OpenChannel), m.top, "OpenChannel", [rowItem])) 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 }) ChannelUtils.Open(channel) end function function OpenPlaylistChannel(playlist as object) as void authorId = playlist.authorId if StringUtils.IsNullOrEmpty(authorId) LogWarn("Invalid authorId:", authorId) return end if channel = InvidiousContent.ToChannelContentNode(invalid, { authorId: authorId }) ChannelUtils.Open(channel) end function function Refresh(_unused 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() if m.top.feeds = invalid m.top.content = invalid return end if task = AsyncTask.Start(Tasks.VideoRowListContentTask, { node: m.top feeds: m.top.feeds }, OnVideoRowListContentTaskResult) m.pendingLoadTasks[task.id] = { task: task } end function function CancelCurrentTasks() pendingLoadTasks = m.pendingLoadTasks for each id in pendingLoadTasks task = pendingLoadTasks[id].task 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) as void ' Do not spin up multiple tasks for the same content node for each id in m.pendingLoadTasks pendingTask = m.pendingLoadTasks[id] if contentNode.isSameNode(pendingTask.content) and pendingTask.task.state = "run" LogInfo("Row content already loading:", contentNode.title) return end if end for LogInfo("Loading row content:", contentNode.title) contentNode.loadState = FeedLoadState.Loading task = AsyncTask.Start(Tasks.VideoRowListRowContentTask, { rowList: m.top feedContentNode: contentNode invidious: m.invidious }, OnVideoRowListRowContentTaskResult) m.pendingLoadTasks[task.id] = { task: task, content: contentNode } 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