From 5179de5c74b074369aa402652c60dbe131f9a995 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 31 Oct 2023 22:47:03 -0400 Subject: [PATCH] Subscribe button (#185) * Subscribe button * changelog * Lint fix * Introduce disposable part * Lint fix * revert navigation change --------- Co-authored-by: github-action linter --- CHANGELOG.md | 2 + .../src/components/ChannelView/ChannelView.bs | 93 ++++++++++++++++++- .../components/ChannelView/ChannelView.xml | 11 ++- .../components/ChannelView/SubscribeTask.bs | 35 +++++++ .../Services/Invidious/Invidious.bs | 17 ++++ .../Services/Invidious/Invidious.xml | 4 +- .../Services/Invidious/InvidiousService.bs | 45 ++++++++- .../Invidious/InvidiousSubscriptionsTask.bs | 42 +++++++++ .../src/components/VideoFeed/VideoRowList.bs | 16 +++- .../src/components/VideoFeed/VideoRowList.xml | 2 +- .../parts/Disposable/Disposable.part.xml | 5 + playlet-lib/src/source/services/HttpClient.bs | 12 ++- .../src/source/utils/DisposableObserve.bs | 39 ++++++++ 13 files changed, 308 insertions(+), 15 deletions(-) create mode 100644 playlet-lib/src/components/ChannelView/SubscribeTask.bs create mode 100644 playlet-lib/src/components/Services/Invidious/InvidiousSubscriptionsTask.bs create mode 100644 playlet-lib/src/components/parts/Disposable/Disposable.part.xml create mode 100644 playlet-lib/src/source/utils/DisposableObserve.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 05f32a57..dc4bab3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,10 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Pagination support in web app search (load more button) - Video links pasted into the web app triggers the dialog to cast the video +- Ability to subscribe/unsubscribe from a channel screen ### Fixed - A bug where casting from web included timestamp even if the "Start at" check box is unchecked +- A leak where channel screens would reload data even when closed ## [0.14.0] - 2023-10-25 diff --git a/playlet-lib/src/components/ChannelView/ChannelView.bs b/playlet-lib/src/components/ChannelView/ChannelView.bs index 7eaa036a..3e3a3d50 100644 --- a/playlet-lib/src/components/ChannelView/ChannelView.bs +++ b/playlet-lib/src/components/ChannelView/ChannelView.bs @@ -1,6 +1,11 @@ +import "pkg:/components/Navigation/Navigation.bs" +import "pkg:/source/asyncTask/asyncTask.bs" +import "pkg:/source/utils/DisposableObserve.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" +import "SubscribeTask.bs" function Init() m.container = m.top.findNode("container") @@ -11,10 +16,12 @@ function Init() m.banner = m.top.findNode("banner") m.thumbnail = m.top.findNode("thumbnail") m.authorLabel = m.top.findNode("authorLabel") + m.subscribeButton = m.top.findNode("subscribeButton") m.rowList = m.top.FindNode("rowList") + m.rowList.focusable = true m.rowList.screen = m.top - m.banner.ObserveField("loadStatus", FuncName(OnBannerLoadStatus)) + DisposableObserveField(m.banner, "loadStatus", FuncName(OnBannerLoadStatus)) m.author = "" m.authorId = "" @@ -23,6 +30,17 @@ end function function OnNodeready() m.rowList@.BindNode(invalid) + + SetNavigation(m.rowList, "up", m.subscribeButton) + SetNavigation(m.subscribeButton, "down", m.rowList) + + m.subscribeButton.text = "Subscribe" + DisposableObserveField(m.subscribeButton, "buttonSelected", FuncName(OnSubscribeButtonSelected)) + + SetSubscribeButtonVisibility() + DisposableObserveFieldScoped(m.invidious, "authToken", FuncName(SetSubscribeButtonVisibility)) + OnSubscriptionChanged() + DisposableObserveFieldScoped(m.invidious, "subscriptions", FuncName(OnSubscriptionChanged)) end function function OnFocusChange() as void @@ -36,6 +54,7 @@ end function function OnContentSet() as void content = m.top.content if content = invalid + m.top.authorId = "" return end if @@ -55,6 +74,8 @@ function OnContentSet() as void authorId = ValidString(content.authorId) author = ValidString(content._author) + m.top.authorId = authorId + if (authorId <> m.authorId or author <> m.author) and IsArray(content.tabs) m.authorId = authorId m.author = author @@ -78,6 +99,10 @@ function CreateChannelFeeds(authorId as string, author as string, tabs as object 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 @@ -91,6 +116,25 @@ end function function Close() m.appController@.PopScreen(invalid) + m.top.dispose = true +end function + +function OnDispose() + m.rowList.dispose = true + DisposeObservers() +end function + +function OnSubscribeButtonSelected() + if m.subscribeTask <> invalid + m.subscribeTask.cancel = true + end if + + isSubbed = IsSubscribed() + m.subscribeTask = StartAsyncTask(SubscribeTask, { + invidious: m.invidious, + ucid: m.authorId, + subscribe: not isSubbed + }) end function function OnBannerLoadStatus() as void @@ -159,10 +203,51 @@ function CreateChannelFeed(title as string, endpoint as string, ucid as string, end function function GetContextMenuOptionsForItem(video as object) as object - content = m.top.content - if content = invalid or StringUtils.IsNullOrEmpty(content.authorId) + if StringUtils.IsNullOrEmpty(m.top.authorId) return [] end if + return m.bookmarks@.GetMenuForChannel(m.top.content) +end function - return m.bookmarks@.GetMenuForChannel(content) +function IsLoggedIn() as boolean + return m.invidious.authToken <> invalid +end function + +function IsSubscribed() as boolean + authorId = m.top.authorId + if StringUtils.IsNullOrEmpty(authorId) + return false + end if + + subscriptions = m.invidious.subscriptions + if subscriptions = invalid + return false + end if + if not subscriptions.DoesExist(authorId) + return false + end if + return subscriptions[authorId] +end function + +function OnAuthorIdChange() + SetSubscribeButtonVisibility() + OnSubscriptionChanged() +end function + +function SetSubscribeButtonVisibility() + isLogged = IsLoggedIn() + m.subscribeButton.visible = isLogged and not StringUtils.IsNullOrEmpty(m.top.authorId) + m.subscribeButton.focusable = m.subscribeButton.visible + if not m.subscribeButton.visible and m.subscribeButton.hasFocus() + NodeSetFocus(m.rowList, true) + end if +end function + +function OnSubscriptionChanged() + isSubbed = IsSubscribed() + if isSubbed + m.subscribeButton.text = "Unsubscribe" + else + m.subscribeButton.text = "Subscribe" + end if end function diff --git a/playlet-lib/src/components/ChannelView/ChannelView.xml b/playlet-lib/src/components/ChannelView/ChannelView.xml index 83ae63fa..806f3a97 100644 --- a/playlet-lib/src/components/ChannelView/ChannelView.xml +++ b/playlet-lib/src/components/ChannelView/ChannelView.xml @@ -1,6 +1,7 @@ - + + @@ -37,6 +38,14 @@ font="font:MediumBoldSystemFont" /> +