]> uap-core.de Git - rssreader.git/commitdiff
load items from DB, make items selectable
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 21 Aug 2025 16:30:56 +0000 (18:30 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 21 Aug 2025 16:30:56 +0000 (18:30 +0200)
rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedCollection.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/MainWindow.kt
ui-java/src/main/java/de/unixwork/ui/Context.java
ui-java/src/main/java/de/unixwork/ui/Event.java
ui-java/src/main/java/de/unixwork/ui/ListViewBuilder.java
ui-kotlin/src/main/kotlin/de/unixwork/ui/kotlin/Toplevel.kt

index 79a0d06fe90b0ab85fb85d13c2e431a9fed34890..84894e850513e74c7f9217f6f7bdcd858c5a9abf 100644 (file)
@@ -179,4 +179,34 @@ object Database {
         parent.feeds.add(feedCol)
         return feedCol
     }
+
+    public fun getItems(feedCollection: FeedCollection, maxItems: Int) : MutableList<Item> {
+        val items = mutableListOf<Item>()
+
+        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
+    }
 }
index 81ea4ade75416e00e4c19d424d0c9f619271d87c..29c14b9b22716d102cd756d42b475e7f5af13d63 100644 (file)
@@ -5,6 +5,6 @@ import de.unixwork.ui.Document
 class FeedCollection(id: Int, name: String)  {
     val id = id
     val name = name
-    val items = mutableListOf<Item>()
+    var items = mutableListOf<Item>()
     var itemsLoaded = false
 }
\ No newline at end of file
index 2a8b5abf44e3cb92ab06281fa495620c1fe64152..1010254123ac7b3a42a43d124b0c88262d507811 100644 (file)
@@ -4,15 +4,27 @@ import de.unixwork.ui.Document
 
 class FeedList : Document() {
     val items = list<Item>("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
index 5eedce43ed1b31072432dcd283e2595f966b2483..0bd14cd1017aa845577eb23090e19ea4377cec50 100644 (file)
@@ -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
index dca0ff48bea464c74edf1f222da3e80bb67aabe1..dac0607f73c8bf0b74c178f895a0e5123d38811b 100644 (file)
@@ -52,13 +52,37 @@ class MainWindow {
 
             hsplitpane(fill = true, initialPosition = 300) {
                 vbox(fill = true) {
-                    listview<Item>(varname = "items", fill = true) { elm, col ->
+                    listview<Item>(
+                        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)
+                    }
                 }
             }
 
index e1e22f84dfe9258c875f4e18cba9c340b1334dd3..b9242171caa4b2a151286065d7373c2eb9ea610a 100644 (file)
@@ -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 <T> UiList<T> list() {
         UiList<T> ls = list(null);
         return ls;
index f3786f6de47e200f304b9eb6d8df36eaebe9bbba..f83bac20c3028c5e373579fad13608e1a7a6a61d 100644 (file)
@@ -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];
index 376072c5940e2cce806ed4a72bc69f2db2f50f05..aefab56fecea9fe733e4c492bb8c6ffb19948527 100644 (file)
@@ -106,6 +106,11 @@ public class ListViewBuilder<T> extends AbstractWidgetBuilder {
         return this;
     }
 
+    public ListViewBuilder<T> onSelection(EventHandler onSelection) {
+        this.onSelection = onSelection;
+        return this;
+    }
+
     public ListViewBuilder<T> onDragStart(EventHandler onDragStart) {
         this.onDragStart = onDragStart;
         return this;
index e8da16dacc1ec206d3b72a7dd32e9cb39aed7f6f..ba0ddf48b5fb9e71fe59f8045decba2065f9d36b 100644 (file)
@@ -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<T>? = 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<T>? = null
     ): UiWidget {
         val list = ListView.list<T>(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<T>? = null
     ): UiWidget {
         val list = ListView.dropdown<T>(ui)
@@ -977,6 +990,7 @@ class Toplevel(obj: UiObject) {
             rowspan = rowspan,
             name = name,
             styleClass = styleClass,
+            onActivate = onActivate,
             getvalue = getvalue
         )
     }