summaryrefslogtreecommitdiff
path: root/app/src/main/java/org/terst
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/terst')
-rw-r--r--app/src/main/java/org/terst/feedreader/data/ArticleSaving.kt35
1 files changed, 35 insertions, 0 deletions
diff --git a/app/src/main/java/org/terst/feedreader/data/ArticleSaving.kt b/app/src/main/java/org/terst/feedreader/data/ArticleSaving.kt
index f2bcb72..53d80eb 100644
--- a/app/src/main/java/org/terst/feedreader/data/ArticleSaving.kt
+++ b/app/src/main/java/org/terst/feedreader/data/ArticleSaving.kt
@@ -2,6 +2,8 @@ package org.terst.feedreader.data
import android.content.Context
import org.terst.feedreader.readability.ReaderExtractor
+import org.terst.feedreader.stash.StashClient
+import org.terst.feedreader.stash.StashSaveResult
import java.security.MessageDigest
fun urlToArticleId(url: String): String =
@@ -28,3 +30,36 @@ suspend fun ensureStandaloneArticle(context: Context, db: AppDatabase, url: Stri
db.articleDao().upsert(entity)
return entity
}
+
+/**
+ * Outcome of a Stash save, already carrying the exact snackbar text and (when the save
+ * succeeded, in either sense) the artifact id -- the same three-way handling (persist the
+ * id, pick a message) was independently copy-pasted at 5 call sites (ShareReceiverActivity,
+ * ReaderScreen x2, LinkReaderScreen x2) before this, all doing the identical
+ * request-build-and-branch dance. Most callers only need [message] (and, to also update
+ * their own local Compose state like `article = a.copy(stashArtifactId = ...)`,
+ * [artifactId]) -- it's a sealed class rather than a flat data class so a caller that DOES
+ * care about the Saved/AlreadySaved distinction (ShareReceiverActivity's wording does) can
+ * still pattern-match exhaustively instead of parsing [message] back apart.
+ */
+sealed class StashSaveOutcome(val message: String, val artifactId: String?) {
+ class Saved(artifactId: String) : StashSaveOutcome("Saved to Stash", artifactId)
+ class AlreadySaved(artifactId: String) : StashSaveOutcome("Already in Stash", artifactId)
+ class Failed(val reason: String) : StashSaveOutcome("Save failed: $reason", null)
+}
+
+suspend fun saveArticleToStash(context: Context, db: AppDatabase, articleId: String, url: String, title: String?): StashSaveOutcome {
+ val settings = Settings(context)
+ val client = StashClient(Net.client, settings.stashBaseUrlNow(), settings.stashApiKeyNow())
+ return when (val result = client.saveArticle(url, title)) {
+ is StashSaveResult.Saved -> {
+ db.articleDao().setStashArtifactId(articleId, result.artifact.id)
+ StashSaveOutcome.Saved(result.artifact.id)
+ }
+ is StashSaveResult.AlreadySaved -> {
+ db.articleDao().setStashArtifactId(articleId, result.existingId)
+ StashSaveOutcome.AlreadySaved(result.existingId)
+ }
+ is StashSaveResult.Failed -> StashSaveOutcome.Failed(result.message)
+ }
+}