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
}
}
}
+
+ 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