Files
playlet-piped/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs
T
2023-09-16 19:01:35 -04:00

302 lines
9.0 KiB
Plaintext

import "pkg:/components/Navigation/Navigation.bs"
import "pkg:/components/Screens/SearchScreen/SearchSuggestionsTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/source/services/SearchHistory.bs"
import "pkg:/source/utils/LoadingScreen.bs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/ErrorUtils.bs"
function Init()
m.container = m.top.FindNode("container")
m.keyboard = m.top.FindNode("keyboard")
m.rowList = m.top.FindNode("rowList")
m.scrollAnimation = m.top.FindNode("scrollAnimation")
m.scrollAnimationInterpolator = m.top.FindNode("scrollAnimationInterpolator")
m.suggestions = m.top.FindNode("suggestions")
m.filtersButton = m.top.FindNode("filtersButton")
textEditBox = m.keyboard.textEditBox
textEditBox.voiceEntryType = "generic"
textEditBox.voiceEnabled = true
m.top.filters = {
date: "",
type: "",
duration: "",
features: [],
sort_by: ""
}
m.top.ObserveField("filters", FuncName(OnFiltersChange))
end function
function OnNodeReady()
m.log = new log.Logger("SearchScreen")
m.rowlist@.BindNode(invalid)
m.keyboard.ObserveField("text", FuncName(OnTextChange))
m.suggestions.ObserveField("buttonFocused", FuncName(OnSearchButtonFocused))
m.suggestions.ObserveField("buttonSelected", FuncName(OnSearchButtonSelected))
m.filtersButton.ObserveField("buttonSelected", FuncName(OnFiltersButtonSelected))
m.top.ObserveField("visible", FuncName(OnVisibilityChange))
m.rowlist.ObserveFieldScoped("onError", FuncName(OnSearchError))
SetNavigation(invalid, "back", m.navBar, function()
ScrollUp()
end function)
SetNavigation(m.suggestions, "right", m.keyboard)
SetNavigation(m.filtersButton, "right", m.keyboard)
SetNavigation(m.keyboard, "left", m.suggestions)
SetNavigation(m.suggestions, "left", m.navBar)
SetNavigation(m.filtersButton, "left", m.navBar)
SetNavigation(m.keyboard, "left", m.navBar)
SetNavigation(m.rowList, "left", m.navBar, function()
ScrollUp()
end function)
SetNavigation(m.filtersButton, "down", m.suggestions)
SetNavigation(m.filtersButton, "down", m.keyboard)
SetNavigation(invalid, "down", m.rowList, function()
ScrollDown()
end function)
SetNavigation(m.rowList, "up", m.suggestions, function()
ScrollUp()
end function)
SetNavigation(m.suggestions, "up", m.filtersButton)
SetNavigation(m.keyboard, "up", m.filtersButton)
m.currentNavigtionNode = m.suggestions
' Trigger suggestions
OnTextChange()
end function
function OnFocusChange() as void
if not m.top.focus
return
end if
if m.currentNavigtionNode.isSameNode(m.suggestions) and not m.suggestions.focusable
m.currentNavigtionNode = m.keyboard
end if
NodeSetFocus(m.currentNavigtionNode, true)
if m.currentNavigtionNode.isSameNode(m.suggestions)
OnSearchButtonFocused()
end if
end function
function OnTextChange() as void
if m.suggestions.isInFocusChain()
return
end if
if m.searchSuggestionsTask <> invalid
m.searchSuggestionsTask.cancel = true
end if
m.searchSuggestionsTask = StartAsyncTask(SearchSuggestionsTask, {
q: m.keyboard.text,
invidious: m.invidious
}, OnSearchSuggestionsTaskResults)
end function
function OnSearchSuggestionsTaskResults(output as object) as void
if m.searchSuggestionsTask <> invalid and m.searchSuggestionsTask.id = output.task.id
m.searchSuggestionsTask = invalid
end if
if output.cancelled
return
end if
if not output.success
m.log.error(ErrorUtils.Format(output.error))
return
end if
' 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 ArrayUtils.IndexOfString(suggestions, q) = -1
suggestions.Unshift(q)
end if
' Capture current focused button and restore it after resetting the buttons
buttonFocused = -1
if m.suggestions.isInFocusChain()
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
m.suggestions.focusable = m.suggestions.buttons.Count() > 0
end function
function OnFiltersButtonSelected()
searchFilters = CreateObject("roSGNode", "SearchFilters")
m.appController@.PushScreen(searchFilters)
searchFilters.searchScreen = m.top
searchFilters@.BindNode(invalid)
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 m.suggestions.isInFocusChain()
return
end if
buttonCount = m.suggestions.buttons.Count()
selected = m.suggestions.buttonFocused
if selected >= buttonCount
m.suggestions.focusButton = 0
end if
selected = m.suggestions.buttonFocused
if selected >= m.suggestions.buttons.Count() or selected < 0
return
end if
text = m.suggestions.buttons[selected]
m.keyboard.text = text
end function
function OnFiltersChange()
filterCount = 0
filters = m.top.filters
for each filter in filters
value = filters[filter]
if IsString(value) and value.len() > 0
filterCount++
else if IsArray(value) and value.Count() > 0
filterCount += value.Count()
end if
end for
if filterCount > 0
m.filtersButton.text = `Filters (${filterCount})`
else
m.filtersButton.text = "Filters"
end if
end function
function Search(text as string)
m.rowlist.UnobserveFieldScoped("someContentReady")
' TODO:P1 someContentReady only fires if there are search results.
' The case where search results are empty is not handled.
m.rowlist.ObserveFieldScoped("someContentReady", FuncName(OnSearchContentReady))
ShowLoadingScreen()
requestData = {
title: `Search - ${text}`,
apiType: "Invidious",
endpoint: "search",
queryParams: {
q: text
}
}
filters = m.top.filters
for each filter in filters
value = filters[filter]
if IsString(value) and value.len() > 0
requestData.queryParams[filter] = value
else if IsArray(value) and value.Count() > 0
' TODO:P1 this is http client implementation details leaking
' We need a declarative way to set array types
requestData.queryParams[filter] = {
value: value,
__arrayType: HttpClient.QueryParamArrayType.CommaSeparated
}
end if
end for
m.rowList.contentData = [requestData]
end function
function OnSearchContentReady() as void
if not m.rowlist.someContentReady
return
end if
m.rowlist.UnobserveFieldScoped("someContentReady")
m.rowlist.focusable = true
m.currentNavigtionNode = m.rowList
NodeSetFocus(m.rowlist, true)
' There's a bug with the floating focus indicator. If the rowList receives focus after the
' content is set, the focus indicator will be "off". This is why we set the content again.
m.rowlist.content = m.rowlist.content
ScrollDown()
HideLoadingScreen()
end function
function OnSearchError()
m.rowlist.contentData = invalid
m.rowlist.focusable = false
HideLoadingScreen()
end function
function OnVisibilityChange()
if m.top.visible = false
ClearSearch()
else
OnTextChange()
end if
end function
function ClearSearch()
m.keyboard.text = ""
m.rowlist.contentData = invalid
m.rowlist.focusable = false
ScrollUp()
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 OnkeyEvent(key as string, press as boolean) as boolean
navigation = NavigationKeyHandler(key, press)
if navigation.handled
m.currentNavigtionNode = navigation.node
if m.currentNavigtionNode.isSameNode(m.navBar)
m.currentNavigtionNode = m.suggestions
end if
if m.currentNavigtionNode.isSameNode(m.suggestions)
OnSearchButtonFocused()
end if
return true
end if
return false
end function