summaryrefslogtreecommitdiff
path: root/app/src/main/java/org/terst/feedreader/ui/ReaderScreen.kt
blob: cef17c5f6ba708c235ca8238023efa5b661a30be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package org.terst.feedreader.ui

import android.content.Intent
import android.net.Uri
import android.webkit.WebView
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Article
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Bookmark
import androidx.compose.material.icons.filled.BookmarkBorder
import androidx.compose.material.icons.filled.OpenInBrowser
import androidx.compose.material.icons.outlined.Article
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.terst.feedreader.data.AppDatabase
import org.terst.feedreader.data.ArticleEntity
import org.terst.feedreader.data.Net
import org.terst.feedreader.data.Settings
import org.terst.feedreader.readability.ReaderExtractor
import org.terst.feedreader.stash.StashClient
import org.terst.feedreader.stash.StashSaveResult

// Below this length (of the plain feed-provided HTML), assume it's a teaser/summary,
// not the real article body, and auto-fall-back to on-device Readability extraction.
private const val THIN_CONTENT_THRESHOLD = 500

/**
 * Swipeable wrapper: snapshots the article-id order of the list this reader
 * was opened from (Unread/Saved/a specific feed) ONCE at entry, then pages
 * across that fixed queue. Deliberately a snapshot, not a live Flow -- opening
 * an article marks it read, which would otherwise drop it out of the live
 * Unread query mid-swipe and shift every subsequent page underneath the user.
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ReaderScreen(feedId: String, initialArticleId: String, onBack: () -> Unit) {
    val context = LocalContext.current
    val db = remember { AppDatabase.get(context) }
    var articleIds by remember(feedId) { mutableStateOf<List<String>?>(null) }

    LaunchedEffect(feedId) {
        val flow = when (feedId) {
            UNREAD_FEED_ID -> db.articleDao().observeUnread()
            SAVED_FEED_ID -> db.articleDao().observeSaved()
            else -> db.articleDao().observeByFeed(feedId)
        }
        articleIds = flow.first().map { it.id }
    }

    val ids = articleIds
    if (ids == null) {
        Box(modifier = Modifier.fillMaxSize()) {
            CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
        }
        return
    }
    // The article may have been opened from somewhere not in this queue (shouldn't normally
    // happen, but fall back to a single-item queue rather than crashing on indexOfFirst = -1).
    val safeIds = if (ids.contains(initialArticleId)) ids else listOf(initialArticleId)
    val initialIndex = safeIds.indexOf(initialArticleId).coerceAtLeast(0)
    val pagerState = rememberPagerState(initialPage = initialIndex) { safeIds.size }

    HorizontalPager(state = pagerState, modifier = Modifier.fillMaxSize()) { page ->
        ReaderContent(articleId = safeIds[page], onBack = onBack)
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun ReaderContent(articleId: String, onBack: () -> Unit) {
    val context = LocalContext.current
    val db = remember { AppDatabase.get(context) }
    val scope = rememberCoroutineScope()
    val snackbarHostState = remember { SnackbarHostState() }

    var article by remember(articleId) { mutableStateOf<ArticleEntity?>(null) }
    var feedContent by remember(articleId) { mutableStateOf<String?>(null) }
    var readerContent by remember(articleId) { mutableStateOf<String?>(null) }
    var isReaderMode by remember(articleId) { mutableStateOf(false) }
    var isExtracting by remember(articleId) { mutableStateOf(false) }
    var isSaving by remember(articleId) { mutableStateOf(false) }

    LaunchedEffect(articleId) {
        val loaded = db.articleDao().getById(articleId) ?: return@LaunchedEffect
        article = loaded
        db.articleDao().setRead(articleId, true)

        feedContent = loaded.feedContentHtml
        readerContent = loaded.extractedContent
        val plainLength = feedContent?.replace(Regex("<[^>]*>"), "")?.trim()?.length ?: 0

        if (readerContent == null && plainLength < THIN_CONTENT_THRESHOLD) {
            // Feed content is too thin to be the real article -- auto-extract, since in this
            // case reader mode isn't really an alternative view, it's the only real content.
            isExtracting = true
            val extracted = ReaderExtractor.extract(context, loaded.link)
            isExtracting = false
            extracted?.content?.let {
                readerContent = it
                db.articleDao().setExtractedContent(articleId, it)
            }
            isReaderMode = true
        }
    }

    fun toggleReaderMode() {
        val a = article ?: return
        if (isReaderMode) {
            isReaderMode = false
            return
        }
        if (readerContent != null) {
            isReaderMode = true
            return
        }
        scope.launch {
            isExtracting = true
            val extracted = ReaderExtractor.extract(context, a.link)
            isExtracting = false
            val content = extracted?.content
            if (content != null) {
                readerContent = content
                db.articleDao().setExtractedContent(articleId, content)
                isReaderMode = true
            } else {
                snackbarHostState.showSnackbar("Couldn't extract a reader view for this article")
            }
        }
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(article?.title.orEmpty(), maxLines = 1) },
                navigationIcon = {
                    IconButton(onClick = onBack) {
                        Icon(Icons.Default.ArrowBack, contentDescription = "Back")
                    }
                },
                actions = {
                    val currentArticle = article
                    // Only offer the toggle when there's an actual feed-provided alternative to
                    // switch back to -- if we already had to auto-extract (thin feed content),
                    // reader mode IS the only content, so there's nothing to toggle between.
                    if (feedContent != null) {
                        IconButton(enabled = !isExtracting, onClick = { toggleReaderMode() }) {
                            Icon(
                                if (isReaderMode) Icons.Filled.Article else Icons.Outlined.Article,
                                contentDescription = "Toggle reader view"
                            )
                        }
                    }
                    IconButton(onClick = {
                        val a = currentArticle ?: return@IconButton
                        val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(a.link))
                        context.startActivity(browserIntent)
                    }) {
                        Icon(Icons.Default.OpenInBrowser, contentDescription = "Open in browser")
                    }
                    IconButton(
                        enabled = !isSaving,
                        onClick = {
                            val a = currentArticle ?: return@IconButton
                            isSaving = true
                            scope.launch {
                                val settings = Settings(context)
                                val client = StashClient(Net.client, settings.stashBaseUrlNow(), settings.stashApiKeyNow())
                                when (val result = client.saveArticle(a.link, a.title)) {
                                    is StashSaveResult.Saved -> {
                                        db.articleDao().setSaved(a.id, true)
                                        db.articleDao().setStashArtifactId(a.id, result.artifact.id)
                                        snackbarHostState.showSnackbar("Saved to Stash")
                                    }
                                    is StashSaveResult.AlreadySaved -> {
                                        db.articleDao().setSaved(a.id, true)
                                        db.articleDao().setStashArtifactId(a.id, result.existingId)
                                        snackbarHostState.showSnackbar("Already in Stash")
                                    }
                                    is StashSaveResult.Failed ->
                                        snackbarHostState.showSnackbar("Save failed: ${result.message}")
                                }
                                isSaving = false
                            }
                        }
                    ) {
                        Icon(
                            if (currentArticle?.isSaved == true) Icons.Default.Bookmark else Icons.Default.BookmarkBorder,
                            contentDescription = "Save to Stash"
                        )
                    }
                }
            )
        },
        snackbarHost = { SnackbarHost(snackbarHostState) }
    ) { padding ->
        Box(modifier = Modifier.fillMaxSize().padding(padding)) {
            val content = if (isReaderMode) readerContent else (feedContent ?: readerContent)
            val baseUrl = article?.link
            if (content != null) {
                AndroidView(
                    modifier = Modifier.fillMaxSize(),
                    factory = { ctx ->
                        WebView(ctx).apply {
                            settings.javaScriptEnabled = false
                            loadDataWithBaseURL(baseUrl, wrapHtml(content), "text/html", "UTF-8", null)
                        }
                    },
                    update = { webView -> webView.loadDataWithBaseURL(baseUrl, wrapHtml(content), "text/html", "UTF-8", null) },
                    // AndroidView does not destroy() the underlying WebView on its own --
                    // without this, swiping through many articles (each page holding its own
                    // WebView) leaks a WebView's native resources per page and eventually
                    // exhausts memory over a long reading session.
                    onRelease = { webView -> webView.destroy() }
                )
            } else if (isExtracting) {
                CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
            }
        }
    }
}

private fun wrapHtml(bodyContent: String): String = """
    <html><head><meta name="viewport" content="width=device-width, initial-scale=1">
    <style>body { font-family: sans-serif; font-size: 18px; line-height: 1.5; padding: 16px; }
    img { max-width: 100%; height: auto; }</style></head>
    <body>$bodyContent</body></html>
""".trimIndent()