Skip to content

Commit

Permalink
feat: Add API for downloading and installing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Nov 28, 2019
1 parent cd8e5d5 commit b8d24ca
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
14 changes: 13 additions & 1 deletion packages/neuron-ui/src/components/GeneralSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'
import { Stack, PrimaryButton, Spinner, Text, ProgressIndicator } from 'office-ui-fabric-react'
import { StateWithDispatch } from 'states/stateProvider/reducer'
import { addPopup } from 'states/stateProvider/actionCreators'
import { checkForUpdates, clearCellCache } from 'services/remote'
import { checkForUpdates, downloadUpdate, installUpdate, clearCellCache } from 'services/remote'

const UpdateDownloadStatus = ({
progress = 0,
Expand All @@ -14,13 +14,19 @@ const UpdateDownloadStatus = ({
const downloaded = progress >= 1

if (available) {
const download = () => {
downloadUpdate()
}

return (
<Stack>
<Text as="p" variant="medium">
{t('updates.updates-found-do-you-want-to-update', { version: newVersion })}
</Text>
<Stack horizontal horizontalAlign="start">
<PrimaryButton
onClick={download}
disabled={available}
styles={{
root: {
minWidth: 180,
Expand All @@ -35,13 +41,19 @@ const UpdateDownloadStatus = ({
}

if (downloaded) {
const quitAndInstall = () => {
installUpdate()
}

return (
<Stack>
<Text as="p" variant="medium">
{t('updates.updates-downloaded-about-to-quit-and-install')}
</Text>
<Stack horizontal horizontalAlign="start">
<PrimaryButton
onClick={quitAndInstall}
disabled={!downloaded}
styles={{
root: {
minWidth: 180,
Expand Down
2 changes: 0 additions & 2 deletions packages/neuron-ui/src/services/remote/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ export const getNeuronWalletState = apiMethodWrapper<void>(api => () => api.load
export const handleViewError = apiMethodWrapper<string>(api => errorMessage => api.handleViewError(errorMessage))
export const contextMenu = apiMethodWrapper<{ type: string; id: string }>(api => params => api.contextMenu(params))

export const checkForUpdates = apiMethodWrapper<void>(api => () => api.checkForUpdates())
export const clearCellCache = apiMethodWrapper<void>(api => () => api.clearCellCache())

export default {
getNeuronWalletState,
handleViewError,
contextMenu,
checkForUpdates,
clearCellCache,
}
1 change: 1 addition & 0 deletions packages/neuron-ui/src/services/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './app'
export * from './wallets'
export * from './networks'
export * from './transactions'
export * from './updater'

const REMOTE_MODULE_NOT_FOUND =
'The remote module is not found, please make sure the UI is running inside the Electron App'
Expand Down
11 changes: 11 additions & 0 deletions packages/neuron-ui/src/services/remote/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { apiMethodWrapper } from './apiMethodWrapper'

export const checkForUpdates = apiMethodWrapper<void>(api => () => api.checkForUpdates())
export const downloadUpdate = apiMethodWrapper<void>(api => () => api.downloadUpdate())
export const installUpdate = apiMethodWrapper<void>(api => () => api.quitAndInstall())

export default {
checkForUpdates,
downloadUpdate,
installUpdate,
}
12 changes: 11 additions & 1 deletion packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,23 @@ export default class ApiController {
return DaoController.getDaoCells(params)
}

// settings
// Settings

@MapApiResponse
public static async checkForUpdates() {
return new UpdateController().checkUpdates()
}

@MapApiResponse
public static async downloadUpdate() {
return new UpdateController(false).downloadUpdate()
}

@MapApiResponse
public static async quitAndInstall() {
return new UpdateController(false).quitAndInstall()
}

@MapApiResponse
public static async clearCellCache() {
await SyncController.stopSyncing()
Expand Down
24 changes: 11 additions & 13 deletions packages/neuron-wallet/src/controllers/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import AppUpdaterSubject from 'models/subjects/app-updater'
export default class UpdateController {
static isChecking = false // One instance is already running and checking

constructor() {
constructor(check: boolean = true) {
autoUpdater.autoDownload = false

if (!UpdateController.isChecking) {
if (check && !UpdateController.isChecking) {
this.bindEvents()
}
}
Expand All @@ -26,7 +26,15 @@ export default class UpdateController {
})
}

bindEvents() {
public quitAndInstall() {
autoUpdater.quitAndInstall()
}

public downloadUpdate() {
autoUpdater.downloadUpdate()
}

private bindEvents() {
autoUpdater.removeAllListeners()

autoUpdater.on('error', error => {
Expand All @@ -53,16 +61,6 @@ export default class UpdateController {
})

autoUpdater.on('update-downloaded', () => {
dialog
.showMessageBox({
type: 'info',
message: i18n.t('updater.updates-downloaded-about-to-quit-and-install'),
buttons: [i18n.t('common.ok')],
})
.then(() => {
setImmediate(() => autoUpdater.quitAndInstall())
})

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

0 comments on commit b8d24ca

Please sign in to comment.