Remove unnecessary BaseWebViewActivity class
This commit is contained in:
parent
eaccfdde59
commit
62ab70f889
2 changed files with 85 additions and 103 deletions
|
@ -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<WebviewActivityBinding>() {
|
|
||||||
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,29 +2,97 @@ package eu.kanade.tachiyomi.ui.webview
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.ApplicationInfo
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
|
import android.webkit.WebChromeClient
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.core.graphics.ColorUtils
|
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.R
|
||||||
|
import eu.kanade.tachiyomi.databinding.WebviewActivityBinding
|
||||||
import eu.kanade.tachiyomi.source.SourceManager
|
import eu.kanade.tachiyomi.source.SourceManager
|
||||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
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.WebViewClientCompat
|
||||||
import eu.kanade.tachiyomi.util.system.WebViewUtil
|
import eu.kanade.tachiyomi.util.system.WebViewUtil
|
||||||
import eu.kanade.tachiyomi.util.system.getResourceColor
|
import eu.kanade.tachiyomi.util.system.getResourceColor
|
||||||
import eu.kanade.tachiyomi.util.system.openInBrowser
|
import eu.kanade.tachiyomi.util.system.openInBrowser
|
||||||
|
import eu.kanade.tachiyomi.util.system.setDefaultSettings
|
||||||
import eu.kanade.tachiyomi.util.system.toast
|
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
|
import uy.kohesive.injekt.injectLazy
|
||||||
|
|
||||||
class WebViewActivity : BaseWebViewActivity() {
|
class WebViewActivity : BaseViewBindingActivity<WebviewActivityBinding>() {
|
||||||
|
|
||||||
private val sourceManager by injectLazy<SourceManager>()
|
private val sourceManager by injectLazy<SourceManager>()
|
||||||
|
|
||||||
|
private var bundle: Bundle? = null
|
||||||
|
|
||||||
|
private var isRefreshing: Boolean = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
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) {
|
if (bundle == null) {
|
||||||
val url = intent.extras!!.getString(URL_KEY) ?: return
|
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 {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
menuInflater.inflate(R.menu.webview, menu)
|
menuInflater.inflate(R.menu.webview, menu)
|
||||||
return true
|
return true
|
||||||
|
@ -99,6 +172,17 @@ class WebViewActivity : BaseWebViewActivity() {
|
||||||
return super.onOptionsItemSelected(item)
|
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() {
|
private fun shareWebpage() {
|
||||||
try {
|
try {
|
||||||
val intent = Intent(Intent.ACTION_SEND).apply {
|
val intent = Intent(Intent.ACTION_SEND).apply {
|
||||||
|
|
Reference in a new issue