mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-12 17:08:59 -04:00
Refactoring the search functionality to support the latest wrapper
This commit is contained in:
parent
a3ae9e942b
commit
c34c0c201e
@ -107,6 +107,7 @@ class ZimFileReader constructor(
|
||||
else
|
||||
null
|
||||
val language: String get() = jniKiwixReader.getMetadata("Language")
|
||||
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
val tags: String
|
||||
get() = try {
|
||||
@ -130,16 +131,17 @@ class ZimFileReader constructor(
|
||||
fun searchSuggestions(prefix: String): SuggestionSearch =
|
||||
suggestionSearcher.suggest(prefix)
|
||||
|
||||
fun getNextSuggestion(suggestionSearch: SuggestionSearch?): SearchSuggestion? {
|
||||
fun getNextSuggestion(suggestionSearch: SuggestionSearch?): List<SearchSuggestion> {
|
||||
val suggestionList = mutableListOf<SearchSuggestion>()
|
||||
val suggestionIterator =
|
||||
suggestionSearch?.getResults(0, suggestionSearch.estimatedMatches.toInt())
|
||||
if (suggestionIterator != null) {
|
||||
while (suggestionIterator.hasNext()) {
|
||||
val suggestionItem = suggestionIterator.next()
|
||||
return SearchSuggestion(suggestionItem.title, suggestionItem.path)
|
||||
suggestionList.add(SearchSuggestion(suggestionItem.title, suggestionItem.path))
|
||||
}
|
||||
}
|
||||
return null
|
||||
return suggestionList
|
||||
}
|
||||
|
||||
fun getPageUrlFrom(title: String): String? =
|
||||
@ -277,6 +279,7 @@ class ZimFileReader constructor(
|
||||
|
||||
fun dispose() {
|
||||
jniKiwixReader.dispose()
|
||||
suggestionSearcher.dispose()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -49,7 +49,18 @@ class ZimSearchResultGenerator @Inject constructor() : SearchResultGenerator {
|
||||
reader.also { yield() }
|
||||
?.searchSuggestions(searchTerm)
|
||||
.also { yield() }
|
||||
.run { suggestionResults(reader, this) }
|
||||
.run {
|
||||
val suggestionList = mutableListOf<ZimSearchResultListItem>()
|
||||
val suggestionIterator =
|
||||
this?.getResults(0, this.estimatedMatches.toInt())
|
||||
if (suggestionIterator != null) {
|
||||
while (suggestionIterator.hasNext()) {
|
||||
val suggestionItem = suggestionIterator.next()
|
||||
suggestionList.add(ZimSearchResultListItem(suggestionItem.title))
|
||||
}
|
||||
}
|
||||
return@run suggestionList
|
||||
}
|
||||
|
||||
private suspend fun suggestionResults(
|
||||
reader: ZimFileReader?,
|
||||
@ -57,7 +68,9 @@ class ZimSearchResultGenerator @Inject constructor() : SearchResultGenerator {
|
||||
) = createList {
|
||||
yield()
|
||||
reader?.getNextSuggestion(suggestionSearch)
|
||||
?.let { ZimSearchResultListItem(it.title) }
|
||||
?.let {
|
||||
ZimSearchResultListItem(it[0].title)
|
||||
}
|
||||
}
|
||||
.distinct()
|
||||
.toList()
|
||||
|
Loading…
x
Reference in New Issue
Block a user