Skip to content

Commit

Permalink
feat: Trigger check updates menu item enabling/disabling
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Nov 27, 2019
1 parent 423109d commit cd8e5d5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
13 changes: 7 additions & 6 deletions packages/neuron-wallet/src/controllers/app/menu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, shell, BrowserWindow, dialog, MenuItemConstructorOptions, clipboard, Menu, MenuItem, MessageBoxOptions, MessageBoxReturnValue } from 'electron'
import { app, shell, BrowserWindow, dialog, MenuItemConstructorOptions, clipboard, Menu, MessageBoxOptions, MessageBoxReturnValue } from 'electron'
import { bech32Address, AddressPrefix, AddressType } from '@nervosnetwork/ckb-sdk-utils'
import i18n from 'utils/i18n'
import env from 'env'
Expand Down Expand Up @@ -77,10 +77,10 @@ const updateApplicationMenu = (mainWindow: BrowserWindow | null) => {
click: () => { showAbout() },
},
{
enabled: isMainWindow,
label: i18n.t('application-menu.neuron.check-updates'),
click: (menuItem: MenuItem) => {
new UpdateController().checkUpdates(menuItem)
enabled: isMainWindow && !UpdateController.isChecking,
click: () => {
new UpdateController().checkUpdates()
navTo(URL.Preference)
}
},
Expand Down Expand Up @@ -228,8 +228,9 @@ const updateApplicationMenu = (mainWindow: BrowserWindow | null) => {
})
helpSubmenu.push({
label: i18n.t('application-menu.neuron.check-updates'),
click: (menuItem: MenuItem) => {
new UpdateController().checkUpdates(menuItem)
enabled: isMainWindow && !UpdateController.isChecking,
click: () => {
new UpdateController().checkUpdates()
navTo(URL.Preference)
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/controllers/app/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const subscribe = (dispatcher: AppResponder) => {
})

AppUpdaterSubject.subscribe(params => {
dispatcher.updateMenu()
dispatcher.sendMessage('app-updater-updated', params)
})
}
64 changes: 32 additions & 32 deletions packages/neuron-wallet/src/controllers/update.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
import { dialog } from 'electron'
import { autoUpdater, UpdateInfo } from 'electron-updater'
import i18n from 'utils/i18n'
import AppUpdaterSubject from 'models/subjects/app-updater'

export default class UpdateController {
sender: { enabled: boolean } | null
static isChecking = false // One instance is already running and checking

constructor() {
autoUpdater.autoDownload = false
this.sender = null

this.bindEvents()
}

// TODO: refactor: do not require sender
public checkUpdates(sender: { enabled: boolean } | null = null) {
this.sender = sender
if (this.sender) {
this.sender.enabled = false
if (!UpdateController.isChecking) {
this.bindEvents()
}
}

public checkUpdates() {
UpdateController.isChecking = true
autoUpdater.checkForUpdates()

AppUpdaterSubject.next({
checking: true,
downloadProgress: -1,
version: '',
releaseNotes: ''
})
}

bindEvents() {
autoUpdater.removeAllListeners()

autoUpdater.on('error', error => {
dialog.showErrorBox('Error', error == null ? 'unknown' : (error.stack || error).toString())
this.enableSender()

UpdateController.isChecking = false
this.notify()
})

autoUpdater.on('update-available', (info: UpdateInfo) => {
const { version } = info
dialog
.showMessageBox({
type: 'info',
title: version,
message: i18n.t('updater.updates-found-do-you-want-to-update', { version }),
buttons: [i18n.t('updater.update-now'), i18n.t('common.no')],
})
.then(returnValue => {
if (returnValue.response === 0) {
autoUpdater.downloadUpdate()
} else {
this.enableSender()
}
})
UpdateController.isChecking = false
this.notify(-1, info.version, 'todo')
})

autoUpdater.on('update-not-available', () => {
Expand All @@ -54,7 +47,9 @@ export default class UpdateController {
message: i18n.t('updater.update-not-available'),
buttons: [i18n.t('common.ok')],
})
this.enableSender()

UpdateController.isChecking = false
this.notify()
})

autoUpdater.on('update-downloaded', () => {
Expand All @@ -67,13 +62,18 @@ export default class UpdateController {
.then(() => {
setImmediate(() => autoUpdater.quitAndInstall())
})

UpdateController.isChecking = false
this.notify(1, 'toto', '')
})
}

enableSender() {
if (this.sender) {
this.sender.enabled = true
}
this.sender = null
private notify(downloadProgress: number = -1, version = '', releaseNotes = '') {
AppUpdaterSubject.next({
checking: UpdateController.isChecking,
downloadProgress,
version,
releaseNotes
})
}
}

0 comments on commit cd8e5d5

Please sign in to comment.