searchScreen.brs (2712B)
1 '******************************************************************** 2 '** Home Media Server Application - Main 3 '** Copyright (c) 2013 Brian C. Lane All Rights Reserved. 4 '******************************************************************** 5 6 '******************************************************************** 7 '** Display search string 8 '******************************************************************** 9 Function searchScreen(screenItems As Object) As Object 10 ' screenItems is the double roArray of the screen objects. First row is the 11 ' search row, so ignore it. 12 13 port = CreateObject("roMessagePort") 14 screen = CreateObject("roSearchScreen") 15 screen.SetMessagePort(port) 16 screen.SetSearchTermHeaderText("Suggestions:") 17 screen.SetSearchButtonText("search") 18 screen.SetClearButtonEnabled(false) 19 20 screen.Show() 21 22 suggestions = invalid 23 while true 24 msg = wait(0, screen.GetMessagePort()) 25 if type(msg) = "roSearchScreenEvent" then 26 if msg.isScreenClosed() 27 return invalid 28 else if msg.isPartialResult() then 29 print "partial search: "; msg.GetMessage() 30 suggestions = getSuggestions(screenItems, msg.GetMessage()) 31 terms = getTitles(suggestions) 32 screen.SetSearchTerms(terms) 33 else if msg.isFullResult() 34 print "full search: "; msg.GetMessage() 35 return suggestions 36 else 37 print "Unknown event: "; msg.GetType(); " msg: ";sg.GetMessage() 38 end if 39 end if 40 end while 41 End Function 42 43 '******************************************************************** 44 '** Return an array of suggested movies objects 45 '******************************************************************** 46 Function getSuggestions(items As Object, needle As String) As Object 47 suggestions = CreateObject("roArray", 10, true) 48 ' iterate the whole list, gathering matches on Title 49 For i = 1 to items.Count()-1 50 For Each movie In items[i] 51 if Instr(1, LCase(movie.Title), needle) <> 0 then 52 suggestions.Push(movie) 53 end if 54 End For 55 End For 56 Sort(suggestions, function(k) 57 return LCase(k.Title) 58 end function) 59 return suggestions 60 End Function 61 62 '******************************************************************** 63 '** Return an array of titles 64 '******************************************************************** 65 Function getTitles(movies As Object) As Object 66 titles = CreateObject("roArray", 10, true) 67 For Each movie In movies 68 titles.Push(movie.Title) 69 End For 70 return titles 71 End Function 72