WebViewScreen: Use Scaffold (#7958)

This commit is contained in:
Ivan Iskandar 2022-09-08 03:59:44 +07:00 committed by GitHub
parent 26d8e47bb9
commit 032b377de7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,9 +4,8 @@ import android.content.pm.ApplicationInfo
import android.webkit.WebResourceRequest import android.webkit.WebResourceRequest
import android.webkit.WebView import android.webkit.WebView
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward import androidx.compose.material.icons.filled.ArrowForward
@ -14,10 +13,9 @@ import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.zIndex
import com.google.accompanist.web.AccompanistWebViewClient import com.google.accompanist.web.AccompanistWebViewClient
import com.google.accompanist.web.LoadingState import com.google.accompanist.web.LoadingState
import com.google.accompanist.web.WebView import com.google.accompanist.web.WebView
@ -25,6 +23,7 @@ import com.google.accompanist.web.rememberWebViewNavigator
import com.google.accompanist.web.rememberWebViewState import com.google.accompanist.web.rememberWebViewState
import eu.kanade.presentation.components.AppBar import eu.kanade.presentation.components.AppBar
import eu.kanade.presentation.components.AppBarActions import eu.kanade.presentation.components.AppBarActions
import eu.kanade.presentation.components.Scaffold
import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.system.setDefaultSettings
@ -39,103 +38,110 @@ fun WebViewScreen(
onOpenInBrowser: (String) -> Unit, onOpenInBrowser: (String) -> Unit,
onClearCookies: (String) -> Unit, onClearCookies: (String) -> Unit,
) { ) {
val context = LocalContext.current
val state = rememberWebViewState(url = url, additionalHttpHeaders = headers) val state = rememberWebViewState(url = url, additionalHttpHeaders = headers)
val navigator = rememberWebViewNavigator() val navigator = rememberWebViewNavigator()
Column { Scaffold(
AppBar( topBar = {
title = state.pageTitle ?: initialTitle, Box {
subtitle = state.content.getCurrentUrl(), AppBar(
navigateUp = onUp, title = state.pageTitle ?: initialTitle,
navigationIcon = Icons.Default.Close, subtitle = state.content.getCurrentUrl(),
actions = { navigateUp = onUp,
AppBarActions( navigationIcon = Icons.Default.Close,
listOf( actions = {
AppBar.Action( AppBarActions(
title = stringResource(R.string.action_webview_back), listOf(
icon = Icons.Default.ArrowBack, AppBar.Action(
onClick = { title = stringResource(R.string.action_webview_back),
if (navigator.canGoBack) { icon = Icons.Default.ArrowBack,
navigator.navigateBack() onClick = {
} if (navigator.canGoBack) {
}, navigator.navigateBack()
enabled = navigator.canGoBack, }
), },
AppBar.Action( enabled = navigator.canGoBack,
title = stringResource(R.string.action_webview_forward), ),
icon = Icons.Default.ArrowForward, AppBar.Action(
onClick = { title = stringResource(R.string.action_webview_forward),
if (navigator.canGoForward) { icon = Icons.Default.ArrowForward,
navigator.navigateForward() onClick = {
} if (navigator.canGoForward) {
}, navigator.navigateForward()
enabled = navigator.canGoForward, }
), },
AppBar.OverflowAction( enabled = navigator.canGoForward,
title = stringResource(R.string.action_webview_refresh), ),
onClick = { navigator.reload() }, AppBar.OverflowAction(
), title = stringResource(R.string.action_webview_refresh),
AppBar.OverflowAction( onClick = { navigator.reload() },
title = stringResource(R.string.action_share), ),
onClick = { onShare(state.content.getCurrentUrl()!!) }, AppBar.OverflowAction(
), title = stringResource(R.string.action_share),
AppBar.OverflowAction( onClick = { onShare(state.content.getCurrentUrl()!!) },
title = stringResource(R.string.action_open_in_browser), ),
onClick = { onOpenInBrowser(state.content.getCurrentUrl()!!) }, AppBar.OverflowAction(
), title = stringResource(R.string.action_open_in_browser),
AppBar.OverflowAction( onClick = { onOpenInBrowser(state.content.getCurrentUrl()!!) },
title = stringResource(R.string.pref_clear_cookies), ),
onClick = { onClearCookies(state.content.getCurrentUrl()!!) }, AppBar.OverflowAction(
), title = stringResource(R.string.pref_clear_cookies),
), onClick = { onClearCookies(state.content.getCurrentUrl()!!) },
),
),
)
},
) )
}, when (val loadingState = state.loadingState) {
) is LoadingState.Initializing -> LinearProgressIndicator(
modifier = Modifier
Box { .fillMaxWidth()
val loadingState = state.loadingState .align(Alignment.BottomCenter),
if (loadingState is LoadingState.Loading) { )
LinearProgressIndicator( is LoadingState.Loading -> LinearProgressIndicator(
progress = loadingState.progress, progress = (loadingState as? LoadingState.Loading)?.progress ?: 1f,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.zIndex(1f), .align(Alignment.BottomCenter),
) )
} else -> {}
val webClient = remember {
object : AccompanistWebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?,
): Boolean {
request?.let {
view?.loadUrl(it.url.toString(), headers)
}
return super.shouldOverrideUrlLoading(view, request)
}
} }
} }
},
WebView( ) { contentPadding ->
state = state, val webClient = remember {
modifier = Modifier.fillMaxSize(), object : AccompanistWebViewClient() {
navigator = navigator, override fun shouldOverrideUrlLoading(
onCreated = { webView -> view: WebView?,
webView.setDefaultSettings() request: WebResourceRequest?,
): Boolean {
// Debug mode (chrome://inspect/#devices) request?.let {
if (BuildConfig.DEBUG && 0 != context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) { view?.loadUrl(it.url.toString(), headers)
WebView.setWebContentsDebuggingEnabled(true)
} }
return super.shouldOverrideUrlLoading(view, request)
headers["user-agent"]?.let { }
webView.settings.userAgentString = it }
}
},
client = webClient,
)
} }
WebView(
state = state,
modifier = Modifier.padding(contentPadding),
navigator = navigator,
onCreated = { webView ->
webView.setDefaultSettings()
// Debug mode (chrome://inspect/#devices)
if (BuildConfig.DEBUG &&
0 != webView.context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
) {
WebView.setWebContentsDebuggingEnabled(true)
}
headers["user-agent"]?.let {
webView.settings.userAgentString = it
}
},
client = webClient,
)
} }
} }