From 793842f4ddee1533ab53d0147e91a79864ccd1ec Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 20 Feb 2024 22:28:54 -0500 Subject: [PATCH] Do not add entire playlist if it already exists --- .../src/components/VideoQueue/VideoQueue.bs | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/playlet-lib/src/components/VideoQueue/VideoQueue.bs b/playlet-lib/src/components/VideoQueue/VideoQueue.bs index 5c2d3ebd..3df96729 100644 --- a/playlet-lib/src/components/VideoQueue/VideoQueue.bs +++ b/playlet-lib/src/components/VideoQueue/VideoQueue.bs @@ -190,6 +190,17 @@ function OnPlaylistLoadStateChange() as void end for if playlistOp = "play" + playlistIndex = MathUtils.Clamp(playlistIndex, 0, videoNodes.Count() - 1) + + ' If the playlist is already in the queue as is, play it from there. + existingIndex = FindPlaylistVideosInQueue(videoNodes) + if existingIndex >= 0 + m.top.index = existingIndex + playlistIndex + m.top.closePlayer = true + m.top.play = true + return + end if + queueCount = m.content.getChildCount() index = m.top.index index += 1 @@ -199,9 +210,7 @@ function OnPlaylistLoadStateChange() as void m.content.insertChildren(videoNodes, index) end if - playlistIndex = MathUtils.Clamp(playlistIndex, 0, videoNodes.Count() - 1) - index = index + playlistIndex - m.top.index = index + m.top.index = index + playlistIndex m.top.closePlayer = true m.top.play = true @@ -217,6 +226,32 @@ function OnPlaylistLoadStateChange() as void end if end function +function FindPlaylistVideosInQueue(videoNodes as object) as integer + queueCount = m.content.getChildCount() + playlistCount = videoNodes.Count() + if queueCount < playlistCount + return -1 + end if + + queueVideos = m.content.getChildren(-1, 0) + + ' array in array search based on node.videoId, starting from the end + for i = queueCount - 1 to playlistCount step -1 + found = true + for j = 0 to playlistCount - 1 + if queueVideos[i - j].videoId <> videoNodes[j].videoId + found = false + exit for + end if + end for + if found + return i - playlistCount + 1 + end if + end for + + return -1 +end function + function OnClearQueue() queueCount = m.content.getChildCount() if queueCount > 0