Notification: move lastSeenTs to cache/noctalia/notifications-state.json

This commit is contained in:
lysec
2025-10-21 14:50:27 +02:00
parent 96cb0a5199
commit 4aa32dbdb3
5 changed files with 63 additions and 19 deletions
+10 -11
View File
@@ -179,17 +179,16 @@
"network": { "network": {
"wifiEnabled": true "wifiEnabled": true
}, },
"notifications": { "notifications": {
"doNotDisturb": false, "doNotDisturb": false,
"monitors": [], "monitors": [],
"location": "top_right", "location": "top_right",
"alwaysOnTop": false, "alwaysOnTop": false,
"lastSeenTs": 0, "respectExpireTimeout": false,
"respectExpireTimeout": false, "lowUrgencyDuration": 3,
"lowUrgencyDuration": 3, "normalUrgencyDuration": 8,
"normalUrgencyDuration": 8, "criticalUrgencyDuration": 15
"criticalUrgencyDuration": 15 },
},
"osd": { "osd": {
"enabled": true, "enabled": true,
"location": "top_right", "location": "top_right",
-1
View File
@@ -313,7 +313,6 @@ Singleton {
property list<string> monitors: [] property list<string> monitors: []
property string location: "top_right" property string location: "top_right"
property bool alwaysOnTop: false property bool alwaysOnTop: false
property real lastSeenTs: 0
property bool respectExpireTimeout: false property bool respectExpireTimeout: false
property int lowUrgencyDuration: 3 property int lowUrgencyDuration: 3
property int normalUrgencyDuration: 8 property int normalUrgencyDuration: 8
+1 -5
View File
@@ -31,12 +31,8 @@ NIconButton {
readonly property bool showUnreadBadge: (widgetSettings.showUnreadBadge !== undefined) ? widgetSettings.showUnreadBadge : widgetMetadata.showUnreadBadge readonly property bool showUnreadBadge: (widgetSettings.showUnreadBadge !== undefined) ? widgetSettings.showUnreadBadge : widgetMetadata.showUnreadBadge
readonly property bool hideWhenZero: (widgetSettings.hideWhenZero !== undefined) ? widgetSettings.hideWhenZero : widgetMetadata.hideWhenZero readonly property bool hideWhenZero: (widgetSettings.hideWhenZero !== undefined) ? widgetSettings.hideWhenZero : widgetMetadata.hideWhenZero
function lastSeenTs() {
return Settings.data.notifications?.lastSeenTs || 0
}
function computeUnreadCount() { function computeUnreadCount() {
var since = lastSeenTs() var since = NotificationService.lastSeenTs
var count = 0 var count = 0
var model = NotificationService.historyList var model = NotificationService.historyList
for (var i = 0; i < model.count; i++) { for (var i = 0; i < model.count; i++) {
@@ -17,7 +17,7 @@ NPanel {
panelKeyboardFocus: true panelKeyboardFocus: true
onOpened: function () { onOpened: function () {
Settings.data.notifications.lastSeenTs = Time.timestamp * 1000 NotificationService.updateLastSeenTs()
} }
panelContent: Rectangle { panelContent: Rectangle {
+51 -1
View File
@@ -16,6 +16,10 @@ Singleton {
property int maxVisible: 5 property int maxVisible: 5
property int maxHistory: 100 property int maxHistory: 100
property string historyFile: Quickshell.env("NOCTALIA_NOTIF_HISTORY_FILE") || (Settings.cacheDir + "notifications.json") 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 // Models
property ListModel activeList: ListModel {} property ListModel activeList: ListModel {}
@@ -264,7 +268,7 @@ Singleton {
saveHistory() saveHistory()
} }
// Persistence // Persistence - History
FileView { FileView {
id: historyFileView id: historyFileView
path: historyFile 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 { Timer {
id: saveTimer id: saveTimer
interval: 200 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) { function getAppName(name) {
if (!name || name.trim() === "") if (!name || name.trim() === "")
return "Unknown" return "Unknown"