From c8b6c5344c7194e2ef3336446919b66714062560 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Sun, 23 Aug 2026 00:47:56 +0000 Subject: Restore scroll position within an article on Next/Previous Next/Previous replaces this screen's own back-stack entry with a fresh one for the neighboring article, which recreates the WebView from scratch -- going back to an article you'd been partway through always landed you back at the top. Tracks each article's scroll offset in an in-memory, process-lifetime map (ReaderScrollPositions) as the WebView scrolls, and restores it once the freshly-loaded page finishes laying out. --- .../java/org/terst/feedreader/ui/ReaderWebView.kt | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'app/src/main/java/org/terst/feedreader/ui/ReaderWebView.kt') diff --git a/app/src/main/java/org/terst/feedreader/ui/ReaderWebView.kt b/app/src/main/java/org/terst/feedreader/ui/ReaderWebView.kt index f0873c8..31fcb02 100644 --- a/app/src/main/java/org/terst/feedreader/ui/ReaderWebView.kt +++ b/app/src/main/java/org/terst/feedreader/ui/ReaderWebView.kt @@ -23,6 +23,18 @@ import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties +// In-memory only (process lifetime, not persisted to disk) -- scoped to "don't lose my +// place if Next/Previous takes me away and back," not to surviving an app restart. A +// process-wide singleton rather than screen-scoped state so it survives an Activity +// recreation (rotation) too, not just Compose recomposition within the same screen instance. +object ReaderScrollPositions { + private val positions = mutableMapOf() + fun get(articleId: String): Int = positions[articleId] ?: 0 + fun set(articleId: String, scrollY: Int) { + positions[articleId] = scrollY + } +} + private fun isYouTubeWatchUrl(uri: Uri): Boolean { val host = uri.host?.lowercase() ?: return false return host == "youtu.be" || host.endsWith(".youtube.com") || host == "youtube.com" @@ -68,14 +80,27 @@ fun newReaderWebView( context: Context, onOpenLink: (String) -> Unit, onLongPressLink: (String) -> Unit = {}, - onYouTubeVideo: (String) -> Unit = {} + onYouTubeVideo: (String) -> Unit = {}, + onScrollChanged: (Int) -> Unit = {}, + onPageFinished: (WebView) -> Unit = {} ): WebView = WebView(context).apply { settings.javaScriptEnabled = false overScrollMode = View.OVER_SCROLL_NEVER isVerticalScrollBarEnabled = false isHorizontalScrollBarEnabled = false + // Reports the WebView's own scroll offset as the user reads, so a caller (ReaderScreen) + // can remember how far into an article they'd gotten and restore it -- e.g. after + // Next/Previous replaces this screen's back-stack entry with a fresh one for the same + // article, which recreates the WebView from scratch and would otherwise always land + // back at the top. + setOnScrollChangeListener { _, _, scrollY, _, _ -> onScrollChanged(scrollY) } webViewClient = object : WebViewClient() { + override fun onPageFinished(view: WebView, url: String?) { + super.onPageFinished(view, url) + onPageFinished(view) + } + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { // A subframe (an