From aa934459bb6da960405a1a4f9ec67e391b2e898d Mon Sep 17 00:00:00 2001 From: MohitMali Date: Mon, 30 Oct 2023 14:17:11 +0530 Subject: [PATCH] Moved `openSearchItem`, `findInPage` functionality to our `CoreReaderFragment` as we were using same code twice for these methods in app and custom module. --- .../destination/reader/KiwixReaderFragment.kt | 29 ------------------- .../core/main/CoreReaderFragment.kt | 28 +++++++++++++++++- .../custom/main/CustomReaderFragment.kt | 27 ----------------- 3 files changed, 27 insertions(+), 57 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 a0cb43c7e2..d762dba235 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 @@ -34,7 +34,6 @@ import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.core.net.toFile import androidx.drawerlayout.widget.DrawerLayout -import androidx.lifecycle.Observer import com.google.android.material.bottomnavigation.BottomNavigationView import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.cachedComponent @@ -42,8 +41,6 @@ import org.kiwix.kiwixmobile.core.R.anim import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.consumeObservable -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.observeNavigationResult import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle import org.kiwix.kiwixmobile.core.extensions.coreMainActivity import org.kiwix.kiwixmobile.core.extensions.isFileExist @@ -54,16 +51,11 @@ 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.FIND_IN_PAGE_SEARCH_STRING import org.kiwix.kiwixmobile.core.main.ToolbarScrollingKiwixWebView -import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.TAG_CURRENT_FILE -import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED import org.kiwix.kiwixmobile.core.utils.TAG_KIWIX import org.kiwix.kiwixmobile.core.utils.files.FileUtils -import org.kiwix.kiwixmobile.core.utils.titleToUrl -import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl import java.io.File private const val HIDE_TAB_SWITCHER_DELAY: Long = 300 @@ -88,27 +80,6 @@ class KiwixReaderFragment : CoreReaderFragment() { toolbar?.let(activity::setupDrawerToggle) setFragmentContainerBottomMarginToSizeOfNavBar() openPageInBookFromNavigationArguments() - - requireActivity().observeNavigationResult( - FIND_IN_PAGE_SEARCH_STRING, - viewLifecycleOwner, - Observer(::findInPage) - ) - requireActivity().observeNavigationResult( - TAG_FILE_SEARCHED, - viewLifecycleOwner, - Observer(::openSearchItem) - ) - } - - private fun openSearchItem(item: SearchItemToOpen) { - zimReaderContainer?.titleToUrl(item.pageTitle)?.let { - if (item.shouldOpenInNewTab) { - createNewTab() - } - loadUrlWithCurrentWebview(zimReaderContainer?.urlSuffixToParsableUrl(it)) - } - requireActivity().consumeObservable(TAG_FILE_SEARCHED) } private fun openPageInBookFromNavigationArguments() { 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 bb4d7774ee..0d4f73656c 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 @@ -77,6 +77,7 @@ import androidx.core.view.WindowInsetsControllerCompat import androidx.core.widget.ContentLoadingProgressBar import androidx.drawerlayout.widget.DrawerLayout import androidx.lifecycle.Lifecycle +import androidx.lifecycle.Observer import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -107,7 +108,9 @@ import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.downloader.fetch.DOWNLOAD_NOTIFICATION_TITLE +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.consumeObservable import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.hasNotificationPermission +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.observeNavigationResult import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.requestNotificationPermission import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.findFirstTextView import org.kiwix.kiwixmobile.core.extensions.isFileExist @@ -131,6 +134,7 @@ import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_P import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_STOP_TTS import org.kiwix.kiwixmobile.core.reader.ZimFileReader import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer +import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen import org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate import org.kiwix.kiwixmobile.core.utils.DimenUtils.getToolbarHeight import org.kiwix.kiwixmobile.core.utils.ExternalLinkOpener @@ -153,6 +157,8 @@ import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import org.kiwix.kiwixmobile.core.utils.files.FileUtils.deleteCachedFiles import org.kiwix.kiwixmobile.core.utils.files.FileUtils.readFile +import org.kiwix.kiwixmobile.core.utils.titleToUrl +import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl import java.io.File import java.io.IOException import java.text.SimpleDateFormat @@ -486,6 +492,16 @@ abstract class CoreReaderFragment : readAloudService?.registerCallBack(this@CoreReaderFragment) } } + requireActivity().observeNavigationResult( + FIND_IN_PAGE_SEARCH_STRING, + viewLifecycleOwner, + Observer(::findInPage) + ) + requireActivity().observeNavigationResult( + TAG_FILE_SEARCHED, + viewLifecycleOwner, + Observer(::openSearchItem) + ) } private fun initTabCallback() { @@ -1621,6 +1637,16 @@ abstract class CoreReaderFragment : openSearch("", isOpenedFromTabView = false, isVoice) } + private fun openSearchItem(item: SearchItemToOpen) { + zimReaderContainer?.titleToUrl(item.pageTitle)?.let { + if (item.shouldOpenInNewTab) { + createNewTab() + } + loadUrlWithCurrentWebview(zimReaderContainer?.urlSuffixToParsableUrl(it)) + } + requireActivity().consumeObservable(TAG_FILE_SEARCHED) + } + private fun handleIntentActions(intent: Intent) { Log.d(TAG_KIWIX, "action" + requireActivity().intent?.action) startIntentBasedOnAction(intent) @@ -1760,7 +1786,7 @@ abstract class CoreReaderFragment : } } - protected fun findInPage(title: String?) { + private fun findInPage(title: String?) { // if the search is localized trigger find in page UI. compatCallback?.apply { setActive() 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 2a469b0c36..d2f338b53a 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 @@ -26,20 +26,13 @@ import android.view.View import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.drawerlayout.widget.DrawerLayout -import androidx.lifecycle.Observer import androidx.navigation.fragment.findNavController import org.kiwix.kiwixmobile.core.base.BaseActivity -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.observeNavigationResult import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle import org.kiwix.kiwixmobile.core.main.CoreReaderFragment -import org.kiwix.kiwixmobile.core.main.FIND_IN_PAGE_SEARCH_STRING import org.kiwix.kiwixmobile.core.main.MainMenu -import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen import org.kiwix.kiwixmobile.core.utils.LanguageUtils -import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower -import org.kiwix.kiwixmobile.core.utils.titleToUrl -import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl import org.kiwix.kiwixmobile.custom.BuildConfig import org.kiwix.kiwixmobile.custom.R import org.kiwix.kiwixmobile.custom.customActivityComponent @@ -77,26 +70,6 @@ class CustomReaderFragment : CoreReaderFragment() { toolbar?.let { setupDrawerToggle(it) } } loadPageFromNavigationArguments() - - requireActivity().observeNavigationResult( - FIND_IN_PAGE_SEARCH_STRING, - viewLifecycleOwner, - Observer(::findInPage) - ) - requireActivity().observeNavigationResult( - TAG_FILE_SEARCHED, - viewLifecycleOwner, - Observer(::openSearchItem) - ) - } - } - - private fun openSearchItem(item: SearchItemToOpen) { - zimReaderContainer?.titleToUrl(item.pageTitle)?.apply { - if (item.shouldOpenInNewTab) { - createNewTab() - } - loadUrlWithCurrentWebview(zimReaderContainer?.urlSuffixToParsableUrl(this)) } }