From 24e4e4f5e9b3c84d7c5a9a2400b8409d65e73aff Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 31 Aug 2025 12:58:07 +0200 Subject: [PATCH] update unread counter in the sourcelist after a syncjob finishes --- .../main/kotlin/de/unixwork/rssreader/App.kt | 1 + .../de/unixwork/rssreader/FeedSourceList.kt | 43 +++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt index 9a68cba..c1a3bac 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt @@ -16,6 +16,7 @@ class App : Application { val window = event.windowData as MainWindow SyncJob().sync() { window.sourceList.invalidateCache() + window.sourceList.reloadStatus() window.feedList.reloadCurrentFeed() } } diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt index cd6ce92..7c7f239 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt @@ -2,15 +2,18 @@ package de.unixwork.rssreader import de.unixwork.ui.Document import de.unixwork.ui.SubList +import de.unixwork.ui.kotlin.ToolkitDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.IO +import kotlinx.coroutines.launch class FeedSourceList : Document() { val feeds = this.sourcelist("feeds") val groups: MutableList init { - val db = Database - - groups = db.getFeedTree(this) + groups = Database.getFeedTree(this) groups.forEach { val sublist = SubList() sublist.header = it.name @@ -35,4 +38,38 @@ class FeedSourceList : Document() { } } } + + fun reloadStatus() { + // Reload the current feed tree from the database + // and update the current items + // TODO: it would be simpler to replace the current tree + // however it is currently not possible to cleanup the sourcelist + + GlobalScope.launch(Dispatchers.IO) { + // get the current feed tree + val groupUpdate = Database.getFeedTree(this@FeedSourceList) + // create a map that contains all feed collections + val index = mutableMapOf() + groupUpdate.forEach { + it.feeds.forEach { feed -> + index[feed.id] = feed + } + } + + // Update feeds in the UI thread + GlobalScope.launch(ToolkitDispatcher) { + groups.forEach { group -> + group.feeds.forEach { feed -> + val updatedFeed = index[feed.id] + updatedFeed?.let { + println("Updating feed ${feed.name} : ${feed.unreadItemsCount} -> ${updatedFeed.unreadItemsCount}") + feed.unreadItemsCount = updatedFeed.unreadItemsCount + } + } + } + feeds.update() + } + + } + } } \ No newline at end of file -- 2.47.3