From c3347363250000d28e572b81c9da43e22e0cd574 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 27 Feb 2024 17:14:15 -0500 Subject: [PATCH] play next video on fail --- CHANGELOG.md | 6 ++ .../src/components/Dialog/DialogUtils.bs | 27 +++++ .../src/components/Dialog/TimedDialog.bs | 98 +++++++++++++++++++ .../src/components/Dialog/TimedDialog.xml | 10 ++ .../components/PlaylistView/PlaylistView.bs | 1 - .../src/components/VideoPlayer/VideoPlayer.bs | 25 +++-- 6 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 playlet-lib/src/components/Dialog/TimedDialog.bs create mode 100644 playlet-lib/src/components/Dialog/TimedDialog.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index a82abc5d..199f5427 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 + +- When a video error dialog shows, a timer with 10 seconds starts to play the next video + ## [0.20.2] - 2024-02-26 ### Added diff --git a/playlet-lib/src/components/Dialog/DialogUtils.bs b/playlet-lib/src/components/Dialog/DialogUtils.bs index 4ced907e..39aa1e54 100644 --- a/playlet-lib/src/components/Dialog/DialogUtils.bs +++ b/playlet-lib/src/components/Dialog/DialogUtils.bs @@ -22,4 +22,31 @@ namespace DialogUtils m.top.getScene().dialog = dialog return dialog end function + + function ShowTimedDialogButtons(message as dynamic, title as string, buttons as object, defaultButton as integer, timeoutSeconds as integer, large = false as boolean) as object + dialog = CreateObject("roSGNode", "TimedDialog") + if GetInterface(message, "ifArray") = invalid + message = message.Tokenize(`\n`).ToArray() + end if + dialog.message = message + dialog.title = title + dialog.actualButtons = buttons + + if large + if m._deviceDisplayWidth = invalid + deviceInfo = CreateObject("roDeviceInfo") + displaySize = deviceInfo.GetDisplaySize() + m._deviceDisplayWidth = displaySize.w + end if + dialog.width = m._deviceDisplayWidth - 100 + end if + + dialog.defaultButtonIndex = defaultButton + dialog.defaultActionTimeoutSeconds = timeoutSeconds + dialog.startCountdown = true + + m.top.getScene().dialog = dialog + + return dialog + end function end namespace diff --git a/playlet-lib/src/components/Dialog/TimedDialog.bs b/playlet-lib/src/components/Dialog/TimedDialog.bs new file mode 100644 index 00000000..c35ae91c --- /dev/null +++ b/playlet-lib/src/components/Dialog/TimedDialog.bs @@ -0,0 +1,98 @@ +import "DialogPalette.bs" +import "pkg:/source/utils/Types.bs" +import "pkg:/source/utils/Logging.bs" + +function Init() + SetPalette(m.top) + m.top.observeField("buttonSelected", FuncName(OnButtonSelected)) + m.top.observeField("wasClosed", FuncName(OnDialogClosed)) +end function + +function OnStartCountdown() + actualButtons = m.top.actualButtons + defaultButtonIndex = m.top.defaultButtonIndex + defaultActionTimeoutSeconds = m.top.defaultActionTimeoutSeconds + + buttons = [] + for i = 0 to actualButtons.Count() - 1 + if i = defaultButtonIndex + buttons.push(`${actualButtons[i]} (${defaultActionTimeoutSeconds})`) + else + buttons.push(actualButtons[i]) + end if + end for + m.top.buttons = buttons + + CancelTimer() + m.timer = CreateObject("roSGNode", "Timer") + m.timer.repeat = true + m.timer.observeFieldScoped("fire", FuncName(TimerUpdateButtons)) + m.currentCountDown = defaultActionTimeoutSeconds + m.timer.control = "start" +end function + +function TimerUpdateButtons() as void + m.currentCountDown -= 1 + if m.currentCountDown <= 0 + m.top.actualButtonSelected = m.top.defaultButtonIndex + m.top.close = true + return + end if + + FindButtonComponents() + if m.buttonComponents = invalid + return + end if + + defaultButtonIndex = m.top.defaultButtonIndex + m.buttonComponents[defaultButtonIndex].text = `${m.top.actualButtons[defaultButtonIndex]} (${m.currentCountDown})` +end function + +function OnButtonSelected() + m.top.actualButtonSelected = m.top.buttonSelected + m.top.close = true +end function + +function OnDialogClosed() + CancelTimer() +end function + +function onKeyEvent(key as string, press as boolean) as boolean + if m.timer = invalid + return false + end if + + CancelTimer() + + FindButtonComponents() + if m.buttonComponents = invalid + return false + end if + + defaultButtonIndex = m.top.defaultButtonIndex + m.buttonComponents[defaultButtonIndex].text = m.top.actualButtons[defaultButtonIndex] + return false +end function + +function CancelTimer() + if m.timer <> invalid + m.timer.control = "stop" + m.timer.unobserveFieldScoped("fire") + m.timer = invalid + end if +end function + +function FindButtonComponents() as void + if m.buttonComponents <> invalid + return + end if + + areas = m.top.getChildren(-1, 0) + for each area in areas + if area.subtype() = "StdDlgButtonArea" + m.buttonComponents = area.getChildren(-1, 0) + return + end if + end for + LogWarn("No buttons found in dialog") +end function diff --git a/playlet-lib/src/components/Dialog/TimedDialog.xml b/playlet-lib/src/components/Dialog/TimedDialog.xml new file mode 100644 index 00000000..8558528b --- /dev/null +++ b/playlet-lib/src/components/Dialog/TimedDialog.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/PlaylistView/PlaylistView.bs b/playlet-lib/src/components/PlaylistView/PlaylistView.bs index 83255514..1764186d 100644 --- a/playlet-lib/src/components/PlaylistView/PlaylistView.bs +++ b/playlet-lib/src/components/PlaylistView/PlaylistView.bs @@ -1,6 +1,5 @@ import "pkg:/components/ChannelView/ChannelUtils.bs" import "pkg:/components/ContextMenu/ContextMenuUtils.bs" -import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Navigation/LongPress.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs" diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index 9339fe83..277d06d5 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -124,9 +124,10 @@ function OnVideoContentTaskResults(output as object) as void LogError(error) videoId = output.task.input.content.videoId message = `Failed to load video information for ${videoId}\n${error}` - DialogUtils.ShowDialog(message, "Video load fail", true) - ' TODO:P1 play next video in the queue after showing the error dialog - m.videoQueue.closePlayer = true + + dialog = DialogUtils.ShowTimedDialogButtons(message, "Video load fail", ["Play next video", "Cancel"], 0, 10, true) + dialog.observeFieldScoped("wasClosed", FuncName(OnVideoContentTaskErrorDialogResponse)) + Lounge.SendOnStateChange() return end if @@ -139,6 +140,18 @@ function OnVideoContentTaskResults(output as object) as void end if end function +function OnVideoContentTaskErrorDialogResponse(event as object) + dialog = event.getRoSGNode() + dialog.unobserveFieldScoped("wasClosed") + shouldPlayNext = dialog.actualButtonSelected = 0 + + if not shouldPlayNext or not VideoQueueUtils.PlayNext(m.videoQueue, m.invidious, m.preferences) + m.videoQueue.closePlayer = true + end if + + Lounge.SendOnStateChange() +end function + function MarkVideoWatched(videoId as string) if m.top.invidious.authToken <> invalid AsyncTask.Start(Tasks.MarkVideoWatchedTask, { @@ -247,9 +260,9 @@ function OnVideoPlayerStateChange() as void title = `Error playing video ${content.videoId}` LogError(title, messageLines) - DialogUtils.ShowDialog(messageLines, title, true) - ' TODO:P1 play next video in the queue after showing the error dialog - m.videoQueue.closePlayer = true + + dialog = DialogUtils.ShowTimedDialogButtons(messageLines, title, ["Play next video", "Cancel"], 0, 10, true) + dialog.observeFieldScoped("wasClosed", FuncName(OnVideoContentTaskErrorDialogResponse)) end if end function