From 8ffd3a8ed225e13d68b8f90dd2d5156f25eade8b Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 10 Dec 2020 09:15:09 -0500 Subject: [PATCH] Don't scroll to top when navigating back/forward in WebView --- .../setting/track/MyAnimeListLoginActivity.kt | 9 ++++----- .../ui/webview/BaseWebViewActivity.kt | 9 +++++++++ .../tachiyomi/ui/webview/WebViewActivity.kt | 19 ++++++++++--------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/track/MyAnimeListLoginActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/track/MyAnimeListLoginActivity.kt index 3f17502f4c..4a217bd82b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/track/MyAnimeListLoginActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/track/MyAnimeListLoginActivity.kt @@ -21,8 +21,6 @@ class MyAnimeListLoginActivity : BaseWebViewActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - title = getString(R.string.login) - if (bundle == null) { binding.webview.webViewClient = object : WebViewClientCompat() { override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { @@ -68,9 +66,10 @@ class MyAnimeListLoginActivity : BaseWebViewActivity() { companion object { fun newIntent(context: Context): Intent { - val intent = Intent(context, MyAnimeListLoginActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - return intent + return Intent(context, MyAnimeListLoginActivity::class.java).apply { + addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + putExtra(TITLE_KEY, context.getString(R.string.login)) + } } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt index b0f80e2c33..b19c068582 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt @@ -23,6 +23,8 @@ open class BaseWebViewActivity : BaseActivity() { internal var bundle: Bundle? = null + internal var isRefreshing: Boolean = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -40,6 +42,8 @@ open class BaseWebViewActivity : BaseActivity() { finish() } + title = intent.extras?.getString(TITLE_KEY) + setSupportActionBar(binding.toolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) binding.toolbar.navigationClicks() @@ -87,5 +91,10 @@ open class BaseWebViewActivity : BaseActivity() { fun refreshPage() { binding.swipeRefresh.isRefreshing = true binding.webview.reload() + isRefreshing = true + } + + companion object { + const val TITLE_KEY = "title_key" } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt index ce2becb968..a9e8b40a8b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -25,8 +25,6 @@ class WebViewActivity : BaseWebViewActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - title = intent.extras?.getString(TITLE_KEY) - if (bundle == null) { val url = intent.extras!!.getString(URL_KEY) ?: return @@ -60,7 +58,10 @@ class WebViewActivity : BaseWebViewActivity() { binding.swipeRefresh.isRefreshing = false // Reset to top when page refreshes - view?.scrollTo(0, 0) + if (isRefreshing) { + view?.scrollTo(0, 0) + isRefreshing = false + } } } @@ -120,12 +121,12 @@ class WebViewActivity : BaseWebViewActivity() { private const val TITLE_KEY = "title_key" fun newIntent(context: Context, url: String, sourceId: Long? = null, title: String? = null): Intent { - val intent = Intent(context, WebViewActivity::class.java) - intent.putExtra(URL_KEY, url) - intent.putExtra(SOURCE_KEY, sourceId) - intent.putExtra(TITLE_KEY, title) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - return intent + return Intent(context, WebViewActivity::class.java).apply { + addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + putExtra(URL_KEY, url) + putExtra(SOURCE_KEY, sourceId) + putExtra(TITLE_KEY, title) + } } } }