From c386d375dee2a846dd59395e6eda3a07311ed3d5 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 12 Oct 2023 22:56:19 -0400 Subject: [PATCH] Tweak Cloudflare help message in WebView screen Catches pages like what Shinigami is currently showing. Also adjusts the banner to make it look more like part of the top AppBar so it looks less like part of the webpage. --- .../settings/screen/SettingsMainScreen.kt | 2 +- .../webview/WebViewScreenContent.kt | 169 ++++++++++-------- 2 files changed, 91 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsMainScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsMainScreen.kt index 86cb2790da..5f9c4e9b54 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsMainScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsMainScreen.kt @@ -78,7 +78,7 @@ object SettingsMainScreen : Screen() { val backPress = LocalBackPress.currentOrThrow val containerColor = if (twoPane) getPalerSurface() else MaterialTheme.colorScheme.surface val topBarState = rememberTopAppBarState() - + Scaffold( topBarScrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(topBarState), topBar = { scrollBehavior -> diff --git a/app/src/main/java/eu/kanade/presentation/webview/WebViewScreenContent.kt b/app/src/main/java/eu/kanade/presentation/webview/WebViewScreenContent.kt index 5f88a4fe35..04133bc49e 100644 --- a/app/src/main/java/eu/kanade/presentation/webview/WebViewScreenContent.kt +++ b/app/src/main/java/eu/kanade/presentation/webview/WebViewScreenContent.kt @@ -7,6 +7,7 @@ import android.webkit.WebView import androidx.compose.foundation.clickable 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.padding import androidx.compose.material.icons.Icons @@ -14,6 +15,8 @@ import androidx.compose.material.icons.outlined.ArrowBack import androidx.compose.material.icons.outlined.ArrowForward import androidx.compose.material.icons.outlined.Close import androidx.compose.material3.LinearProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -22,8 +25,10 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.LocalUriHandler import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp import com.google.accompanist.web.AccompanistWebViewClient import com.google.accompanist.web.LoadingState import com.google.accompanist.web.WebView @@ -72,7 +77,7 @@ fun WebViewScreenContent( super.onPageFinished(view, url) scope.launch { val html = view.getHtml() - showCloudflareHelp = "window._cf_chl_opt" in html + showCloudflareHelp = "window._cf_chl_opt" in html || "Ray ID is" in html } } @@ -103,54 +108,71 @@ fun WebViewScreenContent( Scaffold( topBar = { Box { - AppBar( - title = state.pageTitle ?: initialTitle, - subtitle = currentUrl, - navigateUp = onNavigateUp, - navigationIcon = Icons.Outlined.Close, - actions = { - AppBarActions( - listOf( - AppBar.Action( - title = stringResource(R.string.action_webview_back), - icon = Icons.Outlined.ArrowBack, - onClick = { - if (navigator.canGoBack) { - navigator.navigateBack() - } + Column { + AppBar( + title = state.pageTitle ?: initialTitle, + subtitle = currentUrl, + navigateUp = onNavigateUp, + navigationIcon = Icons.Outlined.Close, + actions = { + AppBarActions( + listOf( + AppBar.Action( + title = stringResource(R.string.action_webview_back), + icon = Icons.Outlined.ArrowBack, + onClick = { + if (navigator.canGoBack) { + navigator.navigateBack() + } + }, + enabled = navigator.canGoBack, + ), + AppBar.Action( + title = stringResource(R.string.action_webview_forward), + icon = Icons.Outlined.ArrowForward, + onClick = { + if (navigator.canGoForward) { + navigator.navigateForward() + } + }, + enabled = navigator.canGoForward, + ), + AppBar.OverflowAction( + title = stringResource(R.string.action_webview_refresh), + onClick = { navigator.reload() }, + ), + AppBar.OverflowAction( + title = stringResource(R.string.action_share), + onClick = { onShare(currentUrl) }, + ), + AppBar.OverflowAction( + title = stringResource(R.string.action_open_in_browser), + onClick = { onOpenInBrowser(currentUrl) }, + ), + AppBar.OverflowAction( + title = stringResource(R.string.pref_clear_cookies), + onClick = { onClearCookies(currentUrl) }, + ), + ), + ) + }, + ) + + if (showCloudflareHelp) { + Surface( + modifier = Modifier.padding(8.dp), + ) { + WarningBanner( + textRes = R.string.information_cloudflare_help, + modifier = Modifier + .clip(MaterialTheme.shapes.small) + .clickable { + uriHandler.openUri("https://tachiyomi.org/docs/guides/troubleshooting/#cloudflare") }, - enabled = navigator.canGoBack, - ), - AppBar.Action( - title = stringResource(R.string.action_webview_forward), - icon = Icons.Outlined.ArrowForward, - onClick = { - if (navigator.canGoForward) { - navigator.navigateForward() - } - }, - enabled = navigator.canGoForward, - ), - AppBar.OverflowAction( - title = stringResource(R.string.action_webview_refresh), - onClick = { navigator.reload() }, - ), - AppBar.OverflowAction( - title = stringResource(R.string.action_share), - onClick = { onShare(currentUrl) }, - ), - AppBar.OverflowAction( - title = stringResource(R.string.action_open_in_browser), - onClick = { onOpenInBrowser(currentUrl) }, - ), - AppBar.OverflowAction( - title = stringResource(R.string.pref_clear_cookies), - onClick = { onClearCookies(currentUrl) }, - ), - ), - ) - }, - ) + ) + } + } + } when (val loadingState = state.loadingState) { is LoadingState.Initializing -> LinearProgressIndicator( modifier = Modifier @@ -168,38 +190,27 @@ fun WebViewScreenContent( } }, ) { contentPadding -> - Column( - modifier = Modifier.padding(contentPadding), - ) { - if (showCloudflareHelp) { - WarningBanner( - textRes = R.string.information_cloudflare_help, - modifier = Modifier.clickable { - uriHandler.openUri("https://tachiyomi.org/docs/guides/troubleshooting/#cloudflare") - }, - ) - } + WebView( + state = state, + modifier = Modifier + .fillMaxSize() + .padding(contentPadding), + navigator = navigator, + onCreated = { webView -> + webView.setDefaultSettings() - WebView( - state = state, - modifier = Modifier.weight(1f), - 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) + } - // 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, - ) - } + headers["user-agent"]?.let { + webView.settings.userAgentString = it + } + }, + client = webClient, + ) } }