Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some visual tweaks #491

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions app/src/main/java/com/capyreader/app/ui/articles/ArticleLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -133,18 +132,10 @@ fun ArticleLayout(
scaffoldNavigator.navigateTo(ListDetailPaneScaffoldRole.Detail)
}

val scrollState = rememberSaveable(key = article?.id, saver = ScrollState.Saver) {
ScrollState(initial = 0)
}
val webViewState = rememberWebViewState(
onNavigateToMedia = {
mediaUrl = it
},
onPageStarted = {
coroutineScope.launch {
scrollState.scrollTo(0)
}
}
)

fun scrollToArticle(index: Int) {
Expand Down Expand Up @@ -373,7 +364,6 @@ fun ArticleLayout(
ArticleView(
article = article,
webViewState = webViewState,
scrollState = scrollState,
articles = indexedArticles,
onBackPressed = {
coroutineScope.launchUI {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import com.jocmp.capy.Article
@Composable
fun ArticleReader(
article: Article,
scrollState: ScrollState,
webViewState: WebViewState,
) {
val scrollState = rememberSaveable(saver = ScrollState.Saver) {
ScrollState(initial = 0)
}

var lastScrollY by rememberSaveable { mutableIntStateOf(0) }

ColumnScrollbar(state = scrollState) {
Expand All @@ -40,7 +43,7 @@ fun ArticleReader(
lastScrollY = scrollState.value
},
)
Spacer(modifier = Modifier.height(150.dp))
Spacer(modifier = Modifier.height(120.dp))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
Expand All @@ -26,6 +25,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
Expand All @@ -49,7 +49,6 @@ import org.koin.compose.koinInject
fun ArticleView(
article: Article,
webViewState: WebViewState,
scrollState: ScrollState,
articles: IndexedArticles,
onBackPressed: () -> Unit,
onToggleRead: () -> Unit,
Expand Down Expand Up @@ -93,18 +92,19 @@ fun ArticleView(
)
},
reader = {
ArticlePullRefresh(
toolbars.show && !toolbars.pinned,
onToggleFullContent = onToggleFullContent,
onRequestNext = onRequestNext,
onRequestPrevious = onRequestPrevious,
articles = articles,
) {
ArticleReader(
article = article,
webViewState = webViewState,
scrollState = scrollState,
)
key(article.id) {
ArticlePullRefresh(
toolbars.show && !toolbars.pinned,
onToggleFullContent = onToggleFullContent,
onRequestNext = onRequestNext,
onRequestPrevious = onRequestPrevious,
articles = articles,
) {
ArticleReader(
article = article,
webViewState = webViewState,
)
}
}
},
bottomBar = {
Expand Down
71 changes: 2 additions & 69 deletions app/src/main/java/com/capyreader/app/ui/components/WebView.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.capyreader.app.ui.components

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
Expand All @@ -16,14 +14,10 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.drawable.toBitmap
import androidx.webkit.WebViewAssetLoader
import androidx.webkit.WebViewAssetLoader.AssetsPathHandler
import androidx.webkit.WebViewAssetLoader.DEFAULT_DOMAIN
import androidx.webkit.WebViewAssetLoader.ResourcesPathHandler
import coil.executeBlocking
import coil.imageLoader
import coil.request.ImageRequest
import com.capyreader.app.common.AppPreferences
import com.capyreader.app.common.WebViewInterface
import com.capyreader.app.common.openLink
Expand All @@ -38,9 +32,6 @@ import kotlinx.coroutines.withContext
import org.koin.compose.koinInject
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream

/**
* Doesn't really fetch from androidplatform.net. This is used as a placeholder domain:
Expand Down Expand Up @@ -74,57 +65,17 @@ fun WebView(

class AccompanistWebViewClient(
private val assetLoader: WebViewAssetLoader,
private val onPageStarted: () -> Unit
) : WebViewClient(),
KoinComponent {
lateinit var state: WebViewState
internal set

private val appPreferences by inject<AppPreferences>()

override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)

view.postVisualStateCallback(requestId, object : WebView.VisualStateCallback() {
override fun onComplete(requestId: Long) {
onPageStarted()
view.visibility = View.VISIBLE
}
})
}

private val requestId = 1200L

override fun onPageFinished(view: WebView, url: String?) {
super.onPageFinished(view, url)
}

override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
val accept = request.requestHeaders.getOrDefault("Accept", null)

if (accept != null && accept.contains("image")) {
try {
val imageRequest = ImageRequest.Builder(view.context)
.data(request.url)
.build()
val bitmap =
view.context.imageLoader.executeBlocking(imageRequest).drawable?.toBitmap()

if (bitmap != null) {
return WebResourceResponse(
"image/jpg",
"UTF-8",
jpegStream(bitmap)
)
}
} catch (exception: Exception) {
return null
}
}

return assetLoader.shouldInterceptRequest(request.url)
}

Expand Down Expand Up @@ -156,12 +107,6 @@ class WebViewState(
val id = article.id

scope.launch {
if (htmlId != null && id != htmlId) {
webView.visibility = View.INVISIBLE
}

htmlId = id

withContext(Dispatchers.IO) {
val html = renderer.render(
article,
Expand All @@ -171,7 +116,7 @@ class WebViewState(

withContext(Dispatchers.Main) {
webView.loadDataWithBaseURL(
ASSET_BASE_URL,
null,
html,
null,
"UTF-8",
Expand All @@ -195,21 +140,18 @@ class WebViewState(
fun rememberWebViewState(
renderer: ArticleRenderer = koinInject(),
onNavigateToMedia: (url: String) -> Unit,
onPageStarted: () -> Unit,
): WebViewState {
val colors = articleTemplateColors()
val scope = rememberCoroutineScope()
val context = LocalContext.current

val client = remember {
AccompanistWebViewClient(
assetLoader =
WebViewAssetLoader.Builder()
assetLoader = WebViewAssetLoader.Builder()
.setDomain(DEFAULT_DOMAIN)
.addPathHandler("/assets/", AssetsPathHandler(context))
.addPathHandler("/res/", ResourcesPathHandler(context))
.build(),
onPageStarted = onPageStarted,
)
}

Expand Down Expand Up @@ -238,12 +180,3 @@ fun rememberWebViewState(
}
}
}

private fun jpegStream(
bitmap: Bitmap,
): InputStream {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
val bitmapData = byteArrayOutputStream.toByteArray()
return ByteArrayInputStream(bitmapData)
}
4 changes: 2 additions & 2 deletions article_forge/views/template.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
</style>
{{font_preload}}
<link rel="stylesheet" href="/assets/stylesheet.css">
<script type="text/javascript" src="/assets/media.js"></script>
<link rel="stylesheet" href="https://appassets.androidplatform.net/assets/stylesheet.css">
<script type="text/javascript" src="https://appassets.androidplatform.net/assets/media.js"></script>
</head>
<body>
<article>
Expand Down
2 changes: 0 additions & 2 deletions capy/src/main/java/com/jocmp/capy/articles/ArticleRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ class ArticleRenderer(

document.getElementById("article-body-content")?.append(article.content)

cleanLinks(document)

return document.html()
}

Expand Down
17 changes: 0 additions & 17 deletions capy/src/main/java/com/jocmp/capy/articles/CleanLinks.kt

This file was deleted.

2 changes: 0 additions & 2 deletions capy/src/main/java/com/jocmp/capy/articles/ParseHTML.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ fun parseHtml(article: Article, html: String): String {
element.append("&nbsp;")
}

cleanLinks(content)

return content.html()
} catch (ex: Throwable) {
return ""
Expand Down
4 changes: 2 additions & 2 deletions capy/src/main/res/raw/template.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/1071.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Added

• Add initial support for FreshRSS and Google Reader API

Fixed

• Fixed missing images on some articles