use typeahead search to handle searches on Android

This commit is contained in:
Satindar Dhillon 2022-12-20 08:08:58 -08:00
parent 20a88e78aa
commit e994462b61
4 changed files with 63 additions and 2 deletions

View file

@ -0,0 +1,15 @@
query TypeaheadSearch($query: String!) {
typeaheadSearch(query: $query) {
... on TypeaheadSearchSuccess {
items {
id
title
slug
siteName
}
}
... on TypeaheadSearchError {
errorCodes
}
}
}

View file

@ -1,6 +1,7 @@
package app.omnivore.omnivore.networking
import app.omnivore.omnivore.graphql.generated.SearchQuery
import app.omnivore.omnivore.graphql.generated.TypeaheadSearchQuery
import app.omnivore.omnivore.models.LinkedItem
import com.apollographql.apollo3.api.Optional
@ -9,6 +10,46 @@ data class SearchQueryResponse(
val items: List<LinkedItem>
)
suspend fun Networker.typeaheadSearch(
query: String
): SearchQueryResponse {
try {
val result = authenticatedApolloClient().query(
TypeaheadSearchQuery(query)
).execute()
val itemList = result.data?.typeaheadSearch?.onTypeaheadSearchSuccess?.items ?: listOf()
val items = itemList.map {
LinkedItem(
id = it.id,
title = it.title,
createdAt = "",
savedAt = "",
readAt = "",
updatedAt = "",
readingProgress = 0.0,
readingProgressAnchor = 0,
imageURLString = null,
pageURLString = "",
descriptionText = "",
publisherURLString = "",
siteName = it.siteName,
author = "",
publishDate = null,
slug = it.slug,
isArchived = false,
contentReader = null,
content = null
)
}
return SearchQueryResponse(null, items)
} catch (e: java.lang.Exception) {
return SearchQueryResponse(null, listOf())
}
}
suspend fun Networker.search(
cursor: String? = null,
limit: Int = 15,

View file

@ -59,7 +59,12 @@ class HomeViewModel @Inject constructor(
searchIdx += 1
// Execute the search
val searchResult = networker.search(cursor = cursor, query = searchQuery())
val searchResult =
if (searchTextLiveData.value != "") {
networker.typeaheadSearch(searchTextLiveData.value ?: "")
} else {
networker.search(cursor = cursor, query = searchQuery())
}
// Search results aren't guaranteed to return in order so this
// will discard old results that are returned while a user is typing.

View file

@ -47,7 +47,7 @@ fun LinkedItemCard(item: LinkedItem, onClickHandler: () -> Unit, actionHandler:
lineHeight = 20.sp
)
if (item.author != null) {
if (item.author != null && item.author != "") {
Text(
text = "By ${item.author}",
style = MaterialTheme.typography.bodyMedium,