notifications: avoid id collisions across sessions

This commit is contained in:
end-4
2025-04-20 08:44:06 +02:00
parent 3096107d6b
commit b5a9e01455
2 changed files with 16 additions and 5 deletions
@@ -15,7 +15,7 @@ Button {
leftPadding: 10 leftPadding: 10
rightPadding: 10 rightPadding: 10
PointingHandInteraction {} // PointingHandInteraction {}
background: Rectangle { background: Rectangle {
radius: Appearance.rounding.small radius: Appearance.rounding.small
+15 -4
View File
@@ -11,6 +11,9 @@ Singleton {
id: root id: root
property var filePath: `${StandardPaths.standardLocations(StandardPaths.CacheLocation)[0]}/notifications/notifications.json` property var filePath: `${StandardPaths.standardLocations(StandardPaths.CacheLocation)[0]}/notifications/notifications.json`
property var list: [] property var list: []
// Quickshell's notification IDs starts at 1 on each run, while saved notifications
// can already contain higher IDs. This is a workaround to avoid id collisions
property int idOffset
signal initDone(); signal initDone();
signal notify(notification: var); signal notify(notification: var);
@@ -31,7 +34,7 @@ Singleton {
onNotification: (notification) => { onNotification: (notification) => {
notification.tracked = true notification.tracked = true
const newNotifObject = { const newNotifObject = {
"id": notification.id, "id": notification.id + root.idOffset,
"actions": notification.actions.map((action) => { "actions": notification.actions.map((action) => {
return { return {
"identifier": action.identifier, "identifier": action.identifier,
@@ -54,7 +57,7 @@ Singleton {
function discardNotification(id) { function discardNotification(id) {
const index = root.list.findIndex((notif) => notif.id === id); const index = root.list.findIndex((notif) => notif.id === id);
const notifServerIndex = notifServer.trackedNotifications.values.findIndex((notif) => notif.id === id); const notifServerIndex = notifServer.trackedNotifications.values.findIndex((notif) => notif.id + root.idOffset === id);
if (index !== -1) { if (index !== -1) {
root.list.splice(index, 1); root.list.splice(index, 1);
notifFileView.setText(JSON.stringify(root.list, null, 2)) notifFileView.setText(JSON.stringify(root.list, null, 2))
@@ -76,12 +79,13 @@ Singleton {
} }
function attemptInvokeAction(id, notifIdentifier) { function attemptInvokeAction(id, notifIdentifier) {
const notifServerIndex = notifServer.trackedNotifications.values.findIndex((notif) => notif.id === id); const notifServerIndex = notifServer.trackedNotifications.values.findIndex((notif) => notif.id + root.idOffset === id);
if (notifServerIndex !== -1) { if (notifServerIndex !== -1) {
const notifServerNotif = notifServer.trackedNotifications.values[notifServerIndex]; const notifServerNotif = notifServer.trackedNotifications.values[notifServerIndex];
const action = notifServerNotif.actions.find((action) => action.identifier === notifIdentifier); const action = notifServerNotif.actions.find((action) => action.identifier === notifIdentifier);
action.invoke() action.invoke()
} else console.log("Notification not found in server: " + id) }
// else console.log("Notification not found in server: " + id)
root.discard(id); root.discard(id);
} }
@@ -103,7 +107,14 @@ Singleton {
onLoaded: { onLoaded: {
const fileContents = notifFileView.text() const fileContents = notifFileView.text()
root.list = JSON.parse(fileContents) root.list = JSON.parse(fileContents)
// Find largest id
let maxId = 0
root.list.forEach((notif) => {
maxId = Math.max(maxId, notif.id)
})
console.log("[Notifications] File loaded") console.log("[Notifications] File loaded")
root.idOffset = maxId
root.initDone() root.initDone()
} }
onLoadFailed: (error) => { onLoadFailed: (error) => {