]> uap-core.de Git - rssreader.git/commitdiff
update unread counter in the sourcelist after a syncjob finishes
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 31 Aug 2025 10:58:07 +0000 (12:58 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 31 Aug 2025 10:58:07 +0000 (12:58 +0200)
rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt

index 9a68cba41dd5e1eb5d1f659a3dd2cefb65d19be8..c1a3bac3957182d727776067d1f72afc97bcaf46 100644 (file)
@@ -16,6 +16,7 @@ class App : Application {
             val window = event.windowData as MainWindow
             SyncJob().sync() {
                 window.sourceList.invalidateCache()
+                window.sourceList.reloadStatus()
                 window.feedList.reloadCurrentFeed()
             }
         }
index cd6ce927755ebfb3d65bb7f5302947a2944d0095..7c7f23936c880abcc487163a48ec0d18778de9d7 100644 (file)
@@ -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<FeedGroup>
 
     init {
-        val db = Database
-
-        groups = db.getFeedTree(this)
+        groups = Database.getFeedTree(this)
         groups.forEach {
             val sublist = SubList<FeedCollection>()
             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<Int, FeedCollection>()
+            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