setup app update api

This commit is contained in:
Abhinav 2022-10-31 11:05:04 +05:30
parent 3df11180ec
commit e33f28f38e
5 changed files with 49 additions and 19 deletions

View file

@ -9,3 +9,13 @@ export const showOnTray = (content: string) => {
export const reloadWindow = () => { export const reloadWindow = () => {
ipcRenderer.send('reload-window'); ipcRenderer.send('reload-window');
}; };
export const registerUpdateEventListener = (showUpdateDialog: () => void) => {
ipcRenderer.on('show-update-dialog', () => {
showUpdateDialog();
});
};
export const updateAndRestart = () => {
ipcRenderer.send('update-and-restart');
};

View file

@ -1,4 +1,10 @@
import { reloadWindow, sendNotification, showOnTray } from './api/system'; import {
registerUpdateEventListener,
reloadWindow,
sendNotification,
showOnTray,
updateAndRestart,
} from './api/system';
import { import {
showUploadDirsDialog, showUploadDirsDialog,
showUploadFilesDialog, showUploadFilesDialog,
@ -82,4 +88,6 @@ windowObject['ElectronAPIs'] = {
logToDisk, logToDisk,
convertHEIC, convertHEIC,
openLogDirectory, openLogDirectory,
registerUpdateEventListener,
updateAndRestart,
}; };

View file

@ -1,4 +1,4 @@
import { BrowserWindow, dialog, Tray } from 'electron'; import { BrowserWindow, Tray } from 'electron';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
import log from 'electron-log'; import log from 'electron-log';
import { setIsAppQuitting, setIsUpdateAvailable } from '../main'; import { setIsAppQuitting, setIsUpdateAvailable } from '../main';
@ -12,27 +12,34 @@ class AppUpdater {
async checkForUpdate(tray: Tray, mainWindow: BrowserWindow) { async checkForUpdate(tray: Tray, mainWindow: BrowserWindow) {
await autoUpdater.checkForUpdatesAndNotify(); await autoUpdater.checkForUpdatesAndNotify();
autoUpdater.on('update-downloaded', () => { autoUpdater.on('update-downloaded', () => {
showUpdateDialog(); showUpdateDialog(mainWindow);
setIsUpdateAvailable(true); setIsUpdateAvailable(true);
tray.setContextMenu(buildContextMenu(mainWindow)); tray.setContextMenu(buildContextMenu(mainWindow));
}); });
} }
updateAndRestart = () => {
setIsAppQuitting(true);
autoUpdater.quitAndInstall();
};
} }
export default new AppUpdater(); export default new AppUpdater();
export const showUpdateDialog = (): void => { export const showUpdateDialog = (mainWindow: BrowserWindow): void => {
dialog mainWindow.webContents.send('show-update-dialog');
.showMessageBox({
type: 'info', // dialog
title: 'Install update', // .showMessageBox({
message: 'Restart to update to the latest version of ente', // type: 'info',
buttons: ['Later', 'Restart now'], // title: 'Install update',
}) // message: 'Restart to update to the latest version of ente',
.then((buttonIndex) => { // buttons: ['Later', 'Restart now'],
if (buttonIndex.response === 1) { // })
setIsAppQuitting(true); // .then((buttonIndex) => {
autoUpdater.quitAndInstall(); // if (buttonIndex.response === 1) {
} // setIsAppQuitting(true);
}); // autoUpdater.quitAndInstall();
// }
// });
}; };

View file

@ -15,6 +15,7 @@ import chokidar from 'chokidar';
import path from 'path'; import path from 'path';
import { getDirFilePaths } from '../services/fs'; import { getDirFilePaths } from '../services/fs';
import { convertHEIC } from '../services/heicConvertor'; import { convertHEIC } from '../services/heicConvertor';
import appUpdater from '../services/appUpdater';
export default function setupIpcComs( export default function setupIpcComs(
tray: Tray, tray: Tray,
@ -106,4 +107,8 @@ export default function setupIpcComs(
ipcMain.handle('open-log-dir', () => { ipcMain.handle('open-log-dir', () => {
shell.openPath(app.getPath('logs')); shell.openPath(app.getPath('logs'));
}); });
ipcMain.handle('update-and-restart', () => {
appUpdater.updateAndRestart();
});
} }

View file

@ -25,11 +25,11 @@ export function buildContextMenu(
paused, paused,
} = args; } = args;
const contextMenu = Menu.buildFromTemplate([ const contextMenu = Menu.buildFromTemplate([
...(isUpdateAvailable() ...(!isUpdateAvailable()
? [ ? [
{ {
label: 'Update available', label: 'Update available',
click: () => showUpdateDialog(), click: () => showUpdateDialog(mainWindow),
}, },
] ]
: []), : []),