Skip to content

Commit

Permalink
refactor(webview): Replace WebView with its Compose counterpart
Browse files Browse the repository at this point in the history
Co-authored-by: null2264 <[email protected]>
  • Loading branch information
arkon and null2264 committed Nov 26, 2024
1 parent a199ff3 commit 5e84586
Show file tree
Hide file tree
Showing 15 changed files with 674 additions and 315 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ dependencies {
implementation(compose.bundles.compose)
debugImplementation(compose.ui.tooling)
implementation(libs.compose.theme.adapter3)
implementation(libs.accompanist.webview)
implementation(compose.webview)

implementation(libs.flexbox)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,175 +1,44 @@
package eu.kanade.tachiyomi.ui.webview

import android.annotation.SuppressLint
import android.app.assist.AssistContent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.LinearLayout
import androidx.core.graphics.ColorUtils
import androidx.core.graphics.Insets
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsCompat.Type.systemBars
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.core.view.marginBottom
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.changesIn
import eu.kanade.tachiyomi.databinding.WebviewActivityBinding
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate
import eu.kanade.tachiyomi.util.system.getPrefTheme
import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.isInNightMode
import eu.kanade.tachiyomi.util.system.setDefaultSettings
import eu.kanade.tachiyomi.util.view.setStyle
import android.R as AR

open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {

private var bundle: Bundle? = null
// FIXME: Not sure if some of these stuff still needed
open class BaseWebViewActivity : BaseActivity<ViewBinding>() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = WebviewActivityBinding.inflate(layoutInflater)
delegate.localNightMode = preferences.nightMode().get()
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
binding.toolbar.setNavigationOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
val tintColor = getResourceColor(R.attr.actionBarTintColor)
binding.toolbar.navigationIcon?.setTint(tintColor)
binding.toolbar.navigationIcon?.setTint(tintColor)
binding.toolbar.overflowIcon?.mutate()
binding.toolbar.overflowIcon?.setTint(tintColor)

val container: ViewGroup = findViewById(R.id.web_view_layout)
val content: LinearLayout = binding.webLinearLayout
WindowCompat.setDecorFitsSystemWindows(window, false)

ViewCompat.setOnApplyWindowInsetsListener(container) { v, insets ->
val contextView = window?.decorView?.findViewById<View>(R.id.action_mode_bar)
contextView?.updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = insets.getInsets(systemBars()).left
rightMargin = insets.getInsets(systemBars()).right
}
// Consume any horizontal insets and pad all content in. There's not much we can do
// with horizontal insets
v.updatePadding(
left = insets.getInsets(systemBars()).left,
right = insets.getInsets(systemBars()).right,
)
WindowInsetsCompat.Builder(insets).setInsets(
systemBars(),
Insets.of(
0,
insets.getInsets(systemBars()).top,
0,
insets.getInsets(systemBars()).bottom,
),
).build()
}
binding.swipeRefresh.setStyle()
binding.swipeRefresh.setOnRefreshListener {
refreshPage()
}

window.statusBarColor = ColorUtils.setAlphaComponent(
getResourceColor(R.attr.colorSurface),
255,
)

ViewCompat.setOnApplyWindowInsetsListener(content) { v, insets ->
// if pure white theme on a device that does not support dark status bar
/*if (getResourceColor(AR.attr.statusBarColor) != Color.TRANSPARENT)
window.statusBarColor = Color.BLACK
else window.statusBarColor = getResourceColor(R.attr.colorPrimary)*/
window.navigationBarColor = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Color.BLACK
} else {
getResourceColor(R.attr.colorPrimaryVariant)
}
v.setPadding(
insets.getInsets(systemBars()).left,
insets.getInsets(systemBars()).top,
insets.getInsets(systemBars()).right,
0,
)
if (!isInNightMode()) {
WindowInsetsControllerCompat(window, content).isAppearanceLightNavigationBars = true
}
insets
}

binding.swipeRefresh.isEnabled = false

if (bundle == null) {
binding.webview.setDefaultSettings()

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
invalidateOptionsMenu()
}
super.onProgressChanged(view, newProgress)
}

override fun onReceivedTitle(view: WebView?, title: String?) {
super.onReceivedTitle(view, title)
this@BaseWebViewActivity.title = title
binding.toolbarTitle.text = title
binding.toolbarSubtitle.text = view?.url
binding.toolbarSubtitle.isSelected = true
}
}
val marginB = binding.webview.marginBottom
ViewCompat.setOnApplyWindowInsetsListener(binding.swipeRefresh) { v, insets ->
val bottomInset = insets.getInsets(systemBars()).bottom
v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = marginB + bottomInset
}
insets
}
} else {
bundle?.let {
binding.webview.restoreState(it)
}
}

preferences.incognitoMode()
.changesIn(lifecycleScope) {
SecureActivityDelegate.setSecure(this)
}
}

override fun onProvideAssistContent(outContent: AssistContent?) {
super.onProvideAssistContent(outContent)
binding.webview.url?.let { outContent?.webUri = it.toUri() }
}

private fun refreshPage() {
binding.swipeRefresh.isRefreshing = true
binding.webview.reload()
}

@SuppressLint("ResourceType")
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
Expand All @@ -189,30 +58,14 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
val attrs = theme.obtainStyledAttributes(
intArrayOf(
R.attr.colorSurface,
R.attr.actionBarTintColor,
R.attr.colorPrimaryVariant,
),
)
val colorSurface = attrs.getColor(0, 0)
val actionBarTintColor = attrs.getColor(1, 0)
val colorPrimaryVariant = attrs.getColor(2, 0)
val colorPrimaryVariant = attrs.getColor(1, 0)
attrs.recycle()

window.statusBarColor = ColorUtils.setAlphaComponent(colorSurface, 255)
binding.toolbar.setBackgroundColor(colorSurface)
binding.toolbar.popupTheme =
if (lightMode) {
R.style.ThemeOverlay_Material3
} else {
R.style.ThemeOverlay_Material3_Dark
}
binding.toolbar.setNavigationIconTint(actionBarTintColor)
binding.toolbar.overflowIcon?.mutate()
binding.toolbar.setTitleTextColor(actionBarTintColor)
binding.toolbar.overflowIcon?.setTint(actionBarTintColor)
binding.swipeRefresh.setColorSchemeColors(actionBarTintColor)
binding.swipeRefresh.setProgressBackgroundColorSchemeColor(colorPrimaryVariant)

window.navigationBarColor =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O || !lightMode) {
colorPrimaryVariant
Expand Down
Loading

0 comments on commit 5e84586

Please sign in to comment.