refactor notifications plugin

This commit is contained in:
Araxeus
2021-05-10 05:00:58 +03:00
parent 7cf78c6635
commit cb743de7fd
2 changed files with 28 additions and 36 deletions

View File

@ -3,7 +3,7 @@ const is = require("electron-is");
const registerCallback = require("../../providers/song-info");
const { notificationImage } = require("./utils");
const { setupInteractive, notifyInteractive } = require("./interactive")
const setupInteractive = require("./interactive")
const notify = (info, options) => {
@ -23,37 +23,22 @@ const notify = (info, options) => {
return currentNotification;
};
module.exports = (win, options) => {
const isInteractive = is.windows() && options.interactive;
//setup interactive notifications for windows
if (isInteractive) {
setupInteractive(win, options.unpauseNotification);
}
const setup = (options) => {
let oldNotification;
let oldURL = "";
win.once("ready-to-show", () => {
// Register the callback for new song information
registerCallback(songInfo => {
// on pause - reset url? and skip notification
if (songInfo.isPaused) {
//reset oldURL if unpause notification option is on
if (options.unpauseNotification) {
oldURL = "";
}
return;
}
// If url isn't the same as last one - send notification
if (songInfo.url !== oldURL) {
oldURL = songInfo.url;
if (isInteractive) {
notifyInteractive(songInfo);
} else {
// 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, options) }, 10);
}
}
});
registerCallback(songInfo => {
if (!songInfo.isPaused || options.unpauseNotification) {
// 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, options) }, 10);
}
});
}
module.exports = (win, options) => {
// Register the callback for new song information
is.windows() && options.interactive ?
setupInteractive(win, options.unpauseNotification) :
setup(options);
};

View File

@ -1,18 +1,25 @@
const { notificationImage, icons } = require("./utils");
const getSongControls = require('../../providers/song-controls');
const registerCallback = require("../../providers/song-info");
const notifier = require("node-notifier");
//store song controls reference on launch
let controls;
let notificationOnPause;
//Save controls and onPause option
module.exports.setupInteractive = (win, unpauseNotification) => {
module.exports = (win, unpauseNotification) => {
//Save controls and onPause option
const { playPause, next, previous } = getSongControls(win);
controls = { playPause, next, previous };
notificationOnPause = unpauseNotification;
// Register songInfoCallback
registerCallback(songInfo => {
if (!songInfo.isPaused || notificationOnPause) {
sendToaster(songInfo);
}
});
win.webContents.once("closed", () => {
deleteNotification()
});
@ -33,7 +40,7 @@ function deleteNotification() {
}
//New notification
module.exports.notifyInteractive = function sendToaster(songInfo) {
function sendToaster(songInfo) {
deleteNotification();
//download image and get path
let imgSrc = notificationImage(songInfo, true);