From 5fa1f30eb61d1f05d0d2e541a3667204e7451adf Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Mon, 13 Oct 2025 11:03:35 +0200 Subject: [PATCH] simplify code that determines an items content string and mimetype --- .../kotlin/de/unixwork/rssreader/Content.kt | 3 ++ .../kotlin/de/unixwork/rssreader/FeedList.kt | 39 ++----------------- .../main/kotlin/de/unixwork/rssreader/Item.kt | 13 +++++++ 3 files changed, 20 insertions(+), 35 deletions(-) create mode 100644 rss-application/src/main/kotlin/de/unixwork/rssreader/Content.kt diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Content.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Content.kt new file mode 100644 index 0000000..5503e2f --- /dev/null +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Content.kt @@ -0,0 +1,3 @@ +package de.unixwork.rssreader + +data class Content(val text: String, val type: String) 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 a325645..fa89888 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/FeedList.kt @@ -102,29 +102,13 @@ class FeedList(window: MainWindow) : Document() { window.window.ui.unsetState(MainWindow.ITEM_HAS_CATEGORY) } - var mimeType: String? = null - var content: String? = null - if(item.contentHtml != null) { - content = item.contentHtml - mimeType = "text/html" - } else if(item.contentText != null) { - content = item.contentText - mimeType = "text/plain" - } else if(item.description != null) { - content = item.description - mimeType = "text/html" - } else { - content = "" - mimeType = "text/plain" - } - if(item.isBookmark) { starred.setIntValue(1) } else { starred.setIntValue(0) } - - webview.loadContent(item.link, content, mimeType, "utf-8") + val content = item.getContent() + webview.loadContent(item.link, content.text, content.type, "utf-8") tabview.setIntValue(1) preview.setIntValue(0) // reset preview toggle button @@ -143,23 +127,8 @@ class FeedList(window: MainWindow) : Document() { fun togglePreview() { currentItem?.let { item -> if(preview.intValue() == 0) { - // TODO: codedup with selectItem, can we fix this? - var mimeType: String? = null - var content: String? = null - if(item.contentHtml != null) { - content = item.contentHtml - mimeType = "text/html" - } else if(item.contentText != null) { - content = item.contentText - mimeType = "text/plain" - } else if(item.description != null) { - content = item.description - mimeType = "text/html" - } else { - content = "" - mimeType = "text/plain" - } - webview.loadContent(item.link, content, mimeType, "utf-8") + val content = item.getContent() + webview.loadContent(item.link, content.text, content.type, "utf-8") } else { webview.loadUrl(item.link) } 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 15e90ed..e4d4228 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Item.kt @@ -21,4 +21,17 @@ class Item(id: Int) { var feedName: String? = null var feedUrl: String? = null + + fun getContent(): Content { + contentHtml?.let { + return Content(it, "text/html") + } + contentText?.let { + return Content(it, "text/plain") + } + description?.let { + return Content(it, "text/html") + } + return Content("", "text/plain") + } } \ No newline at end of file -- 2.47.3