From 62ab70f88996a117cf509a6ec11e2905f2b921fd Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 26 Dec 2020 11:44:21 -0500 Subject: [PATCH] Remove unnecessary BaseWebViewActivity class --- .../ui/webview/BaseWebViewActivity.kt | 102 ------------------ .../tachiyomi/ui/webview/WebViewActivity.kt | 86 ++++++++++++++- 2 files changed, 85 insertions(+), 103 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt 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 deleted file mode 100644 index 5068cdd1da..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt +++ /dev/null @@ -1,102 +0,0 @@ -package eu.kanade.tachiyomi.ui.webview - -import android.content.pm.ApplicationInfo -import android.os.Bundle -import android.webkit.WebChromeClient -import android.webkit.WebView -import android.widget.Toast -import androidx.core.view.isInvisible -import androidx.core.view.isVisible -import eu.kanade.tachiyomi.BuildConfig -import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.databinding.WebviewActivityBinding -import eu.kanade.tachiyomi.ui.base.activity.BaseViewBindingActivity -import eu.kanade.tachiyomi.util.system.WebViewUtil -import eu.kanade.tachiyomi.util.system.setDefaultSettings -import eu.kanade.tachiyomi.util.system.toast -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach -import reactivecircus.flowbinding.appcompat.navigationClicks -import reactivecircus.flowbinding.swiperefreshlayout.refreshes - -open class BaseWebViewActivity : BaseViewBindingActivity() { - - internal var bundle: Bundle? = null - - internal var isRefreshing: Boolean = false - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - if (!WebViewUtil.supportsWebView(this)) { - toast(R.string.information_webview_required, Toast.LENGTH_LONG) - finish() - return - } - - try { - binding = WebviewActivityBinding.inflate(layoutInflater) - setContentView(binding.root) - } catch (e: Throwable) { - // Potentially throws errors like "Error inflating class android.webkit.WebView" - toast(R.string.information_webview_required, Toast.LENGTH_LONG) - finish() - return - } - - title = intent.extras?.getString(TITLE_KEY) - - setSupportActionBar(binding.toolbar) - supportActionBar?.setDisplayHomeAsUpEnabled(true) - binding.toolbar.navigationClicks() - .onEach { super.onBackPressed() } - .launchIn(scope) - - binding.swipeRefresh.isEnabled = false - binding.swipeRefresh.refreshes() - .onEach { refreshPage() } - .launchIn(scope) - - if (bundle == null) { - binding.webview.setDefaultSettings() - - // Debug mode (chrome://inspect/#devices) - if (BuildConfig.DEBUG && 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) { - WebView.setWebContentsDebuggingEnabled(true) - } - - binding.webview.webChromeClient = object : WebChromeClient() { - override fun onProgressChanged(view: WebView?, newProgress: Int) { - binding.progressBar.isVisible = true - binding.progressBar.progress = newProgress - if (newProgress == 100) { - binding.progressBar.isInvisible = true - } - super.onProgressChanged(view, newProgress) - } - } - } else { - binding.webview.restoreState(bundle) - } - } - - override fun onDestroy() { - binding.webview?.destroy() - super.onDestroy() - } - - override fun onBackPressed() { - if (binding.webview.canGoBack()) binding.webview.goBack() - else super.onBackPressed() - } - - 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 a9e8b40a8b..ef3161fbaa 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 @@ -2,29 +2,97 @@ package eu.kanade.tachiyomi.ui.webview import android.content.Context import android.content.Intent +import android.content.pm.ApplicationInfo import android.graphics.Bitmap import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.webkit.WebChromeClient import android.webkit.WebView +import android.widget.Toast import androidx.core.graphics.ColorUtils +import androidx.core.view.isInvisible +import androidx.core.view.isVisible +import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.databinding.WebviewActivityBinding import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.online.HttpSource +import eu.kanade.tachiyomi.ui.base.activity.BaseViewBindingActivity import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.WebViewUtil import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.openInBrowser +import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.system.toast +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import reactivecircus.flowbinding.appcompat.navigationClicks +import reactivecircus.flowbinding.swiperefreshlayout.refreshes import uy.kohesive.injekt.injectLazy -class WebViewActivity : BaseWebViewActivity() { +class WebViewActivity : BaseViewBindingActivity() { private val sourceManager by injectLazy() + private var bundle: Bundle? = null + + private var isRefreshing: Boolean = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + if (!WebViewUtil.supportsWebView(this)) { + toast(R.string.information_webview_required, Toast.LENGTH_LONG) + finish() + return + } + + try { + binding = WebviewActivityBinding.inflate(layoutInflater) + setContentView(binding.root) + } catch (e: Throwable) { + // Potentially throws errors like "Error inflating class android.webkit.WebView" + toast(R.string.information_webview_required, Toast.LENGTH_LONG) + finish() + return + } + + title = intent.extras?.getString(TITLE_KEY) + + setSupportActionBar(binding.toolbar) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + binding.toolbar.navigationClicks() + .onEach { super.onBackPressed() } + .launchIn(scope) + + binding.swipeRefresh.isEnabled = false + binding.swipeRefresh.refreshes() + .onEach { refreshPage() } + .launchIn(scope) + + if (bundle == null) { + binding.webview.setDefaultSettings() + + // Debug mode (chrome://inspect/#devices) + if (BuildConfig.DEBUG && 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) { + WebView.setWebContentsDebuggingEnabled(true) + } + + binding.webview.webChromeClient = object : WebChromeClient() { + override fun onProgressChanged(view: WebView?, newProgress: Int) { + binding.progressBar.isVisible = true + binding.progressBar.progress = newProgress + if (newProgress == 100) { + binding.progressBar.isInvisible = true + } + super.onProgressChanged(view, newProgress) + } + } + } else { + binding.webview.restoreState(bundle) + } + if (bundle == null) { val url = intent.extras!!.getString(URL_KEY) ?: return @@ -69,6 +137,11 @@ class WebViewActivity : BaseWebViewActivity() { } } + override fun onDestroy() { + binding.webview?.destroy() + super.onDestroy() + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.webview, menu) return true @@ -99,6 +172,17 @@ class WebViewActivity : BaseWebViewActivity() { return super.onOptionsItemSelected(item) } + override fun onBackPressed() { + if (binding.webview.canGoBack()) binding.webview.goBack() + else super.onBackPressed() + } + + private fun refreshPage() { + binding.swipeRefresh.isRefreshing = true + binding.webview.reload() + isRefreshing = true + } + private fun shareWebpage() { try { val intent = Intent(Intent.ACTION_SEND).apply {