Add Channel support (#144)

* Add Channel support

* Cross fade

* add todo
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-09-15 17:34:07 -04:00
1 parent 38a054a46a
commit f452bf23de
16 files changed
+347 -60

No files matched your search

+1
View File
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A field to indicate when a playlist was last updated
- Voice search in search page
- Basic support for channels (open channel, see latest videos)
### Changed
@@ -0,0 +1,8 @@
namespace ChannelUtils
function Open(contentNode as object)
channelView = CreateObject("roSGNode", "ChannelView")
m.appController@.PushScreen(channelView)
channelView@.BindNode(invalid)
channelView.content = contentNode
end function
end namespace
@@ -0,0 +1,77 @@
import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Types.bs"
import "pkg:/source/utils/StringUtils.bs"
function Init()
m.log = new log.Logger("ChannelView")
m.banner = m.top.findNode("banner")
m.thumbnail = m.top.findNode("thumbnail")
m.authorLabel = m.top.findNode("authorLabel")
m.rowList = m.top.FindNode("rowList")
m.banner.ObserveField("loadStatus", FuncName(OnBannerLoadStatus))
end function
function OnNodeready()
m.rowList@.BindNode(invalid)
end function
function OnFocusChange() as void
if not m.top.focus
return
end if
NodeSetFocus(m.rowList, true)
end function
function OnContentSet() as void
content = m.top.content
if content = invalid
return
end if
' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation.
m.authorLabel.text = content._author
' TODO:P1 handle case where there's no banner - a big portion of the screen is blank
m.banner.uri = content.banner
if StringUtils.IsNullOrEmpty(content.thumbnail)
m.thumbnail.uri = ""
else
if m.thumbnail.uri <> content.thumbnail or m.thumbnail.loadStatus = "failed"
m.thumbnail.loadingBitmapUri = m.thumbnail.uri
m.thumbnail.uri = content.thumbnail
end if
end if
m.rowList.content = content
content@.LoadChannel(m.invidious)
end function
function OnkeyEvent(key as string, press as boolean) as boolean
if press = false
return false
end if
if key = "back"
Close()
return true
end if
return false
end function
function Close()
m.appController@.PopScreen(invalid)
end function
function OnBannerLoadStatus() as void
status = m.banner.loadStatus
if status <> "ready"
return
end if
aspect = m.banner.bitmapWidth / m.banner.bitmapHeight
m.banner.height = m.banner.width / aspect
end function
@@ -0,0 +1,58 @@
<component name="ChannelView" extends="Group" includes="AutoBind,Focus">
<interface>
<field id="content" type="node" onChange="OnContentSet" />
<field id="appController" type="node" bind="/AppController" />
<field id="invidious" type="node" bind="/Invidious" />
</interface>
<children>
<Rectangle
width="1280"
height="720"
color="0x242424">
</Rectangle>
<Poster
id="banner"
translation="[0,0]"
width="1280" />
<Poster
translation="[115,180]"
width="104"
height="104"
uri="pkg:/images/circle-mask-500.png">
<!-- TODO:P1 figure out why the masksize need to be 150 but the image is 100 -->
<MaskGroup
translation="[2,2]"
masksize="[150,150]"
maskuri="pkg:/images/circle-mask-500.png">
<Poster
id="thumbnail"
width="100"
height="100" />
</MaskGroup>
</Poster>
<Label
id="authorLabel"
translation="[228,226]"
font="font:MediumBoldSystemFont" />
<VideoRowList
itemComponentName="RowCell"
id="rowList"
focusable="false"
translation="[0,320]"
numRows="1"
rowitemSize="[[350,196]]"
rowItemSpacing="[[25,0]]"
itemSize="[1280,326]"
itemSpacing="[0,65]"
rowLabelOffset="[[115,20]]"
focusXOffset="[115]"
showRowLabel="[true]"
rowFocusAnimationStyle="floatingfocus"
variableWidthItems="true"
focusBitmapUri="pkg:/images/focus_grid.9.png" />
</children>
</component>
@@ -0,0 +1,48 @@
import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/components/ContentNode/ChannelContentTask.bs"
import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/components/Dialog/DialogUtils.bs"
function Init()
m.top.latestVideos = m.top.findNode("latestVideos")
end function
function LoadChannel(invidiousNode as object) as void
if m.contentTask <> invalid
m.contentTask.cancel = true
end if
loadState = m.top.loadState
if loadState = FeedLoadState.Loading or loadState = FeedLoadState.Loaded
return
end if
m.top.loadState = FeedLoadState.Loading
m.contentTask = StartAsyncTask(ChannelContentTask, {
content: m.top,
invidious: invidiousNode
}, OnChannelContentTaskResult)
end function
function OnChannelContentTaskResult(output as object) as void
m.contentTask = 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)
m.log.error(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
end function
@@ -1,7 +1,28 @@
<component name="ChannelContentNode" extends="ContentNode">
<interface>
<field id="type" type="string" value="channel" />
<field id="loadState" type="string" />
<function name="LoadChannel" />
<field id="HDItemWidth" type="float" value="200" />
<!-- NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation -->
<field id="_author" type="string" />
<field id="authorId" type="string" />
<field id="description" type="string" />
<field id="subCount" type="integer" />
<field id="subCountText" type="string" />
<field id="videoCount" type="integer" />
<field id="videoCountText" type="string" />
<field id="thumbnail" type="string" />
<field id="banner" type="string" />
<field id="latestVideos" type="node" />
<field id="HDItemWidth" type="float" value="196" />
</interface>
<children>
<ContentNode id="latestVideos" title="Latest Videos">
</ContentNode>
</children>
</component>
@@ -0,0 +1,40 @@
import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
@asynctask
function ChannelContentTask(input as object) as object
contentNode = input.content
invidiousNode = input.invidious
if m.top.cancel
contentNode.loadState = FeedLoadState.None
return invalid
end if
service = new Invidious.InvidiousService(invidiousNode)
response = service.GetChannel(contentNode.authorId, m.top.cancellation)
if m.top.cancel
contentNode.loadState = FeedLoadState.None
return invalid
end if
metadata = response.Json()
if not response.IsSuccess() or metadata = invalid
contentNode.loadState = FeedLoadState.Error
return {
success: false,
error: response.ErrorMessage()
}
end if
instance = service.GetInstance()
InvidiousContent.ToChannelContentNode(contentNode, metadata, instance)
contentNode.loadState = FeedLoadState.Loaded
return {
success: true
}
end function
@@ -9,7 +9,6 @@ import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/components/Dialog/DialogUtils.bs"
' TODO:P2 use already loaded thumbnail, and crossfade to highres version (using same Poster)
function Init()
m.log = new log.Logger("PlaylistView")
@@ -47,7 +46,8 @@ function OnContentSet() as void
m.updatedLabel.text = content.updatedText
m.descriptionLabel.text = content.description
' TODO:P2 cross fade
' 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
@@ -20,6 +20,7 @@
focusXOffset="[115]"
showRowLabel="[true]"
rowFocusAnimationStyle="floatingfocus"
variableWidthItems="true"
focusBitmapUri="pkg:/images/focus_grid.9.png"
/>
</children>
@@ -16,6 +16,7 @@ namespace Invidious
const VIDEOS_ENDPOINT = "/api/v1/videos"
const PLAYLISTS_ENDPOINT = "/api/v1/playlists"
const CHANNELS_ENDPOINT = "/api/v1/channels"
const HISTORY_ENDPOINT = "/api/v1/auth/history"
const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions"
@@ -136,6 +137,17 @@ namespace Invidious
return request.Await()
end function
function GetChannel(authorId as string, cancellation = invalid as object) as object
instance = m.GetInstance()
url = `${instance}${Invidious.CHANNELS_ENDPOINT}/${authorId}`
request = HttpClient.Get(url)
request.QueryParams(m.DefaultQueryParams())
request.Cancellation(cancellation)
return request.Await()
end function
function GetVideoStreamUrl(videoId as string) as string
instance = m.GetInstance()
return `${instance}/latest_version?id=${videoId}`
@@ -21,8 +21,7 @@ namespace InvidiousContent
else if item.type = "playlist"
return ToPlaylistContentNode(invalid, item, instance)
else if item.type = "channel"
return invalid
' return ToChannelContentNode(item, instance)
return ToChannelContentNode(invalid, item, instance)
else
return invalid
end if
@@ -67,7 +66,7 @@ namespace InvidiousContent
SetIfExists(node, "playlistId", item, "playlistId")
SetIfExists(node, "title", item, "title")
SetIfExists(node, "videoCount", item, "videoCount")
node.videoCountText = PlaylistGetVideoCountText(item)
node.videoCountText = GetVideoCountText(item)
SetIfExists(node, "viewCount", item, "viewCount")
node.viewCountText = PlaylistGetViewCountText(item)
SetIfExists(node, "updated", item, "updated")
@@ -99,10 +98,40 @@ namespace InvidiousContent
return node
end function
function ToChannelContentNode(item as object, instance as dynamic) as object
node = CreateObject("roSGNode", "ChannelContentNode")
function ToChannelContentNode(node as object, item as object, instance as dynamic) as object
if node = invalid
node = CreateObject("roSGNode", "ChannelContentNode")
node.loadState = FeedLoadState.None
end if
node.type = "channel"
' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation.
SetIfExists(node, "_author", item, "author")
SetIfExists(node, "authorId", item, "authorId")
SetIfExists(node, "description", item, "description")
SetIfExists(node, "subCount", item, "subCount")
node.subCountText = GetChannelSubCountText(item)
SetIfExists(node, "videoCount", item, "videoCount")
node.videoCountText = GetVideoCountText(item)
node.thumbnail = GetChannelThumbnail(item.authorThumbnails)
node.banner = GetChannelBanner(item.authorBanners)
if item.latestVideos <> invalid
newNodes = []
for each video in item.latestVideos
video.type = "video"
videoNode = ToVideoContentNode(invalid, video, instance)
if videoNode <> invalid
newNodes.push(videoNode)
end if
end for
if newNodes.Count() > 0
node.latestVideos.appendChildren(newNodes)
end if
end if
return node
end function
@@ -201,11 +230,34 @@ namespace InvidiousContent
node[nodeField] = thumbnail
end function
function PlaylistGetVideoCountText(playlistItem as object) as string
videoCount = ValidInt(playlistItem.videoCount)
function GetChannelThumbnail(authorThumbnails as object) as string
if authorThumbnails = invalid or authorThumbnails.Count() = 0
return ""
end if
url = authorThumbnails[authorThumbnails.Count() - 1].url
if url.startsWith("//")
url = "https:" + url
end if
return url
end function
function GetChannelBanner(authorBanners as object) as string
if authorBanners = invalid or authorBanners.Count() = 0
return ""
end if
return authorBanners[authorBanners.Count() - 1].url
end function
function GetVideoCountText(item as object) as string
videoCount = ValidInt(item.videoCount)
return videoCount = 1 ? "1 video" : `${videoCount} videos`
end function
function GetChannelSubCountText(channel as object) as string
subCount = ValidInt(channel.subCount)
return subCount = 1 ? "1 subscriber" : `${subCount} subscribers`
end function
function PlaylistGetViewCountText(playlistItem as object) as string
return VideoGetViewCountText(playlistItem)
end function
@@ -13,44 +13,9 @@ function OnContentSet() as void
return
end if
m.authorLabel.text = content.author
SetSubCount(content.subCount)
m.thumbnail.uri = GetThumbnailUrl(content.authorThumbnails)
SetVideoCount(content.videoCount)
end function
function SetSubCount(subCount as integer)
subCountText = ""
if subCount = 1
subCountText = "1 subscriber"
else
subCountText = `${subCount} subscribers`
end if
m.subCountLabel.text = subCountText
end function
function SetVideoCount(videoCount as integer)
videoCountText = ""
if videoCount = 1
videoCountText = "1 video"
else
videoCountText = `${videoCount} videos`
end if
m.videoCountLabel.text = videoCountText
end function
function GetThumbnailUrl(videoThumbnails as object) as string
if videoThumbnails = invalid or videoThumbnails.Count() = 0
return ""
end if
url = videoThumbnails[videoThumbnails.Count() - 1].url
if url.startsWith("//")
url = "https:" + url
end if
return url
' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation.
m.authorLabel.text = content._author
m.subCountLabel.text = content.subCountText
m.videoCountLabel.text = content.videoCountText
m.thumbnail.uri = content.thumbnail
end function
@@ -8,27 +8,29 @@
<children>
<LayoutGroup
itemSpacings="[24,5,5]">
<Poster
id="thumbnail"
loadDisplayMode="scaleToZoom"
width="200"
height="196">
</Poster>
<MaskGroup
masksize="[294,294]"
maskuri="pkg:/images/circle-mask-500.png">
<Poster
id="thumbnail"
width="196"
height="196" />
</MaskGroup>
<Label
id="authorLabel"
width="200"
width="196"
font="font:SmallestBoldSystemFont"
maxLines="2"
wrap="true" />
<Label
id="subCountLabel"
width="200"
width="196"
height="25"
font="font:SmallestSystemFont" />
<Label
id="videoCountLabel"
width="200"
width="196"
height="25"
font="font:SmallestSystemFont" />
</LayoutGroup>
@@ -7,6 +7,7 @@ import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/components/PlaylistView/PlaylistUtils.bs"
import "pkg:/components/ChannelView/ChannelUtils.bs"
function Init()
m.pendingLoadTasks = {}
@@ -78,6 +79,7 @@ function OnRowItemSelected(event as object)
else if rowItem.type = "channel"
authorId = rowItem.authorId
m.log.info("Open channel: " + authorId)
ChannelUtils.Open(rowItem)
else if rowItem.type = "action"
if rowItem.action = "LoginDialog"
dialog = CreateObject("roSGNode", "LoginDialog")
@@ -84,7 +84,7 @@
required: false,
type: ["video", "playlist", "channel", "all"],
arrayType: "comma-separated",
default: ["video", "playlist"],
default: ["video", "playlist", "channel"],
},
features: {
required: false,
Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB