Files
playlet-piped/playlet-lib/src/components/SearchScreen/SearchScreen.bs
T
Brahim Hadricheandgithub-action linter 8f8c7d38a8 Preferences / Web App (#79)
* Add basic router

* Add a preferences file

* small changes

* Basic settings navigation menu

* Lint fix

* Add bunch of controls

* Lint fix

* StringControl

* Lint fix

* Add description to keyboard

* Settings saving and loading

* Lint fix

* Settings migration stuff

* GET web api

* Lint fix

* PUT and DELETE (I really fucking hate BrightScript)

* plug in sponsorblock settings

* autoplay, quality, and video proxying

* Remove unused stuff

* Lint fix

* Invidious instance stuff

* cache public instances on start

* Start loading home screen after fetching instances

* Move web app out of settings to its own screen

* picture in picture when in settings

* Web app (#80)

* Update instance stuff

* Update dependencies

* Add basic layout

* Info and settings page, missing styling

* Navbar fixes

* Settings page formatting

* Theme stuff

* Feedback link

* Fix username badge

* Fix svelte warnings

* A wip search page

* Add a couple of api functions

* Wip home screen

* user country code

* Expose token and proxy auth calls

* Use auth requests server side

* Some cleanup

* Add svelte vs code extension

* remove old web app

* More layout stuff

* Tweak for string control

* Filter out old search suggestions

* Search history stuff

* img size for lazy loading

* Basic modal to play video

* Lint fix

* Dependencies bump

* Add video duration and LIVE indicator to cell

* Delete legacy stuff

* Add dev menu

* Lint fix

* Video cell layout

* Cell stuff

* prefs visibility

* Modal layout

* Drag and drop functionality

* Clear search history

* Move web calls to main app

* changelog

* changelog

* Mostly comments

* Lint fix

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2023-06-24 15:33:09 -04:00

234 lines
6.2 KiB
Plaintext

import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/components/VideoPlayer/Video.bs"
import "pkg:/source/services/SearchHistory.bs"
import "pkg:/components/SearchScreen/SearchSuggestionsTask.bs"
import "pkg:/components/SearchScreen/SearchTask.bs"
function Init()
m.container = m.top.FindNode("container")
m.scrollAnimation = m.top.FindNode("scrollAnimation")
m.scrollAnimationInterpolator = m.top.FindNode("scrollAnimationInterpolator")
m.keyboard = m.top.FindNode("keyboard")
m.suggestions = m.top.FindNode("suggestions")
m.rowList = m.top.FindNode("rowList")
m.keyboard.ObserveField("text", "OnTextChange")
m.suggestions.ObserveField("buttonSelected", "OnSearchButtonSelected")
m.suggestions.ObserveField("buttonFocused", "OnSearchButtonFocused")
m.rowList.ObserveField("rowItemSelected", "OnSearchVideoSelected")
m.top.ObserveField("focus", "OnFocusChange")
m.top.ObserveField("visible", "OnVisibilityChange")
' Trigger suggestions
OnTextChange()
end function
function OnFocusChange() as void
if m.top.focus <> true
return
end if
if m.rowlist.content <> invalid
m.rowlist.SetFocus(true)
ScrollDown()
return
end if
if m.suggestions.buttons.Count() > 0
if m.suggestions.buttonFocused >= m.suggestions.buttons.Count()
m.suggestions.focusButton = 0
end if
m.suggestions.SetFocus(true)
return
end if
m.keyboard.SetFocus(true)
end function
function OnTextChange() as void
if IsNodeFocused("suggestions")
return
end if
if m.searchSuggestionsTask <> invalid
m.searchSuggestionsTask.control = "stop"
end if
m.searchSuggestionsTask = StartAsyncTask(SearchSuggestionsTask, {
q: m.keyboard.text
}, OnSearchSuggestionsTaskResults)
end function
function OnSearchSuggestionsTaskResults(output as object) as void
' In case this is an old request, discard suggestions
q = output.result.q
if q <> m.keyboard.text
return
end if
suggestions = output.result.response
if suggestions = invalid
return
end if
if q.len() > 0 and ArrayIndexOf(suggestions, q) = -1
suggestions.Unshift(q)
end if
' Capture current focused button and restore it after resetting the buttons
buttonFocused = -1
if IsNodeFocused("suggestions")
buttonFocused = m.suggestions.buttonFocused
end if
if buttonFocused >= m.suggestions.buttons.Count()
buttonFocused = m.suggestions.buttons.Count() - 1
end if
m.suggestions.buttons = suggestions
if buttonFocused <> -1
m.suggestions.focusButton = buttonFocused
end if
end function
function OnSearchButtonSelected()
selected = m.suggestions.buttonSelected
text = m.suggestions.buttons[selected]
SearchHistory.Save(text)
Search(text)
end function
function OnSearchButtonFocused() as void
if not IsNodeFocused("suggestions")
return
end if
selected = m.suggestions.buttonFocused
text = m.suggestions.buttons[selected]
m.keyboard.text = text
end function
function Search(text as string)
if m.searchTask <> invalid
m.searchTask.control = "stop"
end if
m.global.loadingIndicator.visible = true
m.searchTask = StartAsyncTask(SearchTask, {
q: text
}, OnSearchTaskResults)
end function
function OnSearchTaskResults(output as object)
rootChildren = output.result.rootChildren
contentNode = CreateObject("roSGNode", "ContentNode")
contentNode.Update({
children: rootChildren
}, true)
m.rowList.content = contentNode
m.rowList.SetFocus(true)
m.rowList.jumpToRowItem = [0, 0]
ScrollDown()
m.global.loadingIndicator.visible = false
end function
function OnVisibilityChange()
if m.top.visible = false
ClearSearch()
else
OnTextChange()
end if
end function
function ClearSearch()
m.keyboard.text = ""
m.rowlist.content = invalid
ScrollUp()
end function
function IsNodeFocused(id as string) as boolean
return m.top.focusedChild?.id = id or m.container.focusedChild?.id = id
end function
function ScrollUp() as void
destination = m.scrollAnimationInterpolator.keyValue[0][1]
current = m.container.translation[1]
if current = destination
return
end if
m.scrollAnimationInterpolator.reverse = true
m.scrollAnimation.control = "start"
end function
function ScrollDown() as void
destination = m.scrollAnimationInterpolator.keyValue[2][1]
current = m.container.translation[1]
if current = destination
return
end if
m.scrollAnimationInterpolator.reverse = false
m.scrollAnimation.control = "start"
end function
function OnSearchVideoSelected(event as object)
grid = event.GetRoSGNode()
selectedPair = event.GetData()
selectedRow = selectedPair[0]
selectedRowItem = selectedPair[1]
rowContent = grid.content.GetChild(selectedRow)
itemContent = rowContent.GetChild(selectedRowItem)
videoId = itemContent.videoId
PlayVideo(videoId, {
metadata: itemContent,
sender: m.top
})
end function
function OnkeyEvent(key as string, press as boolean) as boolean
if press = false
return false
end if
if key = "options"
if ToggleVideoPictureInPicture(m.top)
return true
end if
end if
if key = "back"
ScrollUp()
m.top.escape = key
return true
end if
if key = "left"
if IsNodeFocused("keyboard") and m.suggestions.buttons.Count() > 0
m.suggestions.SetFocus(true)
return true
end if
ScrollUp()
m.top.escape = key
return true
end if
if key = "right"
if IsNodeFocused("suggestions")
m.keyboard.SetFocus(true)
return true
end if
return false
end if
if key = "down"
if m.rowlist.content <> invalid
ScrollDown()
m.rowlist.SetFocus(true)
return true
end if
return false
end if
if key = "up"
if IsNodeFocused("rowList")
ScrollUp()
m.suggestions.SetFocus(true)
return true
end if
return false
end if
return false
end function