From 4aa32dbdb3a952b37fdb24efe7f3aa279399bf92 Mon Sep 17 00:00:00 2001 From: lysec Date: Tue, 21 Oct 2025 14:50:27 +0200 Subject: [PATCH] Notification: move lastSeenTs to cache/noctalia/notifications-state.json --- Assets/settings-default.json | 21 ++++---- Commons/Settings.qml | 1 - Modules/Bar/Widgets/NotificationHistory.qml | 6 +-- .../Notification/NotificationHistoryPanel.qml | 2 +- Services/NotificationService.qml | 52 ++++++++++++++++++- 5 files changed, 63 insertions(+), 19 deletions(-) diff --git a/Assets/settings-default.json b/Assets/settings-default.json index 904e3961..b98b0aff 100644 --- a/Assets/settings-default.json +++ b/Assets/settings-default.json @@ -179,17 +179,16 @@ "network": { "wifiEnabled": true }, - "notifications": { - "doNotDisturb": false, - "monitors": [], - "location": "top_right", - "alwaysOnTop": false, - "lastSeenTs": 0, - "respectExpireTimeout": false, - "lowUrgencyDuration": 3, - "normalUrgencyDuration": 8, - "criticalUrgencyDuration": 15 - }, + "notifications": { + "doNotDisturb": false, + "monitors": [], + "location": "top_right", + "alwaysOnTop": false, + "respectExpireTimeout": false, + "lowUrgencyDuration": 3, + "normalUrgencyDuration": 8, + "criticalUrgencyDuration": 15 + }, "osd": { "enabled": true, "location": "top_right", diff --git a/Commons/Settings.qml b/Commons/Settings.qml index 08005398..ab00de7e 100644 --- a/Commons/Settings.qml +++ b/Commons/Settings.qml @@ -313,7 +313,6 @@ Singleton { property list monitors: [] property string location: "top_right" property bool alwaysOnTop: false - property real lastSeenTs: 0 property bool respectExpireTimeout: false property int lowUrgencyDuration: 3 property int normalUrgencyDuration: 8 diff --git a/Modules/Bar/Widgets/NotificationHistory.qml b/Modules/Bar/Widgets/NotificationHistory.qml index 6d6652fe..ccceeaa3 100644 --- a/Modules/Bar/Widgets/NotificationHistory.qml +++ b/Modules/Bar/Widgets/NotificationHistory.qml @@ -31,12 +31,8 @@ NIconButton { readonly property bool showUnreadBadge: (widgetSettings.showUnreadBadge !== undefined) ? widgetSettings.showUnreadBadge : widgetMetadata.showUnreadBadge readonly property bool hideWhenZero: (widgetSettings.hideWhenZero !== undefined) ? widgetSettings.hideWhenZero : widgetMetadata.hideWhenZero - function lastSeenTs() { - return Settings.data.notifications?.lastSeenTs || 0 - } - function computeUnreadCount() { - var since = lastSeenTs() + var since = NotificationService.lastSeenTs var count = 0 var model = NotificationService.historyList for (var i = 0; i < model.count; i++) { diff --git a/Modules/Notification/NotificationHistoryPanel.qml b/Modules/Notification/NotificationHistoryPanel.qml index 76a8037b..b4121199 100644 --- a/Modules/Notification/NotificationHistoryPanel.qml +++ b/Modules/Notification/NotificationHistoryPanel.qml @@ -17,7 +17,7 @@ NPanel { panelKeyboardFocus: true onOpened: function () { - Settings.data.notifications.lastSeenTs = Time.timestamp * 1000 + NotificationService.updateLastSeenTs() } panelContent: Rectangle { diff --git a/Services/NotificationService.qml b/Services/NotificationService.qml index 4bb5042d..8a55af35 100644 --- a/Services/NotificationService.qml +++ b/Services/NotificationService.qml @@ -16,6 +16,10 @@ Singleton { property int maxVisible: 5 property int maxHistory: 100 property string historyFile: Quickshell.env("NOCTALIA_NOTIF_HISTORY_FILE") || (Settings.cacheDir + "notifications.json") + property string stateFile: Settings.cacheDir + "notifications-state.json" + + // State + property real lastSeenTs: 0 // Models property ListModel activeList: ListModel {} @@ -264,7 +268,7 @@ Singleton { saveHistory() } - // Persistence + // Persistence - History FileView { id: historyFileView path: historyFile @@ -281,6 +285,23 @@ Singleton { } } + // Persistence - State (lastSeenTs, etc.) + FileView { + id: stateFileView + path: stateFile + printErrors: false + onLoaded: loadState() + onLoadFailed: error => { + if (error === 2) + writeAdapter() + } + + JsonAdapter { + id: stateAdapter + property real lastSeenTs: 0 + } + } + Timer { id: saveTimer interval: 200 @@ -337,6 +358,35 @@ Singleton { } } + function loadState() { + try { + root.lastSeenTs = stateAdapter.lastSeenTs || 0 + + // Migration: if state file is empty but settings has lastSeenTs, migrate it + if (root.lastSeenTs === 0 && Settings.data.notifications && Settings.data.notifications.lastSeenTs) { + root.lastSeenTs = Settings.data.notifications.lastSeenTs + saveState() + Logger.i("Notifications", "Migrated lastSeenTs from settings to state file") + } + } catch (e) { + Logger.e("Notifications", "Load state failed:", e) + } + } + + function saveState() { + try { + stateAdapter.lastSeenTs = root.lastSeenTs + stateFileView.writeAdapter() + } catch (e) { + Logger.e("Notifications", "Save state failed:", e) + } + } + + function updateLastSeenTs() { + root.lastSeenTs = Time.timestamp * 1000 + saveState() + } + function getAppName(name) { if (!name || name.trim() === "") return "Unknown"