From 4cb32b1eff7e3386a1bb893db2340a391fbcebae Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Tue, 28 May 2024 16:22:16 +0530 Subject: [PATCH] Fixed the back button issue for service worker ZIM files. --- .../core/main/CoreReaderFragment.kt | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) 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 6bfcd8528..71449cf58 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 @@ -134,6 +134,7 @@ import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_PAUSE_OR_RESUME_TTS 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.ZimFileReader.Companion.CONTENT_PREFIX import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen import org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate @@ -944,7 +945,7 @@ abstract class CoreReaderFragment : drawerLayout?.openDrawer(GravityCompat.END) } - @Suppress("ReturnCount") + @Suppress("ReturnCount", "NestedBlockDepth") override fun onBackPressed(activity: AppCompatActivity): FragmentActivityExtensions.Super { when { tabSwitcherRoot?.visibility == View.VISIBLE -> { @@ -973,6 +974,30 @@ abstract class CoreReaderFragment : } getCurrentWebView()?.canGoBack() == true -> { + getCurrentWebView()?.apply { + val webViewBackWordHistoryList = mutableListOf() + try { + // Get the webView's backward history + copyBackForwardList().let { webBackForwardList -> + (webBackForwardList.currentIndex downTo 0) + .map(webBackForwardList::getItemAtIndex) + .mapTo(webViewBackWordHistoryList) { it.url } + .reverse() + } + } catch (ignore: Exception) { + // Catch any exception thrown by the WebView since + // `copyBackForwardList` can throw an error. + } + // Check if the WebView has two items in backward history. + // Since here we need to handle the back button. + if (webViewBackWordHistoryList.size == 2 && + isHomePageOfServiceWorkerZimFiles(url, webViewBackWordHistoryList) + ) { + // If it is the last page that is showing to the user, then exit the application. + return@onBackPressed FragmentActivityExtensions.Super.ShouldCall + } + } + // Otherwise, go to the previous page. getCurrentWebView()?.goBack() return FragmentActivityExtensions.Super.ShouldNotCall } @@ -981,6 +1006,14 @@ abstract class CoreReaderFragment : } } + private fun isHomePageOfServiceWorkerZimFiles( + currentUrl: String?, + backwardHistoryList: List + ): Boolean = + currentUrl != null && + backwardHistoryList[1] == currentUrl && + backwardHistoryList[0] == "$CONTENT_PREFIX${zimReaderContainer?.mainPage}" + /** * Sets the title for toolbar, controlling the title of toolbar. * Subclasses like CustomReaderFragment override this method to provide custom @@ -1908,6 +1941,7 @@ abstract class CoreReaderFragment : @OnClick(R2.id.bottom_toolbar_home) fun openMainPage() { + Log.e("ZIMFILEREADER1", "openMainPage ") val articleUrl = zimReaderContainer?.mainPage openArticle(articleUrl) }