Files
playlet-piped/playlet-lib/src/components/PlaylistView/PlaylistView.bs
T

252 lines
7.3 KiB
Plaintext

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/PlaylistView/PlaylistContentTask.bs"
import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs"
function Init()
m.background = m.top.findNode("background")
m.backgroundSmall = m.top.findNode("backgroundSmall")
m.titleLabel = m.top.findNode("titleLabel")
m.subtitleLabel = m.top.findNode("subtitleLabel")
m.updatedLabel = m.top.findNode("updatedLabel")
m.descriptionLabel = m.top.findNode("descriptionLabel")
m.list = m.top.findNode("list")
end function
function OnNodeReady()
m.list.ObserveField("itemFocused", FuncName(OnItemFocused))
InitializeLongPressTimer()
end function
function OnFocusChange() as void
if not m.top.focus
return
end if
NodeSetFocus(m.list, true)
end function
function OnContentSet() as void
content = m.top.content
if content = invalid
return
end if
m.titleLabel.text = content.title
' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation.
m.subtitleLabel.text = `${content._author} • ${content.videoCountText} • ${content.viewCountText}`
m.updatedLabel.text = content.updatedText
m.descriptionLabel.text = content.description
' We use two Posters to cross fade from the low res thumbnail to the high res thumbnail.
' We can't use a single Poster because we're using the blendColor. There's no loadingBitmapBlendColor.
m.backgroundSmall.uri = content.thumbnail
m.background.uri = content.thumbnailBackground
itemFocused = m.list.itemFocused
m.list.content = content
if itemFocused > -1 and content.getChildCount() > itemFocused
m.list.jumpToItem = itemFocused
end if
LoadPlaylistIfNeeded()
end function
function OnkeyEvent(key as string, press as boolean) as boolean
if LongPressHandler(key, press)
return true
end if
if key = LONG_PRESS_KEY and press
OnItemLongPressed(m.list.itemFocused)
return true
end if
if key = "OK" and not press
OnItemSelected(m.list.itemFocused)
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()
if m.playlistLoadTask <> invalid
m.playlistLoadTask.cancel = true
end if
m.appController@.PopScreen(invalid)
end function
function OnItemFocused()
LoadPlaylistIfNeeded()
end function
function OnItemSelected(index as integer) as void
playlist = m.list.content
if playlist = invalid
return
end if
if index < 0 or index >= playlist.getChildCount()
return
end if
playlistId = playlist.playlistId
video = playlist.getChild(index)
videoId = video.videoId
if not StringUtils.IsNullOrEmpty(videoId)
LogInfo("Play playlist:", playlistId, "video:", videoId)
m.playQueue@.Play(playlist, index)
end if
end function
function OnItemLongPressed(index as integer) as void
playlist = m.list.content
if playlist = invalid
return
end if
if index < 0 or index >= playlist.getChildCount()
return
end if
playlistId = playlist.playlistId
video = playlist.getChild(index)
videoId = video.videoId
if StringUtils.IsNullOrEmpty(videoId)
LogWarn("Long press video with no id in playlist/ playlist:", playlistId, "video:", videoId)
return
end if
LogInfo("Long press: playlist:", playlistId, "video:", videoId)
' TODO:P1 handle case of opening the channel
' - Owner of a video
' - Owner of a playlist
contextMenuProviders = [m.top]
ContextMenuUtils.Open(contextMenuProviders, m.appController, video)
end function
function GetContextMenuOptionsForItem(video as object) as object
playlist = m.list.content
if playlist = invalid
return []
end if
index = m.list.itemFocused
if index < 0 or index >= playlist.getChildCount()
return []
end if
selectedVideo = playlist.getChild(m.list.itemFocused)
if selectedVideo = invalid or not selectedVideo.isSameNode(video)
return []
end if
options = [
ContextMenuUtils.CreateOption("Play", m.playQueue, "Play", [playlist, index]),
ContextMenuUtils.CreateOption("Queue", m.playQueue, "AddToQueue", [playlist])
ContextMenuUtils.CreateOption("Play video", m.playQueue, "Play", [video, -1]),
ContextMenuUtils.CreateOption("Queue video", m.playQueue, "AddToQueue", [video]),
]
if not StringUtils.IsNullOrEmpty(playlist.authorId)
options.push(ContextMenuUtils.CreateOption("Open channel", m.top, "OpenPlaylistChannel", [playlist]))
end if
options.append(m.bookmarks@.GetMenuForPlaylist(playlist))
options.append(m.bookmarks@.GetMenuForVideo(video))
return options
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 }, invalid)
ChannelUtils.Open(channel)
end function
'TODO:P1 a playlist could have been opened from the bookmarks, which can be as old as 3 days
' We should make a request in this case, and let the cache handle freshness.
function LoadPlaylistIfNeeded() as void
content = m.top.content
if content = invalid
return
end if
if not content.loadState = FeedLoadState.LoadedPage and not content.loadState = FeedLoadState.None
return
end if
videoCount = content.videoCount
currentVideosCount = content.getChildCount()
if videoCount = currentVideosCount
return
end if
itemFocused = m.list.itemFocused
if itemFocused < currentVideosCount - 5
return
end if
LoadPlaylist()
end function
function LoadPlaylist() as void
if m.playlistLoadTask <> invalid
m.playlistLoadTask.cancel = true
end if
m.top.content.loadState = FeedLoadState.Loading
m.playlistLoadTask = StartAsyncTask(PlaylistContentTask, {
content: m.top.content,
invidious: m.top.invidious
}, OnPlaylistContentTaskResult)
end function
function OnPlaylistContentTaskResult(output as object) as void
m.playlistLoadTask = invalid
if output.cancelled
return
end if
if not output.success or not output.result.success
' output.error for unhandled exception
error = output.error
if error = invalid
' output.result.error for network errors
error = output.result.error
end if
error = ErrorUtils.Format(error)
LogError(error)
playlistId = output.task.input.content.playlistId
message = `Failed to load playlist ${playlistId}\n${error}`
DialogUtils.ShowDialog(message, "Playlist load fail", true)
return
end if
LoadPlaylistIfNeeded()
end function