Skip to content

Commit

Permalink
[components] For mozilla-mobile/android-components#191 - Adds SearchS…
Browse files Browse the repository at this point in the history
…uggestionClient and SuggestionParser

UltraBlame original commit: 540c1fe28641e5adf9582362b1343daee2c52977
  • Loading branch information
marco-c committed May 31, 2024
1 parent 03088b2 commit 808e238
Show file tree
Hide file tree
Showing 4 changed files with 831 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package
mozilla
.
components
.
browser
.
search
import
kotlinx
.
coroutines
.
experimental
.
async
import
kotlinx
.
coroutines
.
experimental
.
Deferred
import
mozilla
.
components
.
browser
.
search
.
parser
.
googleParser
typealias
SearchSuggestionFetcher
=
suspend
(
url
:
String
)
-
>
String
?
class
SearchSuggestionClient
(
private
val
searchEngine
:
SearchEngine
private
val
fetcher
:
SearchSuggestionFetcher
)
{
suspend
fun
getSuggestions
(
query
:
String
)
:
List
<
String
>
?
{
val
suggestionsURL
=
searchEngine
.
buildSuggestionsURL
(
query
)
?
:
return
null
return
fetcher
(
suggestionsURL
)
?
.
let
{
googleParser
(
it
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package
mozilla
.
components
.
browser
.
search
.
parser
import
org
.
json
.
JSONArray
fun
JSONArray
.
asSequence
(
)
:
Sequence
<
Any
>
{
return
object
:
Sequence
<
Any
>
{
override
fun
iterator
(
)
=
object
:
Iterator
<
Any
>
{
val
it
=
(
0
until
this
asSequence
.
length
(
)
)
.
iterator
(
)
override
fun
next
(
)
:
Any
{
val
i
=
it
.
next
(
)
return
this
asSequence
.
get
(
i
)
}
override
fun
hasNext
(
)
=
it
.
hasNext
(
)
}
}
}
typealias
Parser
=
(
String
)
-
>
List
<
String
>
val
googleParser
:
Parser
=
{
input
-
>
JSONArray
(
input
)
.
getJSONArray
(
1
)
.
asSequence
(
)
.
map
{
it
as
?
String
}
.
filterNotNull
(
)
.
toList
(
)
}
Loading

0 comments on commit 808e238

Please sign in to comment.