Skip to content

Commit

Permalink
refactor(reading): reenable webview renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashinch committed Jan 26, 2024
1 parent 89c64cc commit c611d42
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ sealed class ReadingSubheadAlignPreference(val value: Int) : Preference() {
Justify -> TextAlign.Justify
}

fun toTextAlignCSS(): String =
when (this) {
Left -> "left"
Right -> "right"
Center -> "center"
Justify -> "justify"
}

companion object {

val default = Left
Expand Down
48 changes: 20 additions & 28 deletions app/src/main/java/me/ash/reader/ui/component/base/RYWebView.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.ash.reader.ui.component.base

import android.content.Context
import android.net.http.SslError
import android.util.Log
import android.webkit.SslErrorHandler
Expand All @@ -17,10 +18,13 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import me.ash.reader.infrastructure.preference.LocalOpenLink
import me.ash.reader.infrastructure.preference.LocalOpenLinkSpecificBrowser
import me.ash.reader.infrastructure.preference.LocalReadingPageTonalElevation
import me.ash.reader.ui.ext.openURL
import me.ash.reader.ui.ext.surfaceColorAtElevation

const val INJECTION_TOKEN = "/android_asset_font/"

Expand All @@ -33,8 +37,9 @@ fun RYWebView(
val context = LocalContext.current
val openLink = LocalOpenLink.current
val openLinkSpecificBrowser = LocalOpenLinkSpecificBrowser.current
val tonalElevation = LocalReadingPageTonalElevation.current
val color = MaterialTheme.colorScheme.onSurfaceVariant.toArgb()
val backgroundColor = MaterialTheme.colorScheme.surface.toArgb()
val backgroundColor = MaterialTheme.colorScheme.surfaceColorAtElevation(tonalElevation.value.dp).toArgb()
val webViewClient by remember {
mutableStateOf(object : WebViewClient() {

Expand Down Expand Up @@ -107,33 +112,20 @@ fun RYWebView(
this.webViewClient = webViewClient
setBackgroundColor(backgroundColor)
isHorizontalScrollBarEnabled = false
isVerticalScrollBarEnabled = false
isVerticalScrollBarEnabled = true
})
}

// Column(
// modifier = modifier
// .height(if (viewState.isLoading) 100.dp else 0.dp),
// ) {
// Icon(
// modifier = modifier
// .size(50.dp),
// imageVector = Icons.Rounded.HourglassBottom,
// contentDescription = "Loading",
// tint = MaterialTheme.colorScheme.primary,
// )
// Spacer(modifier = modifier.height(50.dp))
// }

AndroidView(
modifier = modifier,//.padding(horizontal = if (content.contains("class=\"page\"")) 0.dp else 24.dp),
modifier = modifier,
factory = { webView },
update = {
it.apply {
Log.i("RLog", "CustomWebView: ${content}")
settings.javaScriptEnabled = true
loadDataWithBaseURL(
null,
getStyle(color) + content,
getStyle(context, color) + content,
"text/HTML",
"UTF-8", null
)
Expand All @@ -145,27 +137,21 @@ fun RYWebView(
@Stable
fun argbToCssColor(argb: Int): String = String.format("#%06X", 0xFFFFFF and argb)

// TODO: Google sans is deprecated
@Stable
fun getStyle(argb: Int): String = """
fun getStyle(context: Context, argb: Int): String = """
<html><head><style>
*{
padding: 0;
margin: 0;
color: ${argbToCssColor(argb)};
// font-family: url('/android_asset_font/font/google_sans_text_regular.TTF'),
// url('/android_asset_font/font/google_sans_text_medium_italic.TTF'),
// url('/android_asset_font/font/google_sans_text_medium.TTF'),
// url('/android_asset_font/font/google_sans_text_italic.TTF'),
// url('/android_asset_font/font/google_sans_text_bold_italic.TTF'),
// url('/android_asset_font/font/google_sans_text_bold.TTF');
font-family: url('/android_asset_font/${context.filesDir.absolutePath}/reading_font.ttf'),
}
html {
padding: 0 24px;
}
img, video, iframe {
figure > img, video, iframe {
margin: 0 -24px 20px;
width: calc(100% + 48px);
border-top: 1px solid ${argbToCssColor(argb)}08;
Expand Down Expand Up @@ -194,6 +180,11 @@ blockquote {
color: ${argbToCssColor(argb)}cc;
}
blockquote img, blockquote video, blockquote iframe {
margin: unset;
max-width: 100%;
}
pre {
max-width: 100%;
background: ${argbToCssColor(argb)}11;
Expand All @@ -218,6 +209,7 @@ h1,h2,h3,h4,h5,h6,figure,br {
margin-bottom: 20px;
}
.element::-webkit-scrollbar { width: 0 !important }
/*.element::-webkit-scrollbar { width: 0 !important }*/
</style></head></html>
"""
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ fun Content(
if (!isLoading) {
when (renderer) {
ReadingRendererPreference.WebView ->
item { RYWebView(content = content) }
item {
DisableSelection {
RYWebView(content = content)
}
}

ReadingRendererPreference.NativeComponent ->
Reader(
Expand Down

0 comments on commit c611d42

Please sign in to comment.