Fixed: Search was not working properly, it always showing the current loaded page when we click on any searched item.

This commit is contained in:
MohitMaliFtechiz 2024-10-18 16:32:48 +05:30 committed by MohitMaliFtechiz
parent ecbf1b3813
commit c7f8d99eb0
4 changed files with 71 additions and 25 deletions

View File

@ -52,6 +52,9 @@ import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
import org.kiwix.kiwixmobile.core.main.CoreWebViewClient import org.kiwix.kiwixmobile.core.main.CoreWebViewClient
import org.kiwix.kiwixmobile.core.main.RestoreOrigin
import org.kiwix.kiwixmobile.core.main.RestoreOrigin.FromSearchScreen
import org.kiwix.kiwixmobile.core.main.RestoreOrigin.FromExternalLaunch
import org.kiwix.kiwixmobile.core.main.ToolbarScrollingKiwixWebView import org.kiwix.kiwixmobile.core.main.ToolbarScrollingKiwixWebView
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource import org.kiwix.kiwixmobile.core.reader.ZimReaderSource
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource.Companion.fromDatabaseValue import org.kiwix.kiwixmobile.core.reader.ZimReaderSource.Companion.fromDatabaseValue
@ -104,7 +107,9 @@ class KiwixReaderFragment : CoreReaderFragment() {
if (args.zimFileUri.isNotEmpty()) { if (args.zimFileUri.isNotEmpty()) {
tryOpeningZimFile(args.zimFileUri) tryOpeningZimFile(args.zimFileUri)
} else { } else {
manageExternalLaunchAndRestoringViewState() val restoreOrigin =
if (args.searchItemTitle.isNotEmpty()) FromSearchScreen else FromExternalLaunch
manageExternalLaunchAndRestoringViewState(restoreOrigin)
} }
} }
requireArguments().clear() requireArguments().clear()
@ -215,31 +220,53 @@ class KiwixReaderFragment : CoreReaderFragment() {
exitBook() exitBook()
} }
/**
* Restores the view state based on the provided JSON data and restore origin.
*
* Depending on the `restoreOrigin`, this method either restores the last opened ZIM file
* (if the launch is external) or skips re-opening the ZIM file when coming from the search screen,
* as the ZIM file is already set in the reader. The method handles setting up the ZIM file and bookmarks,
* and restores the tabs and positions from the provided data.
*
* @param zimArticles JSON string representing the list of articles to be restored.
* @param zimPositions JSON string representing the positions of the restored articles.
* @param currentTab Index of the tab to be restored as the currently active one.
* @param restoreOrigin Indicates whether the restoration is triggered from an external launch or the search screen.
*/
override fun restoreViewStateOnValidJSON( override fun restoreViewStateOnValidJSON(
zimArticles: String?, zimArticles: String?,
zimPositions: String?, zimPositions: String?,
currentTab: Int currentTab: Int,
restoreOrigin: RestoreOrigin
) { ) {
lifecycleScope.launch { when (restoreOrigin) {
val settings = FromExternalLaunch -> {
requireActivity().getSharedPreferences(SharedPreferenceUtil.PREF_KIWIX_MOBILE, 0) lifecycleScope.launch {
val zimReaderSource = fromDatabaseValue(settings.getString(TAG_CURRENT_FILE, null)) val settings =
if (zimReaderSource != null && zimReaderSource.canOpenInLibkiwix()) { requireActivity().getSharedPreferences(SharedPreferenceUtil.PREF_KIWIX_MOBILE, 0)
if (zimReaderContainer?.zimReaderSource == null) { val zimReaderSource = fromDatabaseValue(settings.getString(TAG_CURRENT_FILE, null))
openZimFile(zimReaderSource) if (zimReaderSource?.canOpenInLibkiwix() == true) {
Log.d( if (zimReaderContainer?.zimReaderSource == null) {
TAG_KIWIX, openZimFile(zimReaderSource)
"Kiwix normal start, Opened last used zimFile: -> ${zimReaderSource.toDatabase()}" Log.d(
) TAG_KIWIX,
} else { "Kiwix normal start, Opened last used zimFile: -> ${zimReaderSource.toDatabase()}"
zimReaderContainer?.zimFileReader?.let(::setUpBookmarks) )
} else {
zimReaderContainer?.zimFileReader?.let(::setUpBookmarks)
}
restoreTabs(zimArticles, zimPositions, currentTab)
} else {
getCurrentWebView()?.snack(string.zim_not_opened)
exitBook() // hide the options for zim file to avoid unexpected UI behavior
}
} }
} else {
getCurrentWebView()?.snack(string.zim_not_opened)
exitBook() // hide the options for zim file to avoid unexpected UI behavior
return@launch // book not found so don't need to restore the tabs for this file
} }
restoreTabs(zimArticles, zimPositions, currentTab)
FromSearchScreen -> {
restoreTabs(zimArticles, zimPositions, currentTab)
}
} }
} }

