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.CoreReaderFragment
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.reader.ZimReaderSource
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource.Companion.fromDatabaseValue
@ -104,7 +107,9 @@ class KiwixReaderFragment : CoreReaderFragment() {
if (args.zimFileUri.isNotEmpty()) {
tryOpeningZimFile(args.zimFileUri)
} else {
manageExternalLaunchAndRestoringViewState()
val restoreOrigin =
if (args.searchItemTitle.isNotEmpty()) FromSearchScreen else FromExternalLaunch
manageExternalLaunchAndRestoringViewState(restoreOrigin)
}
}
requireArguments().clear()
@ -215,31 +220,53 @@ class KiwixReaderFragment : CoreReaderFragment() {
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(
zimArticles: String?,
zimPositions: String?,
currentTab: Int
currentTab: Int,
restoreOrigin: RestoreOrigin
) {
lifecycleScope.launch {
val settings =
requireActivity().getSharedPreferences(SharedPreferenceUtil.PREF_KIWIX_MOBILE, 0)
val zimReaderSource = fromDatabaseValue(settings.getString(TAG_CURRENT_FILE, null))
if (zimReaderSource != null && zimReaderSource.canOpenInLibkiwix()) {
if (zimReaderContainer?.zimReaderSource == null) {
openZimFile(zimReaderSource)
Log.d(
TAG_KIWIX,
"Kiwix normal start, Opened last used zimFile: -> ${zimReaderSource.toDatabase()}"
)
} else {
zimReaderContainer?.zimFileReader?.let(::setUpBookmarks)
when (restoreOrigin) {
FromExternalLaunch -> {
lifecycleScope.launch {
val settings =
requireActivity().getSharedPreferences(SharedPreferenceUtil.PREF_KIWIX_MOBILE, 0)
val zimReaderSource = fromDatabaseValue(settings.getString(TAG_CURRENT_FILE, null))
if (zimReaderSource?.canOpenInLibkiwix() == true) {
if (zimReaderContainer?.zimReaderSource == null) {
openZimFile(zimReaderSource)
Log.d(
TAG_KIWIX,
"Kiwix normal start, Opened last used zimFile: -> ${zimReaderSource.toDatabase()}"
)
} 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 kotlin.math.abs
import kotlin.math.max
import org.kiwix.kiwixmobile.core.main.RestoreOrigin.FromExternalLaunch
const val SEARCH_ITEM_TITLE_KEY = "searchItemTitle"
@Suppress("LargeClass")
abstract class CoreReaderFragment :
@ -2427,7 +2430,9 @@ abstract class CoreReaderFragment :
private fun isInvalidJson(jsonString: String?): Boolean =
jsonString == null || jsonString == "[]"
protected fun manageExternalLaunchAndRestoringViewState() {
protected fun manageExternalLaunchAndRestoringViewState(
restoreOrigin: RestoreOrigin = FromExternalLaunch
) {
val settings = requireActivity().getSharedPreferences(
SharedPreferenceUtil.PREF_KIWIX_MOBILE,
0
@ -2438,7 +2443,7 @@ abstract class CoreReaderFragment :
if (isInvalidJson(zimArticles) || isInvalidJson(zimPositions)) {
restoreViewStateOnInvalidJSON()
} else {
restoreViewStateOnValidJSON(zimArticles, zimPositions, currentTab)
restoreViewStateOnValidJSON(zimArticles, zimPositions, currentTab, restoreOrigin)
}
}
@ -2554,7 +2559,8 @@ abstract class CoreReaderFragment :
protected abstract fun restoreViewStateOnValidJSON(
zimArticles: String?,
zimPositions: String?,
currentTab: Int
currentTab: Int,
restoreOrigin: RestoreOrigin
)
/**
@ -2567,3 +2573,8 @@ abstract class CoreReaderFragment :
*/
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 androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import kotlinx.parcelize.Parcelize
import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setNavigationResultOnCurrent
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.search.adapter.SearchListItem
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
@ -34,7 +36,10 @@ data class OpenSearchItem(
) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) {
val readerFragmentResId = (activity as CoreMainActivity).readerFragmentResId
activity.navigate(readerFragmentResId)
activity.navigate(
readerFragmentResId,
bundleOf(SEARCH_ITEM_TITLE_KEY to SEARCH_ITEM_TITLE_KEY)
)
activity.setNavigationResultOnCurrent(
SearchItemToOpen(
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.main.CoreReaderFragment
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.utils.LanguageUtils
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
@ -165,7 +166,9 @@ class CustomReaderFragment : CoreReaderFragment() {
override fun restoreViewStateOnValidJSON(
zimArticles: 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)
}