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