From 3a6ba87f4f46784a26c0704bd9c97418e5251735 Mon Sep 17 00:00:00 2001 From: Akhmad Najmuddin Date: Fri, 2 Dec 2022 07:04:07 +0700 Subject: [PATCH] Fix updater --- package.json | 2 +- packages/main/index.ts | 4 ++-- packages/main/ipcEvents.ts | 21 +++++++++++++++++---- packages/main/updater.ts | 25 ++++++++++++++++--------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index e465584..b9ea6da 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "my-personal-list", "description": "My personal anime tracking list", - "version": "1.4.1", + "version": "1.4.2", "scripts": { "start": "concurrently \"yarn start:renderer\" \"yarn start:main\" --kill-others", "start:main": "tsc && electron .", diff --git a/packages/main/index.ts b/packages/main/index.ts index 12cb27d..0910118 100644 --- a/packages/main/index.ts +++ b/packages/main/index.ts @@ -17,10 +17,10 @@ const store = new Store({ }) app.on('ready', async () => { - createMainWindow() + const window = createMainWindow() createMainMenu(store) initializeIpcEvents(store) - initializeUpdater(store) + initializeUpdater(store, window) // Install React Extension if in dev mode if (!app.isPackaged) { diff --git a/packages/main/ipcEvents.ts b/packages/main/ipcEvents.ts index a4db522..3b423f9 100644 --- a/packages/main/ipcEvents.ts +++ b/packages/main/ipcEvents.ts @@ -8,6 +8,7 @@ import { IpcMainInvokeEvent, shell, Notification, + BrowserWindow, } from 'electron' import { nanoid } from 'nanoid' import { autoUpdater } from 'electron-updater' @@ -173,7 +174,7 @@ export class Events { } onChangeDataDir = async (): Promise => { - const res = await dialog.showOpenDialog({ + const res = await this.dialog.showOpenDialog({ properties: ['openDirectory'], }) @@ -222,11 +223,23 @@ export class Events { } onCheckForUpdate = async (e: IpcMainInvokeEvent) => { + const browser = BrowserWindow.fromWebContents(e.sender) + if (!browser) return + const res = await autoUpdater.checkForUpdates() - if (!res) return alert('No new update') + if (!res) { + this.dialog.showMessageBox(browser, { message: 'No new update' }) + return + } - const update = confirm(getUpdateAvailableMsg(res.updateInfo)) - if (!update) return + const answer = await this.dialog.showMessageBox(browser, { + title: 'Update Available', + message: getUpdateAvailableMsg(res.updateInfo), + type: 'question', + buttons: ['Later', 'Update Now'], + }) + // close = 0, Later = 0, Update Now = 1 + if (answer.response === 0) return await autoUpdater.downloadUpdate() autoUpdater.quitAndInstall() diff --git a/packages/main/updater.ts b/packages/main/updater.ts index 370b37b..00f4e3d 100644 --- a/packages/main/updater.ts +++ b/packages/main/updater.ts @@ -1,29 +1,36 @@ +import { BrowserWindow, dialog } from 'electron' import Store from 'electron-store' import { autoUpdater } from 'electron-updater' import { getUpdateAvailableMsg } from './util' -export const initializeUpdater = async (store: Store) => { +export const initializeUpdater = async ( + store: Store, + window: BrowserWindow, +) => { const lastCheck = store.get('lastUpdateCheck') const neverCheckUpdate = store.get('neverCheckUpdate') - const checked = lastCheck <= new Date().getTime() + 24 * 60 * 60 * 1000 + const checked = lastCheck + 24 * 60 * 60 * 1000 >= new Date().getTime() if (neverCheckUpdate || checked) return const res = await autoUpdater.checkForUpdates() store.set('lastUpdateCheck', new Date().getTime()) if (!res) return - const answer = prompt( - getUpdateAvailableMsg(res.updateInfo), - 'Answer with "now" for update now, "never" for never, or ignore it for ask again tomorrow.', - ) + const answer = await dialog.showMessageBox(window, { + title: 'Update Available', + message: getUpdateAvailableMsg(res.updateInfo), + type: 'question', + buttons: ['Later', 'Never', 'Now'], + }) - switch (answer) { - case 'now': + // close = 0, Later = 0, Never = 1, Now = 2 + switch (answer.response) { + case 2: await autoUpdater.downloadUpdate() autoUpdater.quitAndInstall() break - case 'never': + case 1: store.set('neverCheckUpdate', true) break