From 83337ac5f1552a24e57c8e79c99000c7e6de5076 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 25 Oct 2024 17:32:08 +0530 Subject: [PATCH] Fixed: FindInPage functionality not working in Kiwix and custom apps. --- .../core/main/CoreReaderFragment.kt | 31 ++++++++++++++++--- .../custom/main/CustomReaderFragment.kt | 3 -- 2 files changed, 26 insertions(+), 8 deletions(-) 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 dfd3ea9a3..407705e7f 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 @@ -288,6 +288,7 @@ abstract class CoreReaderFragment : private var isFromManageExternalLaunch = false private var shouldSaveTabsOnPause = true private var searchItemToOpen: SearchItemToOpen? = null + private var findInPageTitle: String? = null @JvmField @Inject @@ -2358,6 +2359,23 @@ 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 + * specified title and displays the search input UI. + * + * @param title The search term or keyword to locate within the page. If null, no action is taken. + */ private fun findInPage(title: String?) { // if the search is localized trigger find in page UI. compatCallback?.apply { @@ -2734,12 +2752,15 @@ abstract class CoreReaderFragment : currentTab, restoreOrigin ) { - // This lambda function is invoked after restoring the tabs. It checks if there is a - // search item to open. If `searchItemToOpen` is not null, it will call the openSearchItem - // method to open the specified search item. After opening, it sets `searchItemToOpen` - // to null to prevent any unexpected behavior on subsequent calls. + // 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 } }, { restoreViewStateOnInvalidWebViewHistory() @@ -2808,7 +2829,7 @@ abstract class CoreReaderFragment : requireActivity().observeNavigationResult( FIND_IN_PAGE_SEARCH_STRING, viewLifecycleOwner, - Observer(::findInPage) + Observer(::storeFindInPageTitle) ) requireActivity().observeNavigationResult( TAG_FILE_SEARCHED, 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 42852ba08..9e3602498 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 @@ -20,7 +20,6 @@ package org.kiwix.kiwixmobile.custom.main import android.app.Dialog import android.os.Bundle -import android.util.Log import android.view.Menu import android.view.MenuInflater import android.view.View @@ -140,13 +139,11 @@ class CustomReaderFragment : CoreReaderFragment() { private fun loadPageFromNavigationArguments() { val args = CustomReaderFragmentArgs.fromBundle(requireArguments()) if (args.pageUrl.isNotEmpty()) { - Log.e("OPEN_PAGE", "loadPageFromNavigationArguments: ${args.pageUrl}") loadUrlWithCurrentWebview(args.pageUrl) // Setup bookmark for current book // See https://github.com/kiwix/kiwix-android/issues/3541 zimReaderContainer?.zimFileReader?.let(::setUpBookmarks) } else { - Log.e("OPEN_PAGE", "loadPageFromNavigationArguments: else part") openObbOrZim(true) } requireArguments().clear()