settings: make update information actually work

This commit is contained in:
Alice Gaudon 2020-05-19 11:21:12 +02:00
parent 96f5796e8e
commit a947edc49e
4 changed files with 30 additions and 12 deletions

View File

@ -1,15 +1,20 @@
const {ipcRenderer, remote} = require('electron'); const {ipcRenderer, remote, shell} = require('electron');
let currentVersion; let currentVersion;
let updateStatus; let updateStatus;
let updateInfo;
let updateButton;
ipcRenderer.on('current-version', (e, version) => { ipcRenderer.on('current-version', (e, version) => {
currentVersion.innerText = `Version: ${version.version}`; currentVersion.innerText = `Version: ${version.version}`;
}); });
ipcRenderer.on('updateStatus', (e, available, version) => { ipcRenderer.on('updateStatus', (e, available, version) => {
console.log(available, version);
updateInfo = version;
if (available) { if (available) {
updateStatus.innerHTML = `A new update is available! <a href="https://github.com/ArisuOngaku/tabs/releases/v${version}">Click here to download manually</a>.`; updateStatus.innerHTML = 'A new update is available!';
updateButton.classList.remove('hidden');
} else { } else {
updateStatus.innerText = 'Tabs is up to date.'; updateStatus.innerText = 'Tabs is up to date.';
} }
@ -23,6 +28,11 @@ function save() {
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
currentVersion = document.getElementById('current-version'); currentVersion = document.getElementById('current-version');
updateStatus = document.getElementById('update-status'); updateStatus = document.getElementById('update-status');
updateButton = document.getElementById('download-button');
updateButton.addEventListener('click', () => {
shell.openExternal(`https://github.com/ArisuOngaku/tabs/releases/tag/v${updateInfo.version}`)
.catch(console.error);
});
ipcRenderer.send('syncSettings'); ipcRenderer.send('syncSettings');
ipcRenderer.send('checkForUpdates'); ipcRenderer.send('checkForUpdates');

View File

@ -26,6 +26,7 @@
<div class="updates"> <div class="updates">
<p id="current-version"></p> <p id="current-version"></p>
<p id="update-status">Loading...</p> <p id="update-status">Loading...</p>
<button id="download-button" type="button" class="hidden">Download</button>
</div> </div>
</div> </div>

View File

@ -1,20 +1,18 @@
import {autoUpdater} from "electron-updater"; import {autoUpdater} from "electron-updater";
export default class Updater { export default class Updater {
#callback = (available, data) => { #updateInfo;
console.log('Update:', available, data);
};
constructor() { constructor() {
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
autoUpdater.on('error', err => { autoUpdater.on('error', err => {
this.#callback(false, err); this.notifyUpdate(false, err);
}); });
autoUpdater.on('update-available', v => { autoUpdater.on('update-available', v => {
this.#callback(true, v); this.notifyUpdate(true, v);
}); });
autoUpdater.on('update-not-available', () => { autoUpdater.on('update-not-available', () => {
this.#callback(false); this.notifyUpdate(false);
}); });
} }
@ -22,15 +20,24 @@ export default class Updater {
* @param {Function} callback * @param {Function} callback
*/ */
checkForUpdates(callback) { checkForUpdates(callback) {
this.#callback = callback; if (this.#updateInfo) {
callback(this.#updateInfo.version !== this.getCurrentVersion().raw, this.#updateInfo);
return;
}
autoUpdater.checkForUpdates().then(r => { autoUpdater.checkForUpdates().then(r => {
this.#callback(false, r.updateInfo); this.#updateInfo = r.updateInfo;
callback(r.updateInfo.version !== this.getCurrentVersion().raw, r.updateInfo);
}).catch(err => { }).catch(err => {
this.#callback(false, err); callback(false, err);
}); });
} }
getCurrentVersion() { getCurrentVersion() {
return autoUpdater.currentVersion; return autoUpdater.currentVersion;
} }
notifyUpdate(available, data) {
console.log('Update:', available, data);
}
} }

View File

@ -234,7 +234,7 @@ async function createWindow() {
let checkForUpdatesListener; let checkForUpdatesListener;
ipcMain.on('checkForUpdates', checkForUpdatesListener = (e) => { ipcMain.on('checkForUpdates', checkForUpdatesListener = (e) => {
updater.checkForUpdates((available, version) => { updater.checkForUpdates((available, version) => {
ipcMain.emit('updateStatus', available, version); settingsWindow.webContents.send('updateStatus', available, version);
}); });
}); });
settingsWindow.on('close', () => { settingsWindow.on('close', () => {