Skip to content

Commit

Permalink
Fix updater, again
Browse files Browse the repository at this point in the history
  • Loading branch information
SnekNOTSnake committed Dec 2, 2022
1 parent 3a6ba87 commit 19f9240
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "my-personal-list",
"description": "My personal anime tracking list",
"version": "1.4.2",
"version": "1.4.3",
"scripts": {
"start": "concurrently \"yarn start:renderer\" \"yarn start:main\" --kill-others",
"start:main": "tsc && electron .",
Expand Down
39 changes: 25 additions & 14 deletions packages/main/ipcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,26 +223,37 @@ export class Events {
}

onCheckForUpdate = async (e: IpcMainInvokeEvent) => {
// Make all of the below listeners only apply in here
autoUpdater.removeAllListeners()

const browser = BrowserWindow.fromWebContents(e.sender)
if (!browser) return

const res = await autoUpdater.checkForUpdates()
if (!res) {
this.dialog.showMessageBox(browser, { message: 'No new update' })
return
}
autoUpdater.on('update-available', async (info) => {
const answer = await this.dialog.showMessageBox(browser, {
title: 'Update Available',
message: getUpdateAvailableMsg(info),
type: 'question',
buttons: ['Not Now', 'Update'],
})

const answer = await this.dialog.showMessageBox(browser, {
title: 'Update Available',
message: getUpdateAvailableMsg(res.updateInfo),
type: 'question',
buttons: ['Later', 'Update Now'],
// close = 0, Not Now = 0, Update = 1
switch (answer.response) {
case 1:
await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
break

default:
break
}
})

autoUpdater.on('update-not-available', () => {
this.dialog.showMessageBox(browser, { message: 'No new update' })
})
// close = 0, Later = 0, Update Now = 1
if (answer.response === 0) return

await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
await autoUpdater.checkForUpdates()
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Menu, MenuItemConstructorOptions } from 'electron'
import Store from 'electron-store'
import { autoUpdater } from 'electron-updater'

import { IPCKey } from '../common/constants'

Expand All @@ -12,7 +13,7 @@ const createTemplate = (
{ label: 'About My Personal List' },
{
label: 'Check For Updates',
click: async (menuItem, browser) => {
click: (menuItem, browser) => {
if (!browser) return
browser.webContents.send(IPCKey.CheckForUpdate)
},
Expand Down Expand Up @@ -50,7 +51,7 @@ const createTemplate = (
},
{
label: 'Change Data Directory',
click: async (menuItem, browser) => {
click: (menuItem, browser) => {
if (!browser) return
browser.webContents.send(IPCKey.ChangeDataDir)
},
Expand Down
54 changes: 30 additions & 24 deletions packages/main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,40 @@ export const initializeUpdater = async (
store: Store<MyStore>,
window: BrowserWindow,
) => {
const lastCheck = store.get('lastUpdateCheck')
const neverCheckUpdate = store.get('neverCheckUpdate')
const checked = lastCheck + 24 * 60 * 60 * 1000 >= new Date().getTime()
if (neverCheckUpdate || checked) return
// Make all of the below listeners only apply in here
autoUpdater.removeAllListeners()

const res = await autoUpdater.checkForUpdates()
store.set('lastUpdateCheck', new Date().getTime())
if (!res) return
autoUpdater.on('update-available', async (info) => {
const answer = await dialog.showMessageBox(window, {
title: 'Update Available',
message: getUpdateAvailableMsg(info),
type: 'question',
buttons: ['Tomorrow', 'Never', 'Update'],
})

// close = 0, Tomorrow = 0, Never = 1, Update = 2
switch (answer.response) {
case 2:
await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
break

const answer = await dialog.showMessageBox(window, {
title: 'Update Available',
message: getUpdateAvailableMsg(res.updateInfo),
type: 'question',
buttons: ['Later', 'Never', 'Now'],
case 1:
store.set('neverCheckUpdate', true)
break

default:
break
}
})

// close = 0, Later = 0, Never = 1, Now = 2
switch (answer.response) {
case 2:
await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
break
autoUpdater.autoDownload = false

case 1:
store.set('neverCheckUpdate', true)
break
const lastCheck = store.get('lastUpdateCheck')
const neverCheckUpdate = store.get('neverCheckUpdate')
const checked = lastCheck + 24 * 60 * 60 * 1000 >= new Date().getTime()
if (neverCheckUpdate || checked) return

default:
break
}
await autoUpdater.checkForUpdates()
store.set('lastUpdateCheck', new Date().getTime())
}
2 changes: 1 addition & 1 deletion packages/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const sanitizeSchedule = (schedule: Schedule): Schedule => {

export const getUpdateAvailableMsg = (
updateInfo: UpdateInfo,
additional?: string,
additional: string = '',
) =>
`Update available: ${updateInfo.version}. Update now? It will download in the background. When it's finished downloading, it will forcefully (sorry) close the app and install. ` +
additional

0 comments on commit 19f9240

Please sign in to comment.