mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-18 20:05:53 -04:00
Fixed the back button issue for service worker ZIM files.
This commit is contained in:
parent
ba42a98ad3
commit
4cb32b1eff
@ -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_PAUSE_OR_RESUME_TTS
|
||||||
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_STOP_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
|
||||||
|
import org.kiwix.kiwixmobile.core.reader.ZimFileReader.Companion.CONTENT_PREFIX
|
||||||
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
||||||
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
|
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
|
||||||
import org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate
|
import org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate
|
||||||
@ -944,7 +945,7 @@ abstract class CoreReaderFragment :
|
|||||||
drawerLayout?.openDrawer(GravityCompat.END)
|
drawerLayout?.openDrawer(GravityCompat.END)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("ReturnCount")
|
@Suppress("ReturnCount", "NestedBlockDepth")
|
||||||
override fun onBackPressed(activity: AppCompatActivity): FragmentActivityExtensions.Super {
|
override fun onBackPressed(activity: AppCompatActivity): FragmentActivityExtensions.Super {
|
||||||
when {
|
when {
|
||||||
tabSwitcherRoot?.visibility == View.VISIBLE -> {
|
tabSwitcherRoot?.visibility == View.VISIBLE -> {
|
||||||
@ -973,6 +974,30 @@ abstract class CoreReaderFragment :
|
|||||||
}
|
}
|
||||||
|
|
||||||
getCurrentWebView()?.canGoBack() == true -> {
|
getCurrentWebView()?.canGoBack() == true -> {
|
||||||
|
getCurrentWebView()?.apply {
|
||||||
|
val webViewBackWordHistoryList = mutableListOf<String>()
|
||||||
|
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()
|
getCurrentWebView()?.goBack()
|
||||||
return FragmentActivityExtensions.Super.ShouldNotCall
|
return FragmentActivityExtensions.Super.ShouldNotCall
|
||||||
}
|
}
|
||||||
@ -981,6 +1006,14 @@ abstract class CoreReaderFragment :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isHomePageOfServiceWorkerZimFiles(
|
||||||
|
currentUrl: String?,
|
||||||
|
backwardHistoryList: List<String>
|
||||||
|
): Boolean =
|
||||||
|
currentUrl != null &&
|
||||||
|
backwardHistoryList[1] == currentUrl &&
|
||||||
|
backwardHistoryList[0] == "$CONTENT_PREFIX${zimReaderContainer?.mainPage}"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the title for toolbar, controlling the title of toolbar.
|
* Sets the title for toolbar, controlling the title of toolbar.
|
||||||
* Subclasses like CustomReaderFragment override this method to provide custom
|
* Subclasses like CustomReaderFragment override this method to provide custom
|
||||||
@ -1908,6 +1941,7 @@ abstract class CoreReaderFragment :
|
|||||||
|
|
||||||
@OnClick(R2.id.bottom_toolbar_home)
|
@OnClick(R2.id.bottom_toolbar_home)
|
||||||
fun openMainPage() {
|
fun openMainPage() {
|
||||||
|
Log.e("ZIMFILEREADER1", "openMainPage ")
|
||||||
val articleUrl = zimReaderContainer?.mainPage
|
val articleUrl = zimReaderContainer?.mainPage
|
||||||
openArticle(articleUrl)
|
openArticle(articleUrl)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user