From 052624fd57065524313f362e24de5712057577f0 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 7 Feb 2025 18:16:11 +0530 Subject: [PATCH] Fixed: Opening a searched item and "Find in Page" was not working after rebasing. * Removed unnecessary code from the project to simplify it and make it more robust. --- .../destination/reader/KiwixReaderFragment.kt | 8 ++- .../core/main/CoreReaderFragment.kt | 53 ++----------------- .../custom/main/CustomReaderFragment.kt | 5 +- 3 files changed, 10 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt index 6a6d0f8118..d0536d9ead 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt @@ -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, currentTab: Int, - restoreOrigin: RestoreOrigin, - onComplete: () -> Unit + restoreOrigin: RestoreOrigin ) { when (restoreOrigin) { FromExternalLaunch -> { @@ -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 @@ -293,7 +291,7 @@ class KiwixReaderFragment : CoreReaderFragment() { } FromSearchScreen -> { - restoreTabs(webViewHistoryItemList, currentTab, onComplete) + restoreTabs(webViewHistoryItemList, currentTab) } } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt index 7fb73b6f94..56bf81620a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt @@ -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 @@ -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. * @@ -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 @@ -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, @@ -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, - currentTab: Int, - onComplete: () -> Unit + currentTab: Int ) { try { currentWebViewIndex = 0 @@ -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) @@ -2822,12 +2780,12 @@ abstract class CoreReaderFragment : requireActivity().observeNavigationResult( FIND_IN_PAGE_SEARCH_STRING, viewLifecycleOwner, - Observer(::storeFindInPageTitle) + Observer(::findInPage) ) requireActivity().observeNavigationResult( TAG_FILE_SEARCHED, viewLifecycleOwner, - Observer(::storeSearchItem) + Observer(::openSearchItem) ) } @@ -2934,8 +2892,7 @@ abstract class CoreReaderFragment : protected abstract fun restoreViewStateOnValidWebViewHistory( webViewHistoryItemList: List, currentTab: Int, - restoreOrigin: RestoreOrigin, - onComplete: () -> Unit + restoreOrigin: RestoreOrigin ) /** diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt index 9e3602498a..fe7160d904 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt @@ -166,10 +166,9 @@ class CustomReaderFragment : CoreReaderFragment() { webViewHistoryItemList: List, 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) } /**