From: Olaf Wintermann Date: Fri, 29 Aug 2025 18:12:11 +0000 (+0200) Subject: move DB operations for loading feed items to background threads X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=56a67f15d4c3ffd02c55a60ba17b15478fa1fee8;p=rssreader.git move DB operations for loading feed items to background threads --- diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt index 29c14b9..022d9a5 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt @@ -7,4 +7,5 @@ class FeedCollection(id: Int, name: String) { val name = name var items = mutableListOf() var itemsLoaded = false + var itemsLoading = false } \ No newline at end of file diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt index 31cc8d7..d7bff02 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt @@ -2,6 +2,11 @@ package de.unixwork.rssreader import de.unixwork.ui.Document import de.unixwork.ui.UiLinkData +import de.unixwork.ui.kotlin.ToolkitDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.IO +import kotlinx.coroutines.launch class FeedList : Document() { val items = list("items") @@ -12,14 +17,37 @@ class FeedList : Document() { val webview = webview("webview") val tabview = integer("tabview") + // Feed that is currently shown var currentFeed: FeedCollection? = null + // Currently requested feed + var showFeed: FeedCollection? = null fun loadFeed(feed: FeedCollection) { - items.clear() + showFeed = feed if(!feed.itemsLoaded) { - feed.items = Database.getItems(feed, 10000) - feed.itemsLoaded = true + if(feed.itemsLoading) { + return // another loading job already in progress for this feed + } + + feed.itemsLoading = true + GlobalScope.launch(Dispatchers.IO) { + feed.items = Database.getItems(feed, 10000) + feed.itemsLoaded = true + // notify UI + GlobalScope.launch(ToolkitDispatcher) { + feed.itemsLoading = false + feed.itemsLoaded = true + if(showFeed == feed) { + loadFeed(feed) + } + } + } + return + } + + // Update item list + items.clear() feed.items.forEach { item -> items.add(item) }