Skip to content

Commit

Permalink
Fix updater
Browse files Browse the repository at this point in the history
  • Loading branch information
SnekNOTSnake committed Dec 2, 2022
1 parent 6551f9d commit 3a6ba87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 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.1",
"version": "1.4.2",
"scripts": {
"start": "concurrently \"yarn start:renderer\" \"yarn start:main\" --kill-others",
"start:main": "tsc && electron .",
Expand Down
4 changes: 2 additions & 2 deletions packages/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const store = new Store<MyStore>({
})

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) {
Expand Down
21 changes: 17 additions & 4 deletions packages/main/ipcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IpcMainInvokeEvent,
shell,
Notification,
BrowserWindow,
} from 'electron'
import { nanoid } from 'nanoid'
import { autoUpdater } from 'electron-updater'
Expand Down Expand Up @@ -173,7 +174,7 @@ export class Events {
}

onChangeDataDir = async (): Promise<MyStore> => {
const res = await dialog.showOpenDialog({
const res = await this.dialog.showOpenDialog({
properties: ['openDirectory'],
})

Expand Down Expand Up @@ -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()
Expand Down
25 changes: 16 additions & 9 deletions packages/main/updater.ts
Original file line number Diff line number Diff line change
@@ -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<MyStore>) => {
export const initializeUpdater = async (
store: Store<MyStore>,
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

Expand Down

0 comments on commit 3a6ba87

Please sign in to comment.