Binding WebViewActivity + LoginPreference

when does this end
This commit is contained in:
Jays2Kings 2021-03-29 23:13:12 -04:00
parent ef737dd40b
commit d4addb2e13
4 changed files with 57 additions and 56 deletions

View File

@ -14,10 +14,10 @@ import eu.kanade.tachiyomi.databinding.SourceGlobalSearchControllerBinding
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.manga.MangaDetailsController import eu.kanade.tachiyomi.ui.manga.MangaDetailsController
import eu.kanade.tachiyomi.util.view.activityBinding
import eu.kanade.tachiyomi.util.view.scrollViewWith import eu.kanade.tachiyomi.util.view.scrollViewWith
import eu.kanade.tachiyomi.util.view.updatePaddingRelative import eu.kanade.tachiyomi.util.view.updatePaddingRelative
import eu.kanade.tachiyomi.util.view.withFadeTransaction import eu.kanade.tachiyomi.util.view.withFadeTransaction
import kotlinx.android.synthetic.main.main_activity.*
import kotlinx.android.synthetic.main.source_global_search_controller.* import kotlinx.android.synthetic.main.source_global_search_controller.*
/** /**
@ -133,7 +133,7 @@ open class GlobalSearchController(
adapter = GlobalSearchAdapter(this) adapter = GlobalSearchAdapter(this)
recycler.updatePaddingRelative( recycler.updatePaddingRelative(
top = (activity?.toolbar?.height ?: 0) + top = (activityBinding?.toolbar?.height ?: 0) +
(activity?.window?.decorView?.rootWindowInsets?.systemWindowInsetTop ?: 0) (activity?.window?.decorView?.rootWindowInsets?.systemWindowInsetTop ?: 0)
) )

View File

@ -1,4 +1,4 @@
package eu.kanade.tachiyomi.ui.webview package eu.kanade.tachiyomi.ui.binding.webview
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
@ -25,7 +25,6 @@ import eu.kanade.tachiyomi.util.view.setStyle
import eu.kanade.tachiyomi.util.view.updateLayoutParams import eu.kanade.tachiyomi.util.view.updateLayoutParams
import eu.kanade.tachiyomi.util.view.updatePadding import eu.kanade.tachiyomi.util.view.updatePadding
import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.util.view.visible
import kotlinx.android.synthetic.main.webview_activity.*
open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() { open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
@ -33,17 +32,18 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = WebviewActivityBinding.inflate(layoutInflater)
delegate.localNightMode = ThemeUtil.nightMode(preferences.theme()) delegate.localNightMode = ThemeUtil.nightMode(preferences.theme())
setContentView(R.layout.webview_activity) setContentView(binding.root)
setSupportActionBar(toolbar) setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener { binding.toolbar.setNavigationOnClickListener {
super.onBackPressed() super.onBackPressed()
} }
toolbar.navigationIcon?.setTint(getResourceColor(R.attr.actionBarTintColor)) binding.toolbar.navigationIcon?.setTint(getResourceColor(R.attr.actionBarTintColor))
val container: ViewGroup = findViewById(R.id.web_view_layout) val container: ViewGroup = findViewById(R.id.web_view_layout)
val content: LinearLayout = findViewById(R.id.web_linear_layout) val content: LinearLayout = binding.webLinearLayout
container.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION container.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
content.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION content.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
@ -66,8 +66,8 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
insets.systemWindowInsetBottom insets.systemWindowInsetBottom
) )
} }
swipe_refresh.setStyle() binding.swipeRefresh.setStyle()
swipe_refresh.setOnRefreshListener { binding.swipeRefresh.setOnRefreshListener {
refreshPage() refreshPage()
} }
@ -110,23 +110,23 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
insets insets
} }
swipe_refresh.isEnabled = false binding.swipeRefresh.isEnabled = false
if (bundle == null) { if (bundle == null) {
webview.setDefaultSettings() binding.webview.setDefaultSettings()
webview.webChromeClient = object : WebChromeClient() { binding.webview.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView?, newProgress: Int) { override fun onProgressChanged(view: WebView?, newProgress: Int) {
progressBar.visible() binding.progressBar.visible()
progressBar.progress = newProgress binding.progressBar.progress = newProgress
if (newProgress == 100) { if (newProgress == 100) {
progressBar.invisible() binding.progressBar.invisible()
} }
super.onProgressChanged(view, newProgress) super.onProgressChanged(view, newProgress)
} }
} }
val marginB = webview.marginBottom val marginB = binding.webview.marginBottom
webview.setOnApplyWindowInsetsListener { v, insets -> binding.webview.setOnApplyWindowInsetsListener { v, insets ->
val bottomInset = insets.systemWindowInsetBottom val bottomInset = insets.systemWindowInsetBottom
v.updateLayoutParams<ViewGroup.MarginLayoutParams> { v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = marginB + bottomInset bottomMargin = marginB + bottomInset
@ -134,13 +134,13 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
insets insets
} }
} else { } else {
webview.restoreState(bundle) binding.webview.restoreState(bundle)
} }
} }
private fun refreshPage() { private fun refreshPage() {
swipe_refresh.isRefreshing = true binding.swipeRefresh.isRefreshing = true
webview.reload() binding.webview.reload()
} }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
@ -153,14 +153,14 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
), ),
255 255
) )
toolbar.setBackgroundColor(getResourceColor(R.attr.colorSecondary)) binding.toolbar.setBackgroundColor(getResourceColor(R.attr.colorSecondary))
toolbar.popupTheme = if (lightMode) R.style.ThemeOverlay_MaterialComponents else R binding.toolbar.popupTheme = if (lightMode) R.style.ThemeOverlay_MaterialComponents else R
.style.ThemeOverlay_MaterialComponents_Dark .style.ThemeOverlay_MaterialComponents_Dark
val tintColor = getResourceColor(R.attr.actionBarTintColor) val tintColor = getResourceColor(R.attr.actionBarTintColor)
toolbar.navigationIcon?.setTint(tintColor) binding.toolbar.navigationIcon?.setTint(tintColor)
toolbar.overflowIcon?.mutate() binding.toolbar.overflowIcon?.mutate()
toolbar.setTitleTextColor(tintColor) binding.toolbar.setTitleTextColor(tintColor)
toolbar.overflowIcon?.setTint(tintColor) binding.toolbar.overflowIcon?.setTint(tintColor)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
window.navigationBarColor = getResourceColor(R.attr.colorPrimaryVariant) window.navigationBarColor = getResourceColor(R.attr.colorPrimaryVariant)
@ -168,11 +168,11 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
window.navigationBarColor = getResourceColor(android.R.attr.colorBackground) window.navigationBarColor = getResourceColor(android.R.attr.colorBackground)
} }
web_linear_layout.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or binding.webLinearLayout.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && lightMode) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && lightMode) {
web_linear_layout.systemUiVisibility = web_linear_layout.systemUiVisibility.or( binding.webLinearLayout.systemUiVisibility = binding.webLinearLayout.systemUiVisibility.or(
View View
.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR .SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
) )
@ -189,7 +189,7 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
} }
} }
override fun onBackPressed() { override fun onBackPressed() {
if (webview.canGoBack()) webview.goBack() if (binding.webview.canGoBack()) binding.webview.goBack()
else super.onBackPressed() else super.onBackPressed()
} }
} }

View File

@ -17,11 +17,11 @@ import androidx.core.graphics.ColorUtils
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
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.binding.webview.BaseWebViewActivity
import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.WebViewClientCompat
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.toast import eu.kanade.tachiyomi.util.system.toast
import kotlinx.android.synthetic.main.webview_activity.*
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
open class WebViewActivity : BaseWebViewActivity() { open class WebViewActivity : BaseWebViewActivity() {
@ -52,14 +52,14 @@ open class WebViewActivity : BaseWebViewActivity() {
container.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION container.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
content.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION content.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
swipe_refresh.isEnabled = false binding.swipeRefresh.isEnabled = false
if (bundle == null) { if (bundle == null) {
val source = sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource ?: return val source = sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource ?: return
val url = intent.extras!!.getString(URL_KEY) ?: return val url = intent.extras!!.getString(URL_KEY) ?: return
val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }
webview.webViewClient = object : WebViewClientCompat() { binding.webview.webViewClient = object : WebViewClientCompat() {
override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean {
view.loadUrl(url) view.loadUrl(url)
return true return true
@ -69,10 +69,10 @@ open class WebViewActivity : BaseWebViewActivity() {
super.onPageFinished(view, url) super.onPageFinished(view, url)
invalidateOptionsMenu() invalidateOptionsMenu()
title = view?.title title = view?.title
swipe_refresh.isEnabled = true binding.swipeRefresh.isEnabled = true
swipe_refresh?.isRefreshing = false binding.swipeRefresh?.isRefreshing = false
val thing = view?.evaluateJavascript("getComputedStyle(document.querySelector('body')).backgroundColor") { view?.evaluateJavascript("getComputedStyle(document.querySelector('body')).backgroundColor") {
nested_view.setBackgroundColor(parseHTMLColor(it)) binding.nestedView.setBackgroundColor(parseHTMLColor(it))
} }
} }
@ -83,12 +83,12 @@ open class WebViewActivity : BaseWebViewActivity() {
override fun onPageCommitVisible(view: WebView?, url: String?) { override fun onPageCommitVisible(view: WebView?, url: String?) {
super.onPageCommitVisible(view, url) super.onPageCommitVisible(view, url)
nested_view.scrollTo(0, 0) binding.nestedView.scrollTo(0, 0)
} }
} }
webview.settings.userAgentString = source.headers["User-Agent"] binding.webview.settings.userAgentString = source.headers["User-Agent"]
webview.loadUrl(url, headers) binding.webview.loadUrl(url, headers)
} }
} }
@ -116,22 +116,22 @@ open class WebViewActivity : BaseWebViewActivity() {
} }
override fun onPrepareOptionsMenu(menu: Menu?): Boolean { override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val backItem = toolbar.menu.findItem(R.id.action_web_back) val backItem = binding.toolbar.menu.findItem(R.id.action_web_back)
val forwardItem = toolbar.menu.findItem(R.id.action_web_forward) val forwardItem = binding.toolbar.menu.findItem(R.id.action_web_forward)
backItem?.isEnabled = webview.canGoBack() backItem?.isEnabled = binding.webview.canGoBack()
forwardItem?.isEnabled = webview.canGoForward() forwardItem?.isEnabled = binding.webview.canGoForward()
val hasHistory = webview.canGoBack() || webview.canGoForward() val hasHistory = binding.webview.canGoBack() || binding.webview.canGoForward()
backItem?.isVisible = hasHistory backItem?.isVisible = hasHistory
forwardItem?.isVisible = hasHistory forwardItem?.isVisible = hasHistory
val tintColor = getResourceColor(R.attr.actionBarTintColor) val tintColor = getResourceColor(R.attr.actionBarTintColor)
val translucentWhite = ColorUtils.setAlphaComponent(tintColor, 127) val translucentWhite = ColorUtils.setAlphaComponent(tintColor, 127)
backItem.icon?.setTint(if (webview.canGoBack()) tintColor else translucentWhite) backItem.icon?.setTint(if (binding.webview.canGoBack()) tintColor else translucentWhite)
forwardItem?.icon?.setTint(if (webview.canGoForward()) tintColor else translucentWhite) forwardItem?.icon?.setTint(if (binding.webview.canGoForward()) tintColor else translucentWhite)
return super.onPrepareOptionsMenu(menu) return super.onPrepareOptionsMenu(menu)
} }
override fun onBackPressed() { override fun onBackPressed() {
if (webview.canGoBack()) webview.goBack() if (binding.webview.canGoBack()) binding.webview.goBack()
else super.onBackPressed() else super.onBackPressed()
} }
@ -141,8 +141,8 @@ open class WebViewActivity : BaseWebViewActivity() {
*/ */
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.action_web_back -> webview.goBack() R.id.action_web_back -> binding.webview.goBack()
R.id.action_web_forward -> webview.goForward() R.id.action_web_forward -> binding.webview.goForward()
R.id.action_web_share -> shareWebpage() R.id.action_web_share -> shareWebpage()
R.id.action_web_browser -> openInBrowser() R.id.action_web_browser -> openInBrowser()
} }
@ -153,7 +153,7 @@ open class WebViewActivity : BaseWebViewActivity() {
try { try {
val intent = Intent(Intent.ACTION_SEND).apply { val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain" type = "text/plain"
putExtra(Intent.EXTRA_TEXT, webview.url) putExtra(Intent.EXTRA_TEXT, binding.webview.url)
} }
startActivity(Intent.createChooser(intent, getString(R.string.share))) startActivity(Intent.createChooser(intent, getString(R.string.share)))
} catch (e: Exception) { } catch (e: Exception) {
@ -162,6 +162,6 @@ open class WebViewActivity : BaseWebViewActivity() {
} }
private fun openInBrowser() { private fun openInBrowser() {
openInBrowser(webview.url) openInBrowser(binding.webview.url)
} }
} }

View File

@ -7,7 +7,7 @@ import android.util.AttributeSet
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import kotlinx.android.synthetic.main.pref_widget_imageview.view.* import eu.kanade.tachiyomi.databinding.PrefWidgetImageviewBinding
class LoginPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : class LoginPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
Preference(context, attrs) { Preference(context, attrs) {
@ -18,12 +18,13 @@ class LoginPreference @JvmOverloads constructor(context: Context, attrs: Attribu
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
val binding = PrefWidgetImageviewBinding.bind(holder.itemView)
holder.itemView.image_view.setImageResource( binding.imageView.setImageResource(
if (getPersistedString("").isNullOrEmpty()) android.R.color.transparent if (getPersistedString("").isNullOrEmpty()) android.R.color.transparent
else R.drawable.ic_done_24dp else R.drawable.ic_done_24dp
) )
holder.itemView.image_view.imageTintList = binding.imageView.imageTintList =
ColorStateList.valueOf(Color.parseColor("#FF4CAF50")) ColorStateList.valueOf(Color.parseColor("#FF4CAF50"))
} }