Subscribe button (#185)

* Subscribe button

* changelog

* Lint fix

* Introduce disposable part

* Lint fix

* revert navigation change

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2023-10-31 22:47:03 -04:00
1 parent e22051014f
commit 5179de5c74
13 files changed
+308 -15

No files matched your search

+2
View File
@@ -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) - Pagination support in web app search (load more button)
- Video links pasted into the web app triggers the dialog to cast the video - Video links pasted into the web app triggers the dialog to cast the video
- Ability to subscribe/unsubscribe from a channel screen
### Fixed ### Fixed
- A bug where casting from web included timestamp even if the "Start at" check box is unchecked - 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 ## [0.14.0] - 2023-10-25
@@ -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/FocusManagement.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/Types.bs"
import "SubscribeTask.bs"
function Init() function Init()
m.container = m.top.findNode("container") m.container = m.top.findNode("container")
@@ -11,10 +16,12 @@ function Init()
m.banner = m.top.findNode("banner") m.banner = m.top.findNode("banner")
m.thumbnail = m.top.findNode("thumbnail") m.thumbnail = m.top.findNode("thumbnail")
m.authorLabel = m.top.findNode("authorLabel") m.authorLabel = m.top.findNode("authorLabel")
m.subscribeButton = m.top.findNode("subscribeButton")
m.rowList = m.top.FindNode("rowList") m.rowList = m.top.FindNode("rowList")
m.rowList.focusable = true
m.rowList.screen = m.top m.rowList.screen = m.top
m.banner.ObserveField("loadStatus", FuncName(OnBannerLoadStatus)) DisposableObserveField(m.banner, "loadStatus", FuncName(OnBannerLoadStatus))
m.author = "" m.author = ""
m.authorId = "" m.authorId = ""
@@ -23,6 +30,17 @@ end function
function OnNodeready() function OnNodeready()
m.rowList@.BindNode(invalid) 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 end function
function OnFocusChange() as void function OnFocusChange() as void
@@ -36,6 +54,7 @@ end function
function OnContentSet() as void function OnContentSet() as void
content = m.top.content content = m.top.content
if content = invalid if content = invalid
m.top.authorId = ""
return return
end if end if
@@ -55,6 +74,8 @@ function OnContentSet() as void
authorId = ValidString(content.authorId) authorId = ValidString(content.authorId)
author = ValidString(content._author) author = ValidString(content._author)
m.top.authorId = authorId
if (authorId <> m.authorId or author <> m.author) and IsArray(content.tabs) if (authorId <> m.authorId or author <> m.author) and IsArray(content.tabs)
m.authorId = authorId m.authorId = authorId
m.author = author m.author = author
@@ -78,6 +99,10 @@ function CreateChannelFeeds(authorId as string, author as string, tabs as object
end function end function
function OnkeyEvent(key as string, press as boolean) as boolean function OnkeyEvent(key as string, press as boolean) as boolean
if NavigationKeyHandler(key, press).handled
return true
end if
if press = false if press = false
return false return false
end if end if
@@ -91,6 +116,25 @@ end function
function Close() function Close()
m.appController@.PopScreen(invalid) 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 end function
function OnBannerLoadStatus() as void function OnBannerLoadStatus() as void
@@ -159,10 +203,51 @@ function CreateChannelFeed(title as string, endpoint as string, ucid as string,
end function end function
function GetContextMenuOptionsForItem(video as object) as object function GetContextMenuOptionsForItem(video as object) as object
content = m.top.content if StringUtils.IsNullOrEmpty(m.top.authorId)
if content = invalid or StringUtils.IsNullOrEmpty(content.authorId)
return [] return []
end if 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 end function
@@ -1,6 +1,7 @@
<component name="ChannelView" extends="Group" includes="AutoBind,Focus,ContextMenuProvider"> <component name="ChannelView" extends="Group" includes="AutoBind,Focus,ContextMenuProvider,Disposable">
<interface> <interface>
<field id="content" type="node" onChange="OnContentSet" /> <field id="content" type="node" onChange="OnContentSet" />
<field id="authorId" type="string" onChange="OnAuthorIdChange" />
<field id="appController" type="node" bind="/AppController" /> <field id="appController" type="node" bind="/AppController" />
<field id="invidious" type="node" bind="/Invidious" /> <field id="invidious" type="node" bind="/Invidious" />
<field id="bookmarks" type="node" bind="/Bookmarks" /> <field id="bookmarks" type="node" bind="/Bookmarks" />
@@ -37,6 +38,14 @@
font="font:MediumBoldSystemFont" /> font="font:MediumBoldSystemFont" />
</Poster> </Poster>
<Button
id="subscribeButton"
iconUri=""
focusedIconUri=""
showFocusFootprint="true"
minWidth="200"
translation="[930,64]" />
<VideoRowList <VideoRowList
itemComponentName="RowCell" itemComponentName="RowCell"
id="rowList" id="rowList"
@@ -0,0 +1,35 @@
import "pkg:/components/Services/Invidious/InvidiousService.bs"
@asynctask
function SubscribeTask(input as object) as object
invidiousNode = input.invidious
ucid = input.ucid
subscribe = input.subscribe
if m.top.cancel
return invalid
end if
service = new Invidious.InvidiousService(invidiousNode)
if subscribe
response = service.Subscribe(ucid)
else
response = service.Unsubscribe(ucid)
end if
if not response.IsSuccess()
error = response.ErrorMessage()
LogError("Failed to subscribe/unsubscribe to channel:", error)
return {
success: false,
error: error
}
end if
invidiousNode.refreshSubscribtions = true
return {
success: true
}
end function
@@ -1,4 +1,6 @@
import "InvidiousService.bs" import "InvidiousService.bs"
import "InvidiousSubscriptionsTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
function Init() function Init()
m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml")) m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml"))
@@ -17,3 +19,18 @@ end function
function SetAuthToken(token as string, instance as string, username as dynamic) function SetAuthToken(token as string, instance as string, username as dynamic)
m.service.SetAuthToken(token, instance, username) m.service.SetAuthToken(token, instance, username)
end function end function
function OnAuthTokenChange() as void
m.top.refreshSubscribtions = true
end function
function GetSubscriptions() as void
if m.subscriptionsTask <> invalid
m.subscriptionsTask.cancel = true
end if
if m.top.authToken = invalid
m.top.subscriptions = invalid
return
end if
m.subscriptionsTask = StartAsyncTask(InvidiousSubscriptionsTask, { invidious: m.top })
end function
@@ -4,8 +4,10 @@
<field id="preferences" type="node" /> <field id="preferences" type="node" />
<field id="webServer" type="node" /> <field id="webServer" type="node" />
<field id="authToken" type="assocarray" /> <field id="authToken" type="assocarray" onChange="OnAuthTokenChange" />
<field id="apiDefinitions" type="assocarray" /> <field id="apiDefinitions" type="assocarray" />
<field id="refreshSubscribtions" type="boolean" alwaysNotify="true" onChange="GetSubscriptions" />
<field id="subscriptions" type="assocarray" />
<function name="GetCurrentInstance" /> <function name="GetCurrentInstance" />
<function name="GetAuthorizeTokenLink" /> <function name="GetAuthorizeTokenLink" />
@@ -22,8 +22,10 @@ namespace Invidious
const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions"
const AUTH_SCOPES = "GET:feed,GET:playlists*,POST:tokens/register,POST:tokens/unregister,POST:history*" const AUTH_SCOPES = "GET:feed,GET:playlists*,POST:tokens/*,POST:history*,:subscriptions*"
const AUTH_PLAYLISTS = "/api/v1/auth/playlists" const AUTH_PLAYLISTS = "/api/v1/auth/playlists"
const AUTH_SUBSCRIPTIONS = "/api/v1/auth/subscriptions"
const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister" const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister"
const AUTH_TOKENS_REGISTER = "/api/v1/auth/tokens/register" const AUTH_TOKENS_REGISTER = "/api/v1/auth/tokens/register"
@@ -481,6 +483,47 @@ namespace Invidious
return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}`
end function end function
function GetSubscriptions(cancellation = invalid as object) as object
authToken = m.node.authToken
if authToken = invalid
return invalid
end if
url = `${authToken.instance}${Invidious.AUTH_SUBSCRIPTIONS}`
request = HttpClient.Get(url)
request.Headers(m.GetAuthenticationHeaders(authToken.token))
request.Cancellation(cancellation)
return request.Await()
end function
function Subscribe(channelId as string, cancellation = invalid as object) as object
authToken = m.node.authToken
if authToken = invalid
return invalid
end if
url = `${authToken.instance}${Invidious.AUTH_SUBSCRIPTIONS}/${channelId}`
request = HttpClient.Post(url, "")
request.Headers(m.GetAuthenticationHeaders(authToken.token))
request.Cancellation(cancellation)
return request.Await()
end function
function Unsubscribe(channelId as string, cancellation = invalid as object) as object
authToken = m.node.authToken
if authToken = invalid
return invalid
end if
url = `${authToken.instance}${Invidious.AUTH_SUBSCRIPTIONS}/${channelId}`
request = HttpClient.Delete(url)
request.Headers(m.GetAuthenticationHeaders(authToken.token))
request.Cancellation(cancellation)
return request.Await()
end function
end class end class
end namespace end namespace
@@ -0,0 +1,42 @@
import "InvidiousService.bs"
import "pkg:/source/utils/Logging.bs"
@asynctask
function InvidiousSubscriptionsTask(input as object) as object
invidiousNode = input.invidious
service = new Invidious.InvidiousService(invidiousNode)
response = service.GetSubscriptions(m.top.cancellation)
if m.top.cancel
return invalid
end if
subscriptions = response.Json()
if not response.IsSuccess() or subscriptions = invalid
error = response.ErrorMessage()
LogError("Error getting subscriptions:", error)
return {
success: false,
error: response.ErrorMessage()
}
end if
if m.top.cancel
return invalid
end if
subscriptionsDict = {}
for each subscription in subscriptions
subscriptionsDict[subscription.authorId] = true
end for
invidiousNode.subscriptions = subscriptionsDict
return {
success: true
}
end function
@@ -7,6 +7,7 @@ import "pkg:/components/VideoFeed/VideoRowListContentTask.bs"
import "pkg:/components/VideoFeed/VideoRowListRowContentTask.bs" import "pkg:/components/VideoFeed/VideoRowListRowContentTask.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/source/utils/DisposableObserve.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/Types.bs"
@@ -18,12 +19,13 @@ function Init()
end function end function
function OnNodeReady() function OnNodeReady()
m.top.ObserveField("itemFocused", FuncName(OnItemFocused)) DisposableObserveField(m.top, "itemFocused", FuncName(OnItemFocused))
m.top.ObserveField("rowItemFocused", FuncName(OnRowItemFocused)) DisposableObserveField(m.top, "rowItemFocused", FuncName(OnRowItemFocused))
m.top.observeField("focusedChild", FuncName(OnFocusedChildChange)) DisposableObserveField(m.top, "focusedChild", FuncName(OnFocusedChildChange))
m.top.ObserveField("visible", FuncName(OnVisibilityChange)) DisposableObserveField(m.top, "visible", FuncName(OnVisibilityChange))
m.invidious.ObserveFieldScoped("authToken", FuncName(OnAuthTokenChange)) ' TODO:P1 Reload when subscriptions change
DisposableObserveFieldScoped(m.invidious, "authToken", FuncName(OnAuthTokenChange))
InitializeLongPressTimer() InitializeLongPressTimer()
end function end function
@@ -311,3 +313,7 @@ function OnkeyEvent(key as string, press as boolean) as boolean
end if end if
return false return false
end function end function
function OnDispose()
DisposeObservers()
end function
@@ -1,4 +1,4 @@
<component name="VideoRowList" extends="RowList" includes="AutoBind,ContextMenuProvider"> <component name="VideoRowList" extends="RowList" includes="AutoBind,ContextMenuProvider,Disposable">
<interface> <interface>
<field id="appController" type="node" bind="/AppController" /> <field id="appController" type="node" bind="/AppController" />
<field id="invidious" type="node" bind="/Invidious" /> <field id="invidious" type="node" bind="/Invidious" />
@@ -0,0 +1,5 @@
<component name="Disposable" extends="Group">
<interface>
<field id="dispose" type="boolean" alwaysNotify="true" onChange="OnDispose" />
</interface>
</component>
+10 -2
View File
@@ -226,14 +226,22 @@ namespace HttpClient
#end if #end if
if m._method = "POST" if m._method = "POST"
m._sent = m.urlTransfer.AsyncPostFromString(m._body) body = m._body
if body = invalid
body = ""
end if
m._sent = m.urlTransfer.AsyncPostFromString(body)
else if m._method = "GET" else if m._method = "GET"
m._sent = m.urlTransfer.AsyncGetToString() m._sent = m.urlTransfer.AsyncGetToString()
else if m._method = "HEAD" else if m._method = "HEAD"
m._sent = m.urlTransfer.AsyncHead() m._sent = m.urlTransfer.AsyncHead()
else else
m.urlTransfer.SetRequest(m._method) m.urlTransfer.SetRequest(m._method)
m._sent = m.urlTransfer.AsyncPostFromString(m._body) body = m._body
if body = invalid
body = ""
end if
m._sent = m.urlTransfer.AsyncPostFromString(body)
end if end if
if m._sent <> true if m._sent <> true
@@ -0,0 +1,39 @@
function DisposableObserveField(node as object, field as string, callback as string)
node.ObserveField(field, callback)
if m._disposableObserveFields = invalid
m._disposableObserveFields = []
end if
m._disposableObserveFields.push({
node: node,
field: field
})
end function
function DisposableObserveFieldScoped(node as object, field as string, callback as string)
node.ObserveFieldScoped(field, callback)
if m._disposableObserveFieldsScoped = invalid
m._disposableObserveFieldsScoped = []
end if
m._disposableObserveFieldsScoped.push({
node: node,
field: field
})
end function
function DisposeObservers()
if m._disposableObserveFields <> invalid
for each item in m._disposableObserveFields
item.node.UnobserveField(item.field)
next
m._disposableObserveFields = invalid
end if
if m._disposableObserveFieldsScoped <> invalid
for each item in m._disposableObserveFieldsScoped
item.node.UnobserveFieldScoped(item.field)
next
m._disposableObserveFieldsScoped = invalid
end if
end function