From: Olaf Wintermann Date: Thu, 21 Aug 2025 16:30:56 +0000 (+0200) Subject: load items from DB, make items selectable X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=82dc67c9ce6f7e966d493a8d04503ce9df34c54b;p=rssreader.git load items from DB, make items selectable --- diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt index 79a0d06..84894e8 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt @@ -179,4 +179,34 @@ object Database { parent.feeds.add(feedCol) return feedCol } + + public fun getItems(feedCollection: FeedCollection, maxItems: Int) : MutableList { + val items = mutableListOf() + + connection.prepareStatement(""" + select I.*, F.URL from items I + inner join feeds F on I.feed_id = F.feed_id + where F.feedcollection_id = ? order by pub_date desc limit ? + """.trimIndent()).use { stmt -> + stmt.setInt(1, feedCollection.id) + stmt.setInt(2, maxItems) + stmt.executeQuery().use { rs -> + while(rs.next()) { + val item = Item(rs.getInt("item_id")) + item.feedId = rs.getInt("feed_id") + item.title = rs.getString("title") + item.link = rs.getString("link") + item.description = rs.getString("description") + item.author = rs.getString("author") + item.pubDate = rs.getString("pub_date") + item.guid = rs.getString("guid") + item.content = rs.getString("content") + item.feedName = feedCollection.name + item.feedUrl = rs.getString("URL") + items.add(item) + } + } + } + return items + } } 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 81ea4ad..29c14b9 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt @@ -5,6 +5,6 @@ import de.unixwork.ui.Document class FeedCollection(id: Int, name: String) { val id = id val name = name - val items = mutableListOf() + var items = mutableListOf() var itemsLoaded = 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 2a8b5ab..1010254 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt @@ -4,15 +4,27 @@ import de.unixwork.ui.Document class FeedList : Document() { val items = list("items") + val feedName = string("feedname") + val author = string("author") + val link = string("link") + val webview = webview("webview") fun loadFeed(feed: FeedCollection) { items.clear() - if(feed.itemsLoaded) { - feed.items.forEach { item -> - items.add(item) - } - } else { - // TODO: load items from DB + if(!feed.itemsLoaded) { + feed.items = Database.getItems(feed, 100) + feed.itemsLoaded = true } + feed.items.forEach { item -> + items.add(item) + } + items.update() + } + + fun selectItem(item: Item) { + feedName.setString(item.feedName) + author.setString(item.author) + link.setString(item.link) + webview.loadContent(null, item.content ?: "", "text/html", "utf-8") } } \ No newline at end of file diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt index 5eedce4..0bd14cd 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt @@ -1,6 +1,16 @@ package de.unixwork.rssreader -class Item { - val title: String? = null - val link: String? = null +class Item(id: Int) { + val id = id + var feedId = -1 + var title: String? = null + var link: String? = null + var description: String? = null + var author: String? = null + var pubDate: String? = null + var guid: String? = null + var content: String? = null + + var feedName: String? = null + var feedUrl: String? = null } \ No newline at end of file diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/MainWindow.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/MainWindow.kt index dca0ff4..dac0607 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/MainWindow.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/MainWindow.kt @@ -52,13 +52,37 @@ class MainWindow { hsplitpane(fill = true, initialPosition = 300) { vbox(fill = true) { - listview(varname = "items", fill = true) { elm, col -> + listview( + varname = "items", + fill = true, + onSelection = { event -> + feedList.items.selected?.let { + feedList.selectItem(it) + } + } + ) + { elm, col -> elm.title } } - vbox(fill = true) { - webview(varname = "webview", fill = true) + grid(fill = true, columnspacing = 8, rowspacing = 8, margin = 8) { + row { + rlabel("Feed:", hfill = true) + llabel(varname = "feedname", hexpand = true) + } + row { + rlabel("Author:", hfill = true) + llabel(varname = "author") + } + row { + rlabel("Link:", hfill = true) + llabel(varname = "link") + } + + row { + webview(varname = "webview", hfill = true, vfill = true, hexpand = true, vexpand = true, colspan = 2) + } } } diff --git a/ui-java/src/main/java/de/unixwork/ui/Context.java b/ui-java/src/main/java/de/unixwork/ui/Context.java index e1e22f8..b924217 100644 --- a/ui-java/src/main/java/de/unixwork/ui/Context.java +++ b/ui-java/src/main/java/de/unixwork/ui/Context.java @@ -47,6 +47,14 @@ public abstract class Context { return new UiInteger(this, name); } + public WebView webview() { + return webview(null); + } + + public WebView webview(String name) { + return new WebView(this, name); + } + public UiList list() { UiList ls = list(null); return ls; diff --git a/ui-java/src/main/java/de/unixwork/ui/Event.java b/ui-java/src/main/java/de/unixwork/ui/Event.java index f3786f6..f83bac2 100644 --- a/ui-java/src/main/java/de/unixwork/ui/Event.java +++ b/ui-java/src/main/java/de/unixwork/ui/Event.java @@ -51,6 +51,9 @@ public class Event { eventData = new SubListEventData(eventDataPtr); break; } + case LIST_SELECTION: { + break; + } case EventDataType.FILE_LIST: { int flistCount = (int)ui.filelist_count.invoke(eventDataPtr); String[] files = new String[flistCount]; diff --git a/ui-java/src/main/java/de/unixwork/ui/ListViewBuilder.java b/ui-java/src/main/java/de/unixwork/ui/ListViewBuilder.java index 376072c..aefab56 100644 --- a/ui-java/src/main/java/de/unixwork/ui/ListViewBuilder.java +++ b/ui-java/src/main/java/de/unixwork/ui/ListViewBuilder.java @@ -106,6 +106,11 @@ public class ListViewBuilder extends AbstractWidgetBuilder { return this; } + public ListViewBuilder onSelection(EventHandler onSelection) { + this.onSelection = onSelection; + return this; + } + public ListViewBuilder onDragStart(EventHandler onDragStart) { this.onDragStart = onDragStart; return this; diff --git a/ui-kotlin/src/main/kotlin/de/unixwork/ui/kotlin/Toplevel.kt b/ui-kotlin/src/main/kotlin/de/unixwork/ui/kotlin/Toplevel.kt index e8da16d..ba0ddf4 100644 --- a/ui-kotlin/src/main/kotlin/de/unixwork/ui/kotlin/Toplevel.kt +++ b/ui-kotlin/src/main/kotlin/de/unixwork/ui/kotlin/Toplevel.kt @@ -869,6 +869,8 @@ class Toplevel(obj: UiObject) { rowspan: Int = -1, name: String? = null, styleClass: String? = null, + onActivate: EventHandler? = null, + onSelection: EventHandler? = null, getvalue: ListValueConverter? = null ): UiWidget { varname?.let { @@ -907,6 +909,12 @@ class Toplevel(obj: UiObject) { styleClass?.let { list.styleClass(it) } + onActivate?.let { + list.onActivate(it) + } + onSelection?.let { + list.onSelection(it) + } getvalue?.let { list.getvalue(it) } @@ -926,6 +934,8 @@ class Toplevel(obj: UiObject) { rowspan: Int = -1, name: String? = null, styleClass: String? = null, + onActivate: EventHandler? = null, + onSelection: EventHandler? = null, getvalue: ListValueConverter? = null ): UiWidget { val list = ListView.list(ui) @@ -943,6 +953,8 @@ class Toplevel(obj: UiObject) { rowspan = rowspan, name = name, styleClass = styleClass, + onActivate = onActivate, + onSelection = onSelection, getvalue = getvalue ) } @@ -960,6 +972,7 @@ class Toplevel(obj: UiObject) { rowspan: Int = -1, name: String? = null, styleClass: String? = null, + onActivate: EventHandler? = null, getvalue: ListValueConverter? = null ): UiWidget { val list = ListView.dropdown(ui) @@ -977,6 +990,7 @@ class Toplevel(obj: UiObject) { rowspan = rowspan, name = name, styleClass = styleClass, + onActivate = onActivate, getvalue = getvalue ) }