Skip to content

Commit

Permalink
Refresh articles based on filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 30, 2023
1 parent 1dcee77 commit 3bb432e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ class AccountViewModel(
}

suspend fun refreshFeed() {
// val id = feedID.value ?: return
//
// account?.refreshFeed(id)
when (val currentFilter = filter.value) {
is ArticleFilter.Feeds -> account?.refreshFeed(currentFilter.feed)
is ArticleFilter.Folders -> account?.refreshFeeds(currentFilter.folder.feeds)
is ArticleFilter.Articles -> account?.refreshAll()
}
}

fun selectArticle(articleID: String) {
Expand Down
57 changes: 42 additions & 15 deletions basil/src/main/java/com/jocmp/basil/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.jocmp.basil.accounts.LocalAccountDelegate
import com.jocmp.basil.accounts.ParsedItem
import com.jocmp.basil.accounts.asOPML
import com.jocmp.basil.db.Database
import com.jocmp.basil.db.Feeds
import com.jocmp.basil.opml.Outline
import com.jocmp.basil.opml.asFeed
import com.jocmp.basil.opml.asFolder
Expand All @@ -13,6 +14,8 @@ import com.jocmp.basil.persistence.articleMapper
import com.jocmp.basil.shared.nowUTC
import com.jocmp.feedfinder.FeedFinder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -115,12 +118,18 @@ data class Account(
return Result.success(feed)
}

suspend fun refreshFeed(feedID: String) {
val feed = flattenedFeeds.find { it.id == feedID } ?: return
suspend fun refreshAll() {
refreshCompactedFeeds(flattenedFeeds)
}

suspend fun refreshFeed(feed: Feed) {
refreshFeeds(listOf(feed))
}

val items = delegate.fetchAll(feed)
suspend fun refreshFeeds(feeds: List<Feed>) {
val ids = feeds.map { it.id }.toSet()

updateArticles(feed, items)
refreshCompactedFeeds(flattenedFeeds.filter { ids.contains(it.id) })
}

fun findFeed(feedID: String): Feed? {
Expand All @@ -144,20 +153,38 @@ data class Account(
items.forEach { item ->
val publishedAt = item.publishedAt?.toEpochSecond()

database.articlesQueries.create(
feed_id = feed.primaryKey,
external_id = item.externalID,
title = item.title,
content_html = item.contentHTML,
url = item.url,
summary = item.summary,
image_url = item.imageURL,
published_at = publishedAt,
arrived_at = publishedAt ?: nowUTC()
)
database.transaction {
database.articlesQueries.create(
feed_id = feed.primaryKey,
external_id = item.externalID,
title = item.title,
content_html = item.contentHTML,
url = item.url,
summary = item.summary,
image_url = item.imageURL,
published_at = publishedAt,
)

database.articlesQueries.updateStatus(
feed_id = feed.primaryKey,
external_id = item.externalID,
arrived_at = publishedAt ?: nowUTC()
)
}

}
}

private suspend fun refreshCompactedFeeds(feeds: Collection<Feed>) =
withContext(Dispatchers.IO) {
feeds.map { feed ->
async {
val items = delegate.fetchAll(feed)
updateArticles(feed, items)
}
}.awaitAll()
}

private fun entrySiteURL(url: URL?): String {
return url?.toString() ?: ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import kotlin.Boolean;

CREATE TABLE article_statuses (
feed_id INTEGER NOT NULL REFERENCES feeds(id),
external_id INTEGER NOT NULL REFERENCES articles(external_id),
external_id TEXT NOT NULL REFERENCES articles(external_id),
read INTEGER AS Boolean DEFAULT 0,
starred INTEGER AS Boolean DEFAULT 0,
arrived_at INTEGER NOT NULL,
Expand Down
64 changes: 32 additions & 32 deletions basil/src/main/sqldelight/com/jocmp/basil/db/articles.sq
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,36 @@ JOIN article_statuses ON articles.external_id = article_statuses.external_id AND
WHERE articles.id = :articleID
LIMIT 1;

create {
REPLACE INTO articles(
feed_id,
external_id,
title,
content_html,
url,
summary,
image_url,
published_at
)
VALUES (
:feed_id,
:external_id,
:title,
:content_html,
:url,
:summary,
:image_url,
:published_at
);
create:
INSERT OR IGNORE INTO articles(
feed_id,
external_id,
title,
content_html,
url,
summary,
image_url,
published_at
)
VALUES (
:feed_id,
:external_id,
:title,
:content_html,
:url,
:summary,
:image_url,
:published_at
);

INSERT OR IGNORE INTO article_statuses(
feed_id,
external_id,
arrived_at
)
VALUES (
:feed_id,
:external_id,
:arrived_at
);
}
updateStatus:
INSERT OR IGNORE INTO article_statuses(
feed_id,
external_id,
arrived_at
)
VALUES (
:feed_id,
:external_id,
:arrived_at
);

0 comments on commit 3bb432e

Please sign in to comment.