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
|
else
|
||||||
null
|
null
|
||||||
val language: String get() = jniKiwixReader.getMetadata("Language")
|
val language: String get() = jniKiwixReader.getMetadata("Language")
|
||||||
|
|
||||||
@Suppress("TooGenericExceptionCaught")
|
@Suppress("TooGenericExceptionCaught")
|
||||||
val tags: String
|
val tags: String
|
||||||
get() = try {
|
get() = try {
|
||||||
@ -130,16 +131,17 @@ class ZimFileReader constructor(
|
|||||||
fun searchSuggestions(prefix: String): SuggestionSearch =
|
fun searchSuggestions(prefix: String): SuggestionSearch =
|
||||||
suggestionSearcher.suggest(prefix)
|
suggestionSearcher.suggest(prefix)
|
||||||
|
|
||||||
fun getNextSuggestion(suggestionSearch: SuggestionSearch?): SearchSuggestion? {
|
fun getNextSuggestion(suggestionSearch: SuggestionSearch?): List<SearchSuggestion> {
|
||||||
|
val suggestionList = mutableListOf<SearchSuggestion>()
|
||||||
val suggestionIterator =
|
val suggestionIterator =
|
||||||
suggestionSearch?.getResults(0, suggestionSearch.estimatedMatches.toInt())
|
suggestionSearch?.getResults(0, suggestionSearch.estimatedMatches.toInt())
|
||||||
if (suggestionIterator != null) {
|
if (suggestionIterator != null) {
|
||||||
while (suggestionIterator.hasNext()) {
|
while (suggestionIterator.hasNext()) {
|
||||||
val suggestionItem = suggestionIterator.next()
|
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? =
|
fun getPageUrlFrom(title: String): String? =
|
||||||
@ -277,6 +279,7 @@ class ZimFileReader constructor(
|
|||||||
|
|
||||||
fun dispose() {
|
fun dispose() {
|
||||||
jniKiwixReader.dispose()
|
jniKiwixReader.dispose()
|
||||||
|
suggestionSearcher.dispose()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -49,7 +49,18 @@ class ZimSearchResultGenerator @Inject constructor() : SearchResultGenerator {
|
|||||||
reader.also { yield() }
|
reader.also { yield() }
|
||||||
?.searchSuggestions(searchTerm)
|
?.searchSuggestions(searchTerm)
|
||||||
.also { yield() }
|
.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(
|
private suspend fun suggestionResults(
|
||||||
reader: ZimFileReader?,
|
reader: ZimFileReader?,
|
||||||
@ -57,7 +68,9 @@ class ZimSearchResultGenerator @Inject constructor() : SearchResultGenerator {
|
|||||||
) = createList {
|
) = createList {
|
||||||
yield()
|
yield()
|
||||||
reader?.getNextSuggestion(suggestionSearch)
|
reader?.getNextSuggestion(suggestionSearch)
|
||||||
?.let { ZimSearchResultListItem(it.title) }
|
?.let {
|
||||||
|
ZimSearchResultListItem(it[0].title)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.distinct()
|
.distinct()
|
||||||
.toList()
|
.toList()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user