View File

@ -174,6 +174,9 @@ import java.util.Date
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.max import kotlin.math.max
import org.kiwix.kiwixmobile.core.main.RestoreOrigin.FromExternalLaunch
const val SEARCH_ITEM_TITLE_KEY = "searchItemTitle"
@Suppress("LargeClass") @Suppress("LargeClass")
abstract class CoreReaderFragment : abstract class CoreReaderFragment :
@ -2427,7 +2430,9 @@ abstract class CoreReaderFragment :
private fun isInvalidJson(jsonString: String?): Boolean = private fun isInvalidJson(jsonString: String?): Boolean =
jsonString == null || jsonString == "[]" jsonString == null || jsonString == "[]"
protected fun manageExternalLaunchAndRestoringViewState() { protected fun manageExternalLaunchAndRestoringViewState(
restoreOrigin: RestoreOrigin = FromExternalLaunch
) {
val settings = requireActivity().getSharedPreferences( val settings = requireActivity().getSharedPreferences(
SharedPreferenceUtil.PREF_KIWIX_MOBILE, SharedPreferenceUtil.PREF_KIWIX_MOBILE,
0 0
@ -2438,7 +2443,7 @@ abstract class CoreReaderFragment :
if (isInvalidJson(zimArticles) || isInvalidJson(zimPositions)) { if (isInvalidJson(zimArticles) || isInvalidJson(zimPositions)) {
restoreViewStateOnInvalidJSON() restoreViewStateOnInvalidJSON()
} else { } else {
restoreViewStateOnValidJSON(zimArticles, zimPositions, currentTab) restoreViewStateOnValidJSON(zimArticles, zimPositions, currentTab, restoreOrigin)
} }
} }
@ -2554,7 +2559,8 @@ abstract class CoreReaderFragment :
protected abstract fun restoreViewStateOnValidJSON( protected abstract fun restoreViewStateOnValidJSON(
zimArticles: String?, zimArticles: String?,
zimPositions: String?, zimPositions: String?,
currentTab: Int currentTab: Int,
restoreOrigin: RestoreOrigin
) )
/** /**
@ -2567,3 +2573,8 @@ abstract class CoreReaderFragment :
*/ */
abstract fun restoreViewStateOnInvalidJSON() abstract fun restoreViewStateOnInvalidJSON()
} }
enum class RestoreOrigin {
FromSearchScreen,
FromExternalLaunch
}

View File

@ -20,10 +20,12 @@ package org.kiwix.kiwixmobile.core.search.viewmodel.effects
import android.os.Parcelable import android.os.Parcelable
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResultOnCurrent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResultOnCurrent
import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.main.SEARCH_ITEM_TITLE_KEY
import org.kiwix.kiwixmobile.core.reader.addContentPrefix import org.kiwix.kiwixmobile.core.reader.addContentPrefix
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
@ -34,7 +36,10 @@ data class OpenSearchItem(
) : SideEffect<Unit> { ) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId
activity.navigate(readerFragmentResId) activity.navigate(
readerFragmentResId,
bundleOf(SEARCH_ITEM_TITLE_KEY to SEARCH_ITEM_TITLE_KEY)
)
activity.setNavigationResultOnCurrent( activity.setNavigationResultOnCurrent(
SearchItemToOpen( SearchItemToOpen(
searchListItem.value, searchListItem.value,

View File

@ -40,6 +40,7 @@ import org.kiwix.kiwixmobile.core.extensions.getResizedDrawable
import org.kiwix.kiwixmobile.core.extensions.isFileExist import org.kiwix.kiwixmobile.core.extensions.isFileExist
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
import org.kiwix.kiwixmobile.core.main.MainMenu import org.kiwix.kiwixmobile.core.main.MainMenu
import org.kiwix.kiwixmobile.core.main.RestoreOrigin
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource import org.kiwix.kiwixmobile.core.reader.ZimReaderSource
import org.kiwix.kiwixmobile.core.utils.LanguageUtils import org.kiwix.kiwixmobile.core.utils.LanguageUtils
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
@ -165,7 +166,9 @@ class CustomReaderFragment : CoreReaderFragment() {
override fun restoreViewStateOnValidJSON( override fun restoreViewStateOnValidJSON(
zimArticles: String?, zimArticles: String?,
zimPositions: String?, zimPositions: String?,
currentTab: Int currentTab: Int,
// Unused in custom apps as there is only one ZIM file that is already set.
restoreOrigin: RestoreOrigin
) { ) {
restoreTabs(zimArticles, zimPositions, currentTab) restoreTabs(zimArticles, zimPositions, currentTab)
} }