add ArticleContent class and stub in webreader

This commit is contained in:
Satindar Dhillon 2022-09-19 09:40:44 -07:00
parent d0e3a06bd6
commit ab9a7dae88
5 changed files with 169 additions and 5 deletions

View file

@ -2,6 +2,6 @@ package app.omnivore.omnivore
sealed class Routes(val route: String) {
object Home : Routes("Home")
object WebReader : Routes("WebReader")
object WebAppReader : Routes("WebAppReader")
object Settings: Routes("Settings")
}

View file

@ -92,10 +92,12 @@ class HomeViewModel @Inject constructor(
readingProgress = it.node.readingProgressPercent,
readingProgressAnchor = it.node.readingProgressAnchorIndex,
imageURLString = it.node.image,
pageURLString = it.node.url,
descriptionText = it.node.description,
publisherURLString = it.node.originalArticleUrl,
author = it.node.author,
slug = it.node.slug
slug = it.node.slug,
publishDate = it.node.publishedAt
)
}
@ -133,12 +135,12 @@ public data class LinkedItem(
public val imageURLString: String?,
// public val onDeviceImageURLString: String?,
// public val documentDirectoryPath: String?,
// public val pageURLString: String,
public val pageURLString: String,
public val descriptionText: String?,
public val publisherURLString: String?,
// public val siteName: String?,
public val author: String?,
// public val publishDate: Any?,
public val publishDate: Any?,
public val slug: String,
// public val isArchived: Boolean,
// public val contentReader: String?,
@ -147,4 +149,8 @@ public data class LinkedItem(
fun publisherDisplayName(): String? {
return publisherURLString?.toUri()?.host
}
fun labelsJSONString(): String {
return ""
}
}

View file

@ -0,0 +1,44 @@
package app.omnivore.omnivore.ui.reader
import android.annotation.SuppressLint
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
import app.omnivore.omnivore.BuildConfig
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun WebReader(slug: String, authCookieString: String) {
WebView.setWebContentsDebuggingEnabled(true)
val url = BuildConfig.OMNIVORE_WEB_URL + "/app/me/$slug"
AndroidView(factory = {
WebView(it).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
settings.javaScriptEnabled = true
settings.allowContentAccess = true
settings.allowFileAccess = true
settings.domStorageEnabled = true
webViewClient = object : WebViewClient() {
}
CookieManager.getInstance().setAcceptThirdPartyCookies(this, true)
CookieManager.getInstance().setAcceptCookie(true)
CookieManager.getInstance().setCookie(BuildConfig.OMNIVORE_API_URL, authCookieString)
CookieManager.getInstance().setCookie(BuildConfig.OMNIVORE_WEB_URL, authCookieString) {
loadUrl(url)
}
}
}, update = {
it.loadUrl(url)
})
}

View file

