From 3559a3fff64fb7a393be847ca64e78ba38e22f9f Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 12 Nov 2023 17:45:00 -0500 Subject: [PATCH] Queue UI (#199) * Refactor long press * Lint fix * Queue UI, refactor PlayQueue to use ContentNode * Add queue notification hint * Play at index * Play queue with highlight * Priority * Comment --------- Co-authored-by: github-action linter --- CHANGELOG.md | 6 + .../components/AppController/AppController.bs | 20 +++- .../AppController/AppController.xml | 1 + playlet-lib/src/components/MainScene.xml | 5 +- .../MainScene_bindings.transpiled.brs | 3 +- .../src/components/Navigation/LongPress.bs | 70 +++++++++--- .../NodeByRef/ContentNodeReference.xml | 5 + .../src/components/NodeByRef/NodeByRef.bs | 41 +++++++ .../Notifications/PlayQueueNotification.bs | 2 + .../Notifications/PlayQueueNotification.xml | 8 +- .../src/components/PlayQueue/PlayQueue.bs | 106 ++++++++++++------ .../src/components/PlayQueue/PlayQueue.xml | 8 +- .../src/components/PlayQueue/PlayQueueView.bs | 83 ++++++++++++++ .../components/PlayQueue/PlayQueueView.xml | 63 +++++++++++ .../components/PlayQueue/PlayQueueViewCell.bs | 75 +++++++++++++ .../PlayQueue/PlayQueueViewCell.xml | 50 +++++++++ .../PlayQueue/PlayQueueViewUtils.bs | 15 +++ .../components/PlaylistView/PlaylistView.bs | 8 +- .../PlaylistView/PlaylistViewCell.bs | 2 +- .../VideoFeed/VideoRowCell/PlaylistRowCell.bs | 2 +- .../VideoFeed/VideoRowCell/VideoRowCell.bs | 2 +- .../src/components/VideoFeed/VideoRowList.bs | 4 +- .../components/VideoPlayer/VideoContainer.bs | 2 + .../src/components/VideoPlayer/VideoPlayer.bs | 6 +- 24 files changed, 523 insertions(+), 64 deletions(-) create mode 100644 playlet-lib/src/components/NodeByRef/ContentNodeReference.xml create mode 100644 playlet-lib/src/components/NodeByRef/NodeByRef.bs create mode 100644 playlet-lib/src/components/PlayQueue/PlayQueueView.bs create mode 100644 playlet-lib/src/components/PlayQueue/PlayQueueView.xml create mode 100644 playlet-lib/src/components/PlayQueue/PlayQueueViewCell.bs create mode 100644 playlet-lib/src/components/PlayQueue/PlayQueueViewCell.xml create mode 100644 playlet-lib/src/components/PlayQueue/PlayQueueViewUtils.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 25535707..4e08108c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- User interface for the Queue: Press and hold `Options (*)` to show the queue + ## [0.15.1] - 2023-11-10 ### Added diff --git a/playlet-lib/src/components/AppController/AppController.bs b/playlet-lib/src/components/AppController/AppController.bs index a89c4ee8..8f33caec 100644 --- a/playlet-lib/src/components/AppController/AppController.bs +++ b/playlet-lib/src/components/AppController/AppController.bs @@ -1,4 +1,6 @@ import "pkg:/components/Dialog/DialogUtils.bs" +import "pkg:/components/Navigation/LongPress.bs" +import "pkg:/components/PlayQueue/PlayQueueViewUtils.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/Logging.bs" @@ -10,6 +12,7 @@ function Init() end function function OnNodeReady() + InitializeLongPress(["options"]) NodeSetFocus(m.top.root, true) end function @@ -78,15 +81,26 @@ function GetRootScreen(screenName as string) as object end function function onKeyEvent(key as string, press as boolean) as boolean - if not press - return false + if LongPressHandler(key, press) + return true end if - if key = "options" + + if key = OPTIONS_LONG_PRESS_KEY and press + LogInfo("Opening PlayQueueView") + PlayQueueViewUtils.Open(m.top.playQueue, m.top) + return true + end if + + if key = "options" and not press if VideoUtils.ToggleVideoPictureInPicture() return true end if end if + if not press + return false + end if + if key = "play" if VideoUtils.TogglePause() return true diff --git a/playlet-lib/src/components/AppController/AppController.xml b/playlet-lib/src/components/AppController/AppController.xml index 4fc7b5df..d3191815 100644 --- a/playlet-lib/src/components/AppController/AppController.xml +++ b/playlet-lib/src/components/AppController/AppController.xml @@ -2,6 +2,7 @@ + diff --git a/playlet-lib/src/components/MainScene.xml b/playlet-lib/src/components/MainScene.xml index e136dc96..8b992195 100644 --- a/playlet-lib/src/components/MainScene.xml +++ b/playlet-lib/src/components/MainScene.xml @@ -2,7 +2,10 @@ - + diff --git a/playlet-lib/src/components/MainScene_bindings.transpiled.brs b/playlet-lib/src/components/MainScene_bindings.transpiled.brs index ddd39e4b..b6caccce 100644 --- a/playlet-lib/src/components/MainScene_bindings.transpiled.brs +++ b/playlet-lib/src/components/MainScene_bindings.transpiled.brs @@ -7,7 +7,8 @@ function InitializeBindings() childProps: { "AppController": { "root": "./AppRoot", - "stack": "./Stack" + "stack": "./Stack", + "playQueue": "./PlayQueue" }, "NavBar": { "appController": "/AppController" diff --git a/playlet-lib/src/components/Navigation/LongPress.bs b/playlet-lib/src/components/Navigation/LongPress.bs index db9ed604..df658c64 100644 --- a/playlet-lib/src/components/Navigation/LongPress.bs +++ b/playlet-lib/src/components/Navigation/LongPress.bs @@ -1,41 +1,79 @@ -const LONG_PRESS_KEY = "OK_LONG_PRESS" +const OK_LONG_PRESS_KEY = "OK_LONG_PRESS" +const OPTIONS_LONG_PRESS_KEY = "OPTIONS_LONG_PRESS" + +function InitializeLongPress(keys as object, duration = 1.0 as float) + longPressKeyMap = { + "OK": OK_LONG_PRESS_KEY, + "options": OPTIONS_LONG_PRESS_KEY + } + m._longPressKeyMap = {} + for each key in keys + m._longPressKeyMap[key] = longPressKeyMap[key] + end for + + #if DEBUG + ' This is for testing long press using the VSCode extension. + ' Using the keyboard: + ' - press K to simulate an "OK" long press on the remote. + ' - press O to simulate an "options" long press on the remote. + simulatedLongPressKeyMap = { + "OK": "Lit_k", + "options": "Lit_o" + } + + m._simulatedLongPressKeyMap = {} + for each key in keys + m._simulatedLongPressKeyMap[simulatedLongPressKeyMap[key]] = key + end for + #end if -function InitializeLongPressTimer(duration = 1.0 as float) m._longPressTimer = CreateObject("roSGNode", "Timer") m._longPressTimer.duration = duration m._longPressFired = false + m._pressState = {} + m._longPressTimer.ObserveField("fire", FuncName(LongPressTimerFired)) end function function LongPressHandler(key as string, press as boolean) as boolean #if DEBUG - if key = "Lit_l" - LogInfo("Simulating long press using key:", key, "press:", press) + if m._simulatedLongPressKeyMap.DoesExist(key) + subtype = m.top.subtype() + simulatedKey = m._simulatedLongPressKeyMap[key] + LogInfo(`[${subtype}] Simulating`, simulatedKey, "long press using key:", key, "press:", press) + longPressKey = m._longPressKeyMap[simulatedKey] m._longPressFired = true - return OnkeyEvent(LONG_PRESS_KEY, press) + return OnkeyEvent(longPressKey, press) end if #end if - if key = "OK" and press + + isLongPressKey = m._longPressKeyMap.DoesExist(key) + if not isLongPressKey + return false + end if + + if press m._longPressTimer.control = "stop" m._longPressTimer.control = "start" m._longPressFired = false - m._okPressed = true + m._longPressKey = key + m._pressState[key] = true return true end if - if key = "OK" and not press + if not press m._longPressTimer.control = "stop" if m._longPressFired = true m._longPressFired = false - m._okPressed = false + m._pressState[key] = false return true end if ' This case is for handling a dialog: The dialog will be dismissed with the OK down event, ' and we would trigger the action with the OK up event. The action should only happen if ' we detected both down and up events. - wasOkPressed = m._okPressed - m._okPressed = false - if wasOkPressed <> true + wasPressed = m._pressState[key] + m._pressState[key] = false + if wasPressed <> true return true end if end if @@ -43,7 +81,11 @@ function LongPressHandler(key as string, press as boolean) as boolean return false end function -function LongPressTimerFired() +function LongPressTimerFired() as void m._longPressFired = true - OnkeyEvent(LONG_PRESS_KEY, true) + if StringUtils.IsNullOrEmpty(m._longPressKey) or not m._longPressKeyMap.DoesExist(m._longPressKey) + return + end if + + OnkeyEvent(m._longPressKeyMap[m._longPressKey], true) end function diff --git a/playlet-lib/src/components/NodeByRef/ContentNodeReference.xml b/playlet-lib/src/components/NodeByRef/ContentNodeReference.xml new file mode 100644 index 00000000..0b1dbecc --- /dev/null +++ b/playlet-lib/src/components/NodeByRef/ContentNodeReference.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/NodeByRef/NodeByRef.bs b/playlet-lib/src/components/NodeByRef/NodeByRef.bs new file mode 100644 index 00000000..476a61c0 --- /dev/null +++ b/playlet-lib/src/components/NodeByRef/NodeByRef.bs @@ -0,0 +1,41 @@ +' NodeByRef: these functions provides the ability to "wrap" a node in +' another node, so it can be passed by reference. This is useful for +' when a node is required to be a child of multiple nodes at once, +' which is not possible. +' Instead, the node is wrapped, passed around, and unwrapped when needed. +import "pkg:/source/utils/StringUtils.bs" + +namespace NodeByRef + + function Wrap(node as object) as object + if node = invalid + return invalid + end if + + if node.refNode <> invalid + return node + end if + + ref = CreateObject("roSGNode", "ContentNodeReference") + ref.refNode = node + if not StringUtils.IsNullOrEmpty(node.id) + ref.id = node.id + "-ref" + end if + return ref + end function + + function Unwrap(node as object) as object + if node = invalid + return invalid + end if + + loopGuard = 0 + while node.refNode <> invalid and loopGuard < 10 + node = node.refNode + loopGuard += 1 + end while + + return node + end function + +end namespace diff --git a/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.bs b/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.bs index a964990b..96a8136a 100644 --- a/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.bs +++ b/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.bs @@ -1,3 +1,4 @@ +import "pkg:/components/NodeByRef/NodeByRef.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" @@ -14,6 +15,7 @@ end function function OnContentSet() as void content = m.top.content + content = NodeByRef.Unwrap(content) if content = invalid return end if diff --git a/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.xml b/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.xml index f056287b..b97d245f 100644 --- a/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.xml +++ b/playlet-lib/src/components/PlayQueue/Notifications/PlayQueueNotification.xml @@ -8,7 +8,7 @@ @@ -40,6 +40,12 @@ color="0x262626ff" wrap="true" /> + = m.queue.Count() - m.queue.push(node) - m.top.index = m.queue.Count() - 1 + node = WrapWithIndexField(node) + if m.top.index >= m.content.getChildCount() + m.content.appendChild(node) + m.top.index = m.content.getChildCount() - 1 else - ArrayUtils.Insert(m.queue, node, m.top.index) + m.content.insertChild(node, m.top.index) end if PlayItemAtCurrentIndex() end function function AddToQueue(node as object) - m.queue.push(node) + node = WrapWithIndexField(node) + m.content.appendChild(node) queueNotifications = m.top.preferences["misc.queue_notifications"] if queueNotifications PlayQueue.ShowNotifcation(m.top.notifications, node) end if end function -' TODO:P1 better model of the queue (Now playing, etc) +function WrapWithIndexField(node as object) as object + node = NodeByRef.Wrap(node) + if not node.hasField("index") + node.addField("index", "integer", false) + end if + node.index = m.top.index + return node +end function + +' TODO:P0 better model of the queue (Now playing, etc) function GetQueue(unused as dynamic) as object + queueNodes = m.content.getChildren(-1, 0) items = [] - for each item in m.queue - if item.type = "video" + for each node in queueNodes + node = NodeByRef.Unwrap(node) + if node = invalid + continue for + end if + + nodeType = node.type + if nodeType = "video" items.push({ - type: item.type, - videoId: item.videoId, - title: item.title, - author: item.author, - thumbnail: item.thumbnail + type: nodeType, + videoId: node.videoId, + title: node.title, + author: node.author, + thumbnail: node.thumbnail }) - else if item.type = "playlist" + else if node.type = "playlist" items.push({ - type: item.type, - playlistId: item.playlistId, - title: item.title, - thumbnail: item.thumbnail, - videoCountText: item.videoCountText + type: nodeType, + playlistId: node.playlistId, + title: node.title, + thumbnail: node.thumbnail, + videoCountText: node.videoCountText }) end if end for @@ -68,21 +90,24 @@ function GetQueue(unused as dynamic) as object end function function Clear(unused as dynamic) - m.queue.Clear() + m.content.removeChildrenIndex(m.content.getChildCount(), 0) m.top.index = -1 m.top.playlistIndex = -1 end function function PlayNextItem(unused as dynamic) as boolean - if m.queue.Count() = 0 + childCount = m.content.getChildCount() + if childCount = 0 return false end if - if m.top.index <> -1 - if m.top.index >= m.queue.Count() + index = m.top.index + if index <> -1 + if index >= childCount return false end if - node = m.queue[m.top.index] + node = m.content.getChild(index) + node = NodeByRef.Unwrap(node) if node.type = "playlist" m.top.playlistIndex += 1 if PlayPlaylistAtCurrentIndex(node) @@ -93,15 +118,22 @@ function PlayNextItem(unused as dynamic) as boolean m.top.index += 1 m.top.playlistIndex = -1 - if m.top.index >= m.queue.Count() + if m.top.index >= childCount return false end if return PlayItemAtCurrentIndex() end function +function PlayItemAtIndex(index as integer) as boolean + m.top.index = index + m.top.playlistIndex = -1 + return PlayItemAtCurrentIndex() +end function + function PlayItemAtCurrentIndex() as boolean - node = m.queue[m.top.index] + node = m.content.getChild(m.top.index) + node = NodeByRef.Unwrap(node) if node = invalid return false end if @@ -118,17 +150,18 @@ function PlayItemAtCurrentIndex() as boolean end function function PlayPlaylistAtCurrentIndex(playlist as object) as boolean + playlist = NodeByRef.Unwrap(playlist) if playlist.videoCount < 0 LoadPlaylist(playlist) return true end if - index = m.top.playlistIndex - if index < 0 or index >= playlist.videoCount + playlistIndex = m.top.playlistIndex + if playlistIndex < 0 or playlistIndex >= playlist.videoCount return false end if - if index < playlist.getChildCount() - child = playlist.getChild(index) + if playlistIndex < playlist.getChildCount() + child = playlist.getChild(playlistIndex) VideoUtils.PlayVideo(child) return true end if @@ -177,3 +210,12 @@ function OnPlaylistContentTaskResult(output as object) as void PlayPlaylistAtCurrentIndex(playlist) end function + +function OnIndexChange() + index = m.top.index + m.content.index = index + children = m.top.content.getChildren(-1, 0) + for each child in children + child.index = index + end for +end function diff --git a/playlet-lib/src/components/PlayQueue/PlayQueue.xml b/playlet-lib/src/components/PlayQueue/PlayQueue.xml index 3084ef4f..dddeb5f8 100644 --- a/playlet-lib/src/components/PlayQueue/PlayQueue.xml +++ b/playlet-lib/src/components/PlayQueue/PlayQueue.xml @@ -1,7 +1,7 @@ - - + + @@ -11,5 +11,9 @@ + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/PlayQueue/PlayQueueView.bs b/playlet-lib/src/components/PlayQueue/PlayQueueView.bs new file mode 100644 index 00000000..80ebf239 --- /dev/null +++ b/playlet-lib/src/components/PlayQueue/PlayQueueView.bs @@ -0,0 +1,83 @@ +import "pkg:/components/Navigation/Navigation.bs" +import "pkg:/source/utils/FocusManagement.bs" +import "pkg:/source/utils/Types.bs" + +function Init() + m.queueList = m.top.findNode("queueList") + m.queueLabel = m.top.findNode("queueLabel") + m.clearButton = m.top.findNode("clearButton") + m.showAnimation = m.top.findNode("showAnimation") + + m.clearButton.ObserveField("buttonSelected", FuncName(OnClearButtonSelected)) + m.queueList.ObserveField("itemSelected", FuncName(OnItemSelected)) + + SetNavigation(m.queueList, "up", m.clearButton) + SetNavigation(m.clearButton, "down", m.queueList) +end function + +function Show(unsued as dynamic) + m.showAnimation.control = "start" +end function + +function OnFocusChange() as void + if not m.top.focus + return + end if + NodeSetFocus(m.queueList, true) +end function + +function OnContentSet() as void + content = m.top.content + if content = invalid + return + end if + + if not content.isSameNode(m.queueList.content) + m.queueList.content = content + end if + + index = content.index + if IsInt(index) and index <> -1 + m.queueLabel.text = `Queue (${index + 1}/${content.getChildCount()})` + else + m.queueLabel.text = `Queue (${content.getChildCount()})` + end if +end function + +function OnkeyEvent(key as string, press as boolean) as boolean + if NavigationKeyHandler(key, press).handled + return true + end if + + if press = false + return false + end if + + if key = "back" + Close() + return true + end if + return false +end function + +function Close() + m.top.appController@.PopScreen(invalid) +end function + +function OnClearButtonSelected() + m.top.playQueue@.Clear(invalid) +end function + +function OnItemSelected() as void + content = m.queueList.content + if content = invalid + return + end if + + index = m.queueList.itemSelected + if index < 0 or index >= content.getChildCount() + return + end if + + m.top.playQueue@.PlayItemAtIndex(index) +end function diff --git a/playlet-lib/src/components/PlayQueue/PlayQueueView.xml b/playlet-lib/src/components/PlayQueue/PlayQueueView.xml new file mode 100644 index 00000000..6a4cd9ce --- /dev/null +++ b/playlet-lib/src/components/PlayQueue/PlayQueueView.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + +