diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7102d5c5..94008295 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Metadata to Video Player
- Support for DASH and Live videos
- Log out button to web app
+- Search Screen
### Fixed
- Bug where Settings page and home page are both visible, if settings page is selected, and user logs in.
diff --git a/src/components/HomeGridScreen/HomeGridScreen.bs b/src/components/HomeGridScreen/HomeGridScreen.bs
index 08a141f1..12e548e9 100644
--- a/src/components/HomeGridScreen/HomeGridScreen.bs
+++ b/src/components/HomeGridScreen/HomeGridScreen.bs
@@ -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
diff --git a/src/components/MainScene.bs b/src/components/MainScene.bs
index e384c9bf..c0e841fa 100644
--- a/src/components/MainScene.bs
+++ b/src/components/MainScene.bs
@@ -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
diff --git a/src/components/NavBar/NavBar.bs b/src/components/NavBar/NavBar.bs
index c0c98792..444997e1 100644
--- a/src/components/NavBar/NavBar.bs
+++ b/src/components/NavBar/NavBar.bs
@@ -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
diff --git a/src/components/SearchScreen/SearchHistory.bs b/src/components/SearchScreen/SearchHistory.bs
new file mode 100644
index 00000000..5f7dcd9d
--- /dev/null
+++ b/src/components/SearchScreen/SearchHistory.bs
@@ -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
diff --git a/src/components/SearchScreen/SearchScreen.bs b/src/components/SearchScreen/SearchScreen.bs
new file mode 100644
index 00000000..564cdf45
--- /dev/null
+++ b/src/components/SearchScreen/SearchScreen.bs
@@ -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
diff --git a/src/components/SearchScreen/SearchScreen.xml b/src/components/SearchScreen/SearchScreen.xml
index fb4642d4..98631de9 100644
--- a/src/components/SearchScreen/SearchScreen.xml
+++ b/src/components/SearchScreen/SearchScreen.xml
@@ -1,15 +1,47 @@
+
+
+
+
+
-
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/components/SearchScreen/SearchSuggestionsTask.bs b/src/components/SearchScreen/SearchSuggestionsTask.bs
new file mode 100644
index 00000000..a0fc0cfd
--- /dev/null
+++ b/src/components/SearchScreen/SearchSuggestionsTask.bs
@@ -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
diff --git a/src/components/SearchScreen/SearchSuggestionsTask.xml b/src/components/SearchScreen/SearchSuggestionsTask.xml
new file mode 100644
index 00000000..eeb28593
--- /dev/null
+++ b/src/components/SearchScreen/SearchSuggestionsTask.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
diff --git a/src/components/SearchScreen/SearchTask.bs b/src/components/SearchScreen/SearchTask.bs
new file mode 100644
index 00000000..996efae6
--- /dev/null
+++ b/src/components/SearchScreen/SearchTask.bs
@@ -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
diff --git a/src/components/SearchScreen/SearchTask.xml b/src/components/SearchScreen/SearchTask.xml
new file mode 100644
index 00000000..83bb86ed
--- /dev/null
+++ b/src/components/SearchScreen/SearchTask.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
diff --git a/src/components/VideoPlayer/Video.bs b/src/components/VideoPlayer/Video.bs
index 470644af..ed1e5413 100644
--- a/src/components/VideoPlayer/Video.bs
+++ b/src/components/VideoPlayer/Video.bs
@@ -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
diff --git a/src/components/VideoPlayer/VideoPlayer.bs b/src/components/VideoPlayer/VideoPlayer.bs
index 73c9c36f..13cf35f2 100644
--- a/src/components/VideoPlayer/VideoPlayer.bs
+++ b/src/components/VideoPlayer/VideoPlayer.bs
@@ -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
diff --git a/src/components/VideoPlayer/VideoPlayer.xml b/src/components/VideoPlayer/VideoPlayer.xml
index 204d0827..16065e3c 100644
--- a/src/components/VideoPlayer/VideoPlayer.xml
+++ b/src/components/VideoPlayer/VideoPlayer.xml
@@ -1,4 +1,7 @@
+
+
+
\ No newline at end of file
diff --git a/src/source/utils/General.bs b/src/source/utils/General.bs
index 7e4c092c..1ab9b4c2 100644
--- a/src/source/utils/General.bs
+++ b/src/source/utils/General.bs
@@ -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