@ -0,0 +1,106 @@
package app.omnivore.omnivore.ui.reader
import app.omnivore.omnivore.ui.home.LinkedItem
enum class WebFont(val displayText: String, val rawValue: String) {
INTER("Inter", "Inter"),
SYSTEM("System Default", "unset"),
OPEN_DYSLEXIC("Open Dyslexic", "OpenDyslexic"),
MERRIWEATHER("Merriweather", "Merriweather"),
LORA("Lora", "Lora"),
OPEN_SANS("Open Sans", "Open Sans"),
ROBOTO("Roboto", "Roboto"),
CRIMSON_TEXT("Crimson Text", "Crimson Text"),
SOURCE_SERIF_PRO("Source Serif Pro", "Source Serif Pro"),
Inter("Inter", "Inter"),
}
enum class ArticleContentStatus(val rawValue: String) {
FAILED("FAILED"),
PROCESSING("PROCESSING"),
SUCCEEDED("SUCCEEDED"),
UNKNOWN("UNKNOWN")
}
data class ArticleContent(
val title: String,
val htmlContent: String,
val highlightsJSONString: String,
val contentStatus: String, // ArticleContentStatus,
val objectID: String?, // whatever the Room Equivalent of objectID is
)
data class WebReaderContent(
val textFontSize: Int,
val lineHeight: Int,
val maxWidthPercentage: Int,
val item: LinkedItem,
val themeKey: String,
val fontFamily: WebFont,
val articleContent: ArticleContent,
val prefersHighContrastText: Boolean
) {
fun styledContent(): String {
// TODO: Kotlinize these three values (pasted from Swift)
val savedAt = "new Date((item.unwrappedSavedAt.timeIntervalSince1970 * 1000)).toISOString()"
val createdAt = "new Date((item.unwrappedCreatedAt.timeIntervalSince1970 * 1000)).toISOString()"
val publishedAt = if (item.publishDate != null) "new Date((item.publishDate!.timeIntervalSince1970 * 1000)).toISOString()" else "undefined"
return """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no' />
<style>
@import url("highlight${if (themeKey == "Gray") "-dark" else ""}.css");
</style>
</head>
<body>
<div id="root" />
<div>HIIIIII</div>
<div id='_omnivore-htmlContent' style="display: none;">
${articleContent.htmlContent}
</div>
<script type="text/javascript">
window.omnivoreEnv = {
"NEXT_PUBLIC_APP_ENV": "prod",
"NEXT_PUBLIC_BASE_URL": "unset",
"NEXT_PUBLIC_SERVER_BASE_URL": "unset",
"NEXT_PUBLIC_HIGHLIGHTS_BASE_URL": "unset"
}
window.omnivoreArticle = {
id: "${item.id}",
linkId: "${item.id}",
slug: "${item.slug}",
createdAt: $createdAt,
savedAt: $savedAt,
publishedAt: $publishedAt,
url: `${item.pageURLString}`,
title: `${articleContent.title.replace("`", "\\`")}`,
content: document.getElementById('_omnivore-htmlContent').innerHTML,
originalArticleUrl: "${item.pageURLString}",
contentReader: "WEB",
readingProgressPercent: ${item.readingProgress},
readingProgressAnchorIndex: ${item.readingProgressAnchor},
labels: ${item.labelsJSONString()},
highlights: ${articleContent.highlightsJSONString},
}
window.fontSize = $textFontSize
window.fontFamily = "${fontFamily.rawValue}"
window.maxWidthPercentage = $maxWidthPercentage
window.lineHeight = $lineHeight
window.localStorage.setItem("theme", "$themeKey")
window.prefersHighContrastFont = $prefersHighContrastText
window.enableHighlightBar = false
</script>
<script src="bundle.js"></script>
<script src="mathJaxConfiguration.js" id="MathJax-script"></script>
<script src="mathjax.js" id="MathJax-script"></script>
</body>
</html>
""".trimIndent()
}
}

View file

@ -19,6 +19,7 @@ import app.omnivore.omnivore.ui.auth.WelcomeScreen
import app.omnivore.omnivore.ui.home.HomeView
import app.omnivore.omnivore.ui.home.HomeViewModel
import app.omnivore.omnivore.ui.reader.ArticleWebView
import app.omnivore.omnivore.ui.reader.WebReader
import com.google.accompanist.systemuicontroller.rememberSystemUiController
@Composable
@ -69,13 +70,20 @@ fun PrimaryNavigator(
)
}
composable("WebReader/{slug}") {
composable("WebAppReader/{slug}") {
ArticleWebView(
it.arguments?.getString("slug") ?: "",
authCookieString = loginViewModel.getAuthCookieString() ?: ""
)
}
composable("WebReader/{slug}") {
WebReader(
it.arguments?.getString("slug") ?: "",
authCookieString = loginViewModel.getAuthCookieString() ?: ""
)
}
composable(Routes.Settings.route) {
SettingsView(loginViewModel = loginViewModel, navController = navController)
}