From c34c0c201edd1581e47a63d8eec0d643aba2241c Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 14 Jul 2023 19:09:30 +0530 Subject: [PATCH] Refactoring the search functionality to support the latest wrapper --- .../kiwixmobile/core/reader/ZimFileReader.kt | 9 ++++++--- .../search/viewmodel/SearchResultGenerator.kt | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt index d0ac0b78c..4210d5826 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt @@ -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 { + val suggestionList = mutableListOf() 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 { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchResultGenerator.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchResultGenerator.kt index 74ec4e32e..4b8f07655 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchResultGenerator.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchResultGenerator.kt @@ -49,7 +49,18 @@ class ZimSearchResultGenerator @Inject constructor() : SearchResultGenerator { reader.also { yield() } ?.searchSuggestions(searchTerm) .also { yield() } - .run { suggestionResults(reader, this) } + .run { + val suggestionList = mutableListOf() + 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()