From c1ee58b47f033f0e94b57bc9aade0619be9b4910 Mon Sep 17 00:00:00 2001 From: Sem Visscher Date: Mon, 1 Feb 2021 20:18:16 +0100 Subject: [PATCH 1/4] Reuse the same notifcation, instead of creating a new notification each time. --- plugins/notifications/back.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index d70898cc..e0e98407 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -2,7 +2,7 @@ const { Notification } = require("electron"); const getSongInfo = require("../../providers/song-info"); -const notify = (info) => { +const notify = (info, notification) => { let notificationImage = "assets/youtube-music.png"; if (info.image) { @@ -10,25 +10,31 @@ const notify = (info) => { } // Fill the notification with content - const notification = { - title: info.title || "Playing", - body: info.artist, - icon: notificationImage, - silent: true, - }; + notification.title = info.title || "Playing"; + notification.body = info.artist; + notification.icon = notificationImage; + // Send the notification - new Notification(notification).show(); + notification.show(); }; module.exports = (win) => { const registerCallback = getSongInfo(win); - + + // Create a notification + let notification = new Notification( { + title:'', + body: '', + icon: "assets/youtube-music.png", + silent: true, + }); + win.on("ready-to-show", () => { // Register the callback for new song information registerCallback((songInfo) => { // If song is playing send notification if (!songInfo.isPaused) { - notify(songInfo); + notify(songInfo, notification); } }); }); From df627788c982c0967f39e3a664881eed3ee2baf8 Mon Sep 17 00:00:00 2001 From: Sem Visscher Date: Mon, 1 Feb 2021 20:23:46 +0100 Subject: [PATCH 2/4] small formatting changes --- plugins/notifications/back.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index e0e98407..4b217968 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -10,7 +10,7 @@ const notify = (info, notification) => { } // Fill the notification with content - notification.title = info.title || "Playing"; + notification.title = info.title || "Playing"; notification.body = info.artist; notification.icon = notificationImage; @@ -23,8 +23,8 @@ module.exports = (win) => { // Create a notification let notification = new Notification( { - title:'', - body: '', + title: "", + body: "", icon: "assets/youtube-music.png", silent: true, }); From 1fdf2416ad414035104bfb51b8450d82e566cb13 Mon Sep 17 00:00:00 2001 From: Sem Visscher Date: Mon, 1 Feb 2021 21:34:56 +0100 Subject: [PATCH 3/4] Update back.js --- plugins/notifications/back.js | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index 4b217968..3b588833 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -1,8 +1,7 @@ const { Notification } = require("electron"); - const getSongInfo = require("../../providers/song-info"); -const notify = (info, notification) => { +const notify = (info) => { let notificationImage = "assets/youtube-music.png"; if (info.image) { @@ -10,31 +9,32 @@ const notify = (info, notification) => { } // Fill the notification with content - notification.title = info.title || "Playing"; - notification.body = info.artist; - notification.icon = notificationImage; - + const notification = { + title: info.title || "Playing", + body: info.artist, + icon: notificationImage, + silent: true, + }; + // Send the notification - notification.show(); + currentNotification = new Notification(notification); + currentNotification.show() + + return currentNotification; }; module.exports = (win) => { const registerCallback = getSongInfo(win); - - // Create a notification - let notification = new Notification( { - title: "", - body: "", - icon: "assets/youtube-music.png", - silent: true, - }); - + let oldNotification; win.on("ready-to-show", () => { // Register the callback for new song information registerCallback((songInfo) => { // If song is playing send notification - if (!songInfo.isPaused) { - notify(songInfo, notification); + if (!songInfo.isPaused) { + // Close the old notification + oldNotification?.close(); + // This fixes a weird bug that would cause the notification to be updated instead of showing + setTimeout(()=>{ oldNotification = notify(songInfo) }, 1); } }); }); From e75dfcf41cc2abda403bcac3dd253fb4639d4ab3 Mon Sep 17 00:00:00 2001 From: Sem Visscher Date: Mon, 1 Feb 2021 22:00:18 +0100 Subject: [PATCH 4/4] little fixes --- plugins/notifications/back.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index 3b588833..4fc1d87d 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -1,7 +1,7 @@ const { Notification } = require("electron"); const getSongInfo = require("../../providers/song-info"); -const notify = (info) => { +const notify = info => { let notificationImage = "assets/youtube-music.png"; if (info.image) { @@ -28,13 +28,13 @@ module.exports = (win) => { let oldNotification; win.on("ready-to-show", () => { // Register the callback for new song information - registerCallback((songInfo) => { + registerCallback(songInfo => { // If song is playing send notification if (!songInfo.isPaused) { // Close the old notification oldNotification?.close(); // This fixes a weird bug that would cause the notification to be updated instead of showing - setTimeout(()=>{ oldNotification = notify(songInfo) }, 1); + setTimeout(()=>{ oldNotification = notify(songInfo) }, 10); } }); });