Artwork caching (sorta)
This commit is contained in:
parent
c773058328
commit
380189df05
1 changed files with 58 additions and 39 deletions
|
@ -1,5 +1,6 @@
|
||||||
import fetch from "electron-fetch";
|
import fetch from "electron-fetch";
|
||||||
import {nativeImage, Notification} from "electron";
|
import {nativeImage, Notification} from "electron";
|
||||||
|
import NativeImage = Electron.NativeImage;
|
||||||
|
|
||||||
export default class playbackNotifications {
|
export default class playbackNotifications {
|
||||||
|
|
||||||
|
@ -15,6 +16,8 @@ export default class playbackNotifications {
|
||||||
|
|
||||||
private _utils: any;
|
private _utils: any;
|
||||||
private _notification: Notification | undefined;
|
private _notification: Notification | undefined;
|
||||||
|
private _artworkImage: { [key: string]: NativeImage } = {};
|
||||||
|
private _artworkNums: Array<string> = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates playback notification
|
* Creates playback notification
|
||||||
|
@ -24,13 +27,12 @@ export default class playbackNotifications {
|
||||||
if (this._notification) {
|
if (this._notification) {
|
||||||
this._notification.close();
|
this._notification.close();
|
||||||
}
|
}
|
||||||
fetch(a.artwork.url.replace('/{w}x{h}bb', '/512x512bb').replace('/2000x2000bb', '/35x35bb')).then(async blob => {
|
|
||||||
const artworkImage = nativeImage.createFromBuffer(Buffer.from(await blob.arrayBuffer()));
|
|
||||||
this._notification = new Notification({
|
this._notification = new Notification({
|
||||||
title: a.name,
|
title: a.name,
|
||||||
body: `${a.artistName} — ${a.albumName}`,
|
body: `${a.artistName} — ${a.albumName}`,
|
||||||
silent: true,
|
silent: true,
|
||||||
icon: artworkImage,
|
icon: this._artworkImage[a.artwork.url],
|
||||||
urgency: 'low',
|
urgency: 'low',
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
|
@ -46,8 +48,7 @@ export default class playbackNotifications {
|
||||||
<visual>
|
<visual>
|
||||||
<binding template="ToastGeneric">
|
<binding template="ToastGeneric">
|
||||||
<text id="1">${a.name ?? ''}</text>
|
<text id="1">${a.name ?? ''}</text>
|
||||||
<text id="2">${a.artistName ?? ''} - ${a.albumName ?? ''
|
<text id="2">${a.artistName ?? ''} - ${a.albumName ?? ''}</text>
|
||||||
}</text>
|
|
||||||
</binding>
|
</binding>
|
||||||
</visual>
|
</visual>
|
||||||
<actions>
|
<actions>
|
||||||
|
@ -56,13 +57,16 @@ export default class playbackNotifications {
|
||||||
</actions>
|
</actions>
|
||||||
</toast>`
|
</toast>`
|
||||||
});
|
});
|
||||||
|
|
||||||
this._notification.on('click', (event: any) => {
|
this._notification.on('click', (event: any) => {
|
||||||
this._utils.getWindow().show()
|
this._utils.getWindow().show()
|
||||||
this._utils.getWindow().focus()
|
this._utils.getWindow().focus()
|
||||||
})
|
})
|
||||||
|
|
||||||
this._notification.on('close', (event: any) => {
|
this._notification.on('close', (event: any) => {
|
||||||
this._notification = undefined;
|
this._notification = undefined;
|
||||||
})
|
})
|
||||||
|
|
||||||
this._notification.on('action', (event: any, action: any) => {
|
this._notification.on('action', (event: any, action: any) => {
|
||||||
if (action === 'Play/Pause') {
|
if (action === 'Play/Pause') {
|
||||||
this._utils.playback.playPause()
|
this._utils.playback.playPause()
|
||||||
|
@ -73,9 +77,9 @@ export default class playbackNotifications {
|
||||||
|
|
||||||
this._notification.show();
|
this._notification.show();
|
||||||
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
* Public Methods
|
* Public Methods
|
||||||
* ****************************************************************************************/
|
* ****************************************************************************************/
|
||||||
|
@ -88,7 +92,22 @@ export default class playbackNotifications {
|
||||||
console.debug(`[Plugin][${this.name}] Loading Complete.`);
|
console.debug(`[Plugin][${this.name}] Loading Complete.`);
|
||||||
|
|
||||||
utils.getIPCMain().on('playbackNotifications:create', (event: any, a: any) => {
|
utils.getIPCMain().on('playbackNotifications:create', (event: any, a: any) => {
|
||||||
|
a.artwork.url = a.artwork.url.replace('/{w}x{h}bb', '/512x512bb').replace('/2000x2000bb', '/35x35bb');
|
||||||
|
|
||||||
|
if (this._artworkNums.length > 20) {
|
||||||
|
delete this._artworkImage[this._artworkNums[0]];
|
||||||
|
this._artworkNums.shift();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._artworkImage[a.artwork.url]) {
|
||||||
this.createNotification(a);
|
this.createNotification(a);
|
||||||
|
} else {
|
||||||
|
fetch(a.artwork.url).then(async blob => {
|
||||||
|
this._artworkImage[a.artwork.url] = nativeImage.createFromBuffer(Buffer.from(await blob.arrayBuffer()));
|
||||||
|
this._artworkNums[this._artworkNums.length] = a.artwork.url;
|
||||||
|
this.createNotification(a);
|
||||||
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue