Search screen (#10)

* Search screen with keyboard and suggestions

* Clear search on visibility change

* Search page works

* Local search history

* Use built-in delete function

* Changelog

* Animation

* Adjust suggestions

* Web play video always goes to home
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2022-11-23 09:02:05 -05:00
1 parent 22dfe785f1
commit 740440c3a7
15 files changed
+395 -22

No files matched your search

@@ -93,7 +93,6 @@ function StartFetchTask() as void
end function
function OnHomeGridScreenContentLoaded()
m.top.SetFocus(true)
m.global.loadingIndicator.visible = false
end function
@@ -121,7 +120,7 @@ function OnHomeGridScreenVideoSelected(event as object) as void
end if
end if
ShowVideoScreenFull(videoId)
ShowVideoScreenFull(videoId, m.top)
end function
function CloseVideo()
@@ -136,15 +135,10 @@ end function
function OnPlayVideoCommand()
videoId = m.top.playvideo
CloseVideo()
ShowVideoScreenFull(videoId)
ShowVideoScreenFull(videoId, m.top)
end function
function OnHomeGridScreenVideoFocused(event as object) as void
' Skip loading the video after first load
if m.firstFocus = invalid
m.firstFocus = true
return
end if
if m.videoPlayer <> invalid
m.videoPlayer.control = "stop"
m.videoPlayer.visible = false
+17
View File
@@ -15,7 +15,9 @@ function Init()
m.homeGridScreen.SetFocus(true)
m.settingsScreen = m.top.findNode("SettingsScreen")
m.searchScreen = m.top.findNode("SearchScreen")
m.searchScreen.ObserveField("escape", "OnSearchScreenEscape")
m.navBar = m.top.findNode("NavBar")
m.navBar.ObserveField("escapeIndex", "OnNavBarEscape")
@@ -32,6 +34,11 @@ function InitWebServer()
m.serverTask.control = "RUN"
end function
function OnSearchScreenEscape()
m.navBar.SetFocus(true)
m.navBar.focusIndex = 0
end function
function OnHomeGridScreenEscape()
m.navBar.SetFocus(true)
m.navBar.focusIndex = 1
@@ -78,6 +85,7 @@ function OnWebServerLogin()
m.homeGridScreen.id = "HomeGridScreen"
m.homeGridScreen.ObserveField("escape", "OnHomeGridScreenEscape")
' Selecting home
m.navBar.focusIndex = -1
m.homeGridScreen.focus = true
@@ -88,6 +96,15 @@ end function
function OnWebServerCommand()
if m.serverTask.command.StartsWith("play:")
' Selecting home
m.navBar.focusIndex = -1
m.homeGridScreen.focus = true
m.searchScreen.visible = false
m.homeGridScreen.visible = true
m.settingsScreen.visible = false
' Trigger play video from home
' TODO: allow returning to current screen, instead of home
m.homeGridScreen.playvideo = m.serverTask.command.Mid(5)
end if
end function
+2 -2
View File
@@ -24,8 +24,8 @@ function onKeyEvent(key as string, press as boolean) as boolean
m.top.focusIndex = target
return true
else if key = "right"
' Can only escape to home screen, at index 1
allowedEscapes = { "1": true }
' Can only escape to search screen or home screen
allowedEscapes = { "0": true, "1": true }
if allowedEscapes[StrI(m.top.focusIndex).Trim()] = true
m.top.escapeIndex = m.top.focusIndex
m.top.focusIndex = -1
@@ -0,0 +1,41 @@
function SaveSearchHistory(q as string, maxItems = 10 as integer)
history = RegistryUtils.Read("search_history")
if history <> invalid
history = ParseJson(history)
else
history = []
end if
index = ArrayIndexOf(history, q)
if index <> -1
history.Delete(index)
end if
history.Unshift(q)
if history.Count() > maxItems
history.Pop()
end if
history = FormatJson(history)
RegistryUtils.Write("search_history", history)
end function
function GetSavedSearchHistory(q as string) as object
history = RegistryUtils.Read("search_history")
if history = invalid
return []
end if
history = ParseJson(history)
if q = ""
return history
end if
result = []
for each keyword in history
if keyword.InStr(q) = 0
result.push(keyword)
end if
end for
return result
end function
+176
View File
@@ -0,0 +1,176 @@
import "pkg:/source/roku_modules/rokupromise/Promise.brs"
import "pkg:/components/VideoPlayer/Video.bs"
import "pkg:/components/SearchScreen/SearchHistory.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.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
if m.rowlist.content <> invalid
m.rowlist.SetFocus(true)
ScrollDown()
return
end if
if m.suggestions.buttons.Count() > 0
m.suggestions.SetFocus(true)
return
end if
m.keyboard.SetFocus(true)
end if
end function
function OnTextChange() as void
text = m.keyboard.text
createTaskPromise("SearchSuggestionsTask", {
input: {
q: text
}
}).then(function(task as object) as void
' In case this is an old request, discard suggestions
if task.output.q <> m.keyboard.text
return
end if
suggestions = task.output.response
if suggestions <> invalid
' TODO: fix race condition where button group gains focus before suggestions are set
m.suggestions.buttons = suggestions
end if
end function)
end function
function OnSearchButtonSelected()
selected = m.suggestions.buttonSelected
text = m.suggestions.buttons[selected]
Search(text)
end function
function Search(text as string)
SaveSearchHistory(text)
createTaskPromise("SearchTask", {
input: {
q: text
}
}).then(function(task as object) as void
rootChildren = task.output.rootChildren
contentNode = CreateObject("roSGNode", "ContentNode")
contentNode.Update({
children: rootChildren
}, true)
m.rowList.content = contentNode
m.rowList.SetFocus(true)
m.rowList.jumpToRowItem = [0, 0]
ScrollDown()
end function)
end function
function OnVisibilityChange()
if m.top.visible = false
ClearSearch()
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
ShowVideoScreenFull(videoId, m.top)
end function
function OnkeyEvent(key as string, press as boolean) as boolean
if press = false
return false
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
+41 -9
View File
@@ -1,15 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="SearchScreen" extends="Group">
<interface>
<field id="escape" type="string" alwaysNotify="true" />
<field id="focus" type="boolean" alwaysNotify="true" />
</interface>
<children>
<Label
text="Search feature coming soon..."
width="1280"
height="720"
translation="[0, 0]"
horizAlign="center"
vertAlign="center"
font="font:MediumBoldSystemFont"
/>
<Group id="container">
<MiniKeyboard
id="keyboard"
translation="[670,110]"
/>
<ButtonGroup
id="suggestions"
maxWidth="450"
buttonHeight="50"
translation="[160,100]"
/>
<RowList
itemComponentName="VideoRowCell"
id="rowList"
translation="[0,620]"
numRows="1"
rowitemSize="[[350,196]]"
rowItemSpacing="[[25,0]]"
itemSize="[1280,326]"
itemSpacing="[0,65]"
rowLabelOffset="[[95,20]]"
focusXOffset="[95]"
showRowLabel="[true]"
rowFocusAnimationStyle="floatingfocus"
focusBitmapUri="pkg:/images/focus_grid.9.png"
/>
<Animation
id="scrollAnimation"
duration="0.3">
<Vector2DFieldInterpolator
id="scrollAnimationInterpolator"
key="[ 0.0, 0.5, 1.0 ]"
keyValue="[ [0.0,0.0], [0.0,-160.0], [0.0,-320.0]]"
fieldToInterp="container.translation" />
</Animation>
</Group>
</children>
</component>
@@ -0,0 +1,34 @@
import "pkg:/source/services/Invidious.bs"
import "pkg:/components/SearchScreen/SearchHistory.bs"
function Init()
m.top.functionName = "TaskMain"
end function
function TaskMain()
input = m.top.getField("input")
q = input.q
searchSuggestsions = q <> "" ? Invidious.SearchSuggestions(q) : invalid
history = GetSavedSearchHistory(q)
if searchSuggestsions <> invalid
history = MergeSearchSuggestions(history, searchSuggestsions.suggestions)
end if
m.top.setField("output", {
q: q,
response: history
})
end function
function MergeSearchSuggestions(history as object, queryResults as object, maxItems = 10 as integer) as object
if queryResults = invalid
return history
end if
while history.Count() < maxItems and queryResults.Count() > 0
history.push(queryResults.shift())
end while
return history
end function
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="SearchSuggestionsTask" extends="Task">
<interface>
<field id="input" type="assocarray" />
<field id="output" type="assocarray" />
</interface>
</component>
+49
View File
@@ -0,0 +1,49 @@
import "pkg:/source/services/Invidious.bs"
function Init()
m.top.functionName = "TaskMain"
end function
function TaskMain()
input = m.top.getField("input")
q = input.q
rootChildren = []
response = Invidious.Search(q, { type: "video" })
rowContent = GetCategoryContent(`Search - ${q}`, response)
if rowContent <> invalid
rootChildren.Push(rowContent)
end if
m.top.setField("output", {
q: q,
rootChildren: rootChildren
})
end function
function GetCategoryContent(category as string, json as object) as object
if json <> invalid and json.Count() > 0
row = {}
row.title = category
row.children = []
for each item in json ' parse items and push them to row
itemData = GetItemData(item)
if itemData <> invalid
row.children.Push(itemData)
end if
end for
return row
end if
return invalid
end function
function GetItemData(video as object) as object
' TODO: handle playlists and channels
if video.videoId = invalid
return invalid
end if
return video
end function
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="SearchTask" extends="Task">
<interface>
<field id="input" type="assocarray" />
<field id="output" type="assocarray" />
</interface>
</component>
+2 -1
View File
@@ -12,8 +12,9 @@ function ShowVideoScreenEmbedded(videoId as string, thumbnailNode as object)
ShowVideoScreen(videoId)
end function
function ShowVideoScreenFull(videoId as string)
function ShowVideoScreenFull(videoId as string, sender as object)
m.videoPlayer = CreateObject("roSGNode", "VideoPlayer")
m.videoPlayer.sender = sender
AddVideoFields()
m.videoPlayer.embedded = false
+2 -2
View File
@@ -13,9 +13,9 @@ function OnkeyEvent(key as string, press as boolean) as boolean
end function
function CloseVideo()
homeGridScreen = m.top.getParent().findNode("HomeGridScreen")
sender = m.top.sender
m.top.control = "stop"
m.top.visible = false
m.top.getParent().removeChild(m.videoPlayer)
homeGridScreen.focus = true
sender.focus = true
end function
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="VideoPlayer" extends="Video">
<interface>
<field id="sender" type="node" />
</interface>
</component>
+9
View File
@@ -49,3 +49,12 @@ function GetDeviceInfo() as object
end if
return m.deviceInfo
end function
function ArrayIndexOf(array as object, value as string) as integer
for i = 0 to array.Count() - 1
if array[i] = value
return i
end if
end for
return -1
end function