From d1b95d7b7f4f5e6dbd34f8b25267804d8b6581bd Mon Sep 17 00:00:00 2001 From: FlaminSarge Date: Sun, 11 Aug 2019 14:29:51 -0700 Subject: [PATCH] Allow back button to navigate to previous URL in WebView, add Forward, Refresh, and Close menu options --- .../ui/manga/info/MangaWebViewController.kt | 42 +++++++++++++++++-- app/src/main/res/menu/web_view.xml | 19 +++++++++ app/src/main/res/values/strings.xml | 2 + 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/menu/web_view.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt index 8d7d9687a0..2ae3667e81 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt @@ -1,9 +1,7 @@ package eu.kanade.tachiyomi.ui.manga.info import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import android.webkit.WebView import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.SourceManager @@ -16,6 +14,10 @@ class MangaWebViewController(bundle: Bundle? = null) : BaseController(bundle) { private val sourceManager by injectLazy() + init { + setHasOptionsMenu(true) + } + constructor(sourceId: Long, url: String) : this(Bundle().apply { putLong(SOURCE_KEY, sourceId) putString(URL_KEY, url) @@ -43,6 +45,40 @@ class MangaWebViewController(bundle: Bundle? = null) : BaseController(bundle) { web.loadUrl(url, headers) } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.web_view, menu) + } + + override fun onPrepareOptionsMenu(menu: Menu) { + val web = view as WebView + menu.findItem(R.id.action_forward).isVisible = web.canGoForward() + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.action_forward -> { + val web = view as WebView + if (web.canGoForward()) web.goForward() + } + R.id.action_refresh -> { + val web = view as WebView + web.reload() + } + R.id.action_close -> router.popController(this) + else -> return super.onOptionsItemSelected(item) + } + return true + } + + override fun handleBack(): Boolean { + val web = view as WebView + if (web.canGoBack()) { + web.goBack() + return true + } + return super.handleBack() + } + override fun onDestroyView(view: View) { val web = view as WebView web.stopLoading() diff --git a/app/src/main/res/menu/web_view.xml b/app/src/main/res/menu/web_view.xml new file mode 100644 index 0000000000..1e4056460b --- /dev/null +++ b/app/src/main/res/menu/web_view.xml @@ -0,0 +1,19 @@ + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e454cac60b..c7711a76ad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -93,6 +93,8 @@ Restore Open Log in + Forward + Refresh Deleting…