Skip to content

Commit

Permalink
Fixed: Opening a searched item and "Find in Page" was not working aft…
Browse files Browse the repository at this point in the history
…er rebasing.

* Removed unnecessary code from the project to simplify it and make it more robust.
  • Loading branch information
MohitMaliDeveloper committed Feb 7, 2025
1 parent 3f80e9d commit 052624f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,11 @@ class KiwixReaderFragment : CoreReaderFragment() {
* @param webViewHistoryItemList WebViewHistoryItem list representing the list of articles to be restored.
* @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.
* @param onComplete Callback to be invoked upon completion of the restoration process.
*/
override fun restoreViewStateOnValidWebViewHistory(
webViewHistoryItemList: List<WebViewHistoryItem>,
currentTab: Int,
restoreOrigin: RestoreOrigin,
onComplete: () -> Unit
restoreOrigin: RestoreOrigin
) {
when (restoreOrigin) {
FromExternalLaunch -> {
Expand All @@ -284,7 +282,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
} else {
zimReaderContainer?.zimFileReader?.let(::setUpBookmarks)
}
restoreTabs(webViewHistoryItemList, currentTab, onComplete)
restoreTabs(webViewHistoryItemList, currentTab)
} else {
getCurrentWebView()?.snack(string.zim_not_opened)
exitBook() // hide the options for zim file to avoid unexpected UI behavior
Expand All @@ -293,7 +291,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
}

FromSearchScreen -> {
restoreTabs(webViewHistoryItemList, currentTab, onComplete)
restoreTabs(webViewHistoryItemList, currentTab)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ abstract class CoreReaderFragment :
private var isFirstTimeMainPageLoaded = true
private var isFromManageExternalLaunch = false
private val savingTabsMutex = Mutex()
private var searchItemToOpen: SearchItemToOpen? = null
private var findInPageTitle: String? = null

@JvmField
@Inject
Expand Down Expand Up @@ -2136,18 +2134,6 @@ abstract class CoreReaderFragment :
openSearch("", isOpenedFromTabView = false, isVoice)
}

/**
* Stores the specified search item to be opened later.
*
* This method saves the provided `SearchItemToOpen` object, which will be used to
* open the searched item after the tabs have been restored.
*
* @param item The search item to be opened after restoring the tabs.
*/
private fun storeSearchItem(item: SearchItemToOpen) {
searchItemToOpen = item
}

/**
* Opens a search item based on its properties.
*
Expand Down Expand Up @@ -2355,16 +2341,6 @@ abstract class CoreReaderFragment :
}
}

/**
* Stores the given title for a "find in page" search operation.
* This title is used later when triggering the "find in page" functionality.
*
* @param title The title or keyword to search for within the current WebView content.
*/
private fun storeFindInPageTitle(title: String) {
findInPageTitle = title
}

/**
* Initiates the "find in page" UI for searching within the current WebView content.
* If the `compatCallback` is active, it sets up the WebView to search for the
Expand Down Expand Up @@ -2736,21 +2712,7 @@ abstract class CoreReaderFragment :
restoreViewStateOnInvalidWebViewHistory()
return@subscribe
}
restoreViewStateOnValidWebViewHistory(
webViewHistoryItemList,
currentTab,
restoreOrigin
) {
// This lambda is executed after the tabs have been restored. It checks if there is a
// search item to open. If `searchItemToOpen` is not null, it calls `openSearchItem`
// to open the specified item, then sets `searchItemToOpen` to null to prevent
// any unexpected behavior on future calls. Similarly, if `findInPageTitle` is set,
// it invokes `findInPage` and resets `findInPageTitle` to null.
searchItemToOpen?.let(::openSearchItem)
searchItemToOpen = null
findInPageTitle?.let(::findInPage)
findInPageTitle = null
}
restoreViewStateOnValidWebViewHistory(webViewHistoryItemList, currentTab, restoreOrigin)
}, {
Log.e(
TAG_KIWIX,
Expand All @@ -2772,22 +2734,19 @@ abstract class CoreReaderFragment :
* - Iterates over the provided webViewHistoryItemList, creating new tabs and restoring
* their states based on the historical data.
* - Selects the specified tab to make it the currently active one.
* - Invokes the onComplete callback once the restoration is finished.
*
* If any error occurs during the restoration process, it logs a warning and displays
* a toast message to inform the user that the tabs could not be restored.
*
* @param webViewHistoryItemList List of WebViewHistoryItem representing the historical data for restoring tabs.
* @param currentTab Index of the tab to be set as the currently active tab after restoration.
* @param onComplete Callback to be invoked upon successful restoration of the tabs.
*
* @Warning: This method restores tabs state in new launches, do not modify it
* unless it is explicitly mentioned in the issue you're fixing.
*/
protected fun restoreTabs(
webViewHistoryItemList: List<WebViewHistoryItem>,
currentTab: Int,
onComplete: () -> Unit
currentTab: Int
) {
try {
currentWebViewIndex = 0
Expand All @@ -2802,7 +2761,6 @@ abstract class CoreReaderFragment :
}
}
selectTab(currentTab)
onComplete.invoke()
} catch (ignore: Exception) {
Log.w(TAG_KIWIX, "Kiwix shared preferences corrupted", ignore)
activity.toast(R.string.could_not_restore_tabs, Toast.LENGTH_LONG)
Expand All @@ -2822,12 +2780,12 @@ abstract class CoreReaderFragment :
requireActivity().observeNavigationResult<String>(
FIND_IN_PAGE_SEARCH_STRING,
viewLifecycleOwner,
Observer(::storeFindInPageTitle)
Observer(::findInPage)
)
requireActivity().observeNavigationResult<SearchItemToOpen>(
TAG_FILE_SEARCHED,
viewLifecycleOwner,
Observer(::storeSearchItem)
Observer(::openSearchItem)
)
}

Expand Down Expand Up @@ -2934,8 +2892,7 @@ abstract class CoreReaderFragment :
protected abstract fun restoreViewStateOnValidWebViewHistory(
webViewHistoryItemList: List<WebViewHistoryItem>,
currentTab: Int,
restoreOrigin: RestoreOrigin,
onComplete: () -> Unit
restoreOrigin: RestoreOrigin
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ class CustomReaderFragment : CoreReaderFragment() {
webViewHistoryItemList: List<WebViewHistoryItem>,
currentTab: Int,
// Unused in custom apps as there is only one ZIM file that is already set.
restoreOrigin: RestoreOrigin,
onComplete: () -> Unit
restoreOrigin: RestoreOrigin
) {
restoreTabs(webViewHistoryItemList, currentTab, onComplete)
restoreTabs(webViewHistoryItemList, currentTab)
}

/**
Expand Down

0 comments on commit 052624f

Please sign in to comment.