Skip to content
Permalink
Browse files

feat(wallet): ability to toggle autoupdater

  • Loading branch information...
mrfelton committed May 14, 2019
1 parent 0734aa8 commit 180c031d8c820becc53d4558630520567deb636f
@@ -150,18 +150,21 @@ app.on('ready', async () => {
// Get the users preference so that we can:
// - set the background colour of the window to avoid unwanted flicker.
// - Initialise the Language menu with the users locale selected by default.
// - Enable autoupdates based on user preferences.
let autoupdate = {}
let theme = {}
let locale
let locale = null

if (!process.env.DISABLE_INIT) {
try {
const settings = await fetchSettings()
const currentConfig = getSetting(settings, 'config')
locale = currentConfig.locale || config.locale
const currentConfig = getSetting(settings, 'config') || {}
const themeKey = currentConfig.theme || config.theme
locale = currentConfig.locale || config.locale
autoupdate = currentConfig.autoupdate || config.autoupdate
theme = themes[themeKey]
} catch (e) {
mainLog.warn('Unable to determine user locale and theme', e)
mainLog.warn('Unable to determine user settings: %s', e.message)
}
}

@@ -195,8 +198,7 @@ app.on('ready', async () => {
}

// Initialise the updater.
updater = new ZapUpdater(mainWindow)
updater.init()
updater = new ZapUpdater(mainWindow, autoupdate)

// Initialise the application.
zap = new ZapController(mainWindow)
@@ -1,29 +1,26 @@
import { dialog } from 'electron'
import { dialog, ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
import isDev from 'electron-is-dev'
import config from 'config'
import { updaterLog } from '@zap/utils/log'
import delay from '@zap/utils/delay'

autoUpdater.logger = updaterLog
autoUpdater.channel = config.autoupdate.channel
autoUpdater.allowDowngrade = false
import { updaterLog } from '@zap/utils/log'

/**
* @class ZapUpdater
*
* The ZapUpdater class manages the electron auto update process.
*/
class ZapUpdater {
constructor(mainWindow) {
constructor(mainWindow, options = {}) {
const settings = { ...config.autopilot, options }

this.mainWindow = mainWindow
}
this.isActive = false
this.timer = null

init() {
// Do not run the updater if we are running in dev mode or if autoupdates are disabled.
if (isDev || !config.autoupdate.active) {
return
}
autoUpdater.logger = updaterLog
autoUpdater.channel = settings.channel
autoUpdater.allowDowngrade = false

autoUpdater.on('update-downloaded', () => {
const opt = {
@@ -41,18 +38,44 @@ class ZapUpdater {
})
})

this.initAutoUpdate()
// Enable updates if needed.
if (settings.active && !isDev) {
this.enableAutoUpdates()
}

ipcMain.on('configureAutoUpdater', (event, settings) => {
settings.active ? this.enableAutoUpdates() : this.disableAutoUpdates()
})
}

async initAutoUpdate() {
/**
* Enable automatic updates.
*/
enableAutoUpdates() {
if (this.isActive || isDev) {
return
}
try {
await autoUpdater.checkForUpdates()
updaterLog.info('Automatic Updates enabled')
this.isActive = true
autoUpdater.checkForUpdates()
const oneHour = 60 * 60 * 1000
setInterval(() => autoUpdater.checkForUpdates(), oneHour)
this.timer = setInterval(() => autoUpdater.checkForUpdates(), oneHour)
} catch (error) {
updaterLog.warn('Cannot check for updates', error.message)
}
}

/**
* Disable automatic updates.
*/
disableAutoUpdates() {
if (!this.isActive || isDev) {
return
}
clearInterval(this.timer)
updaterLog.info('Automatic Updates disabled')
}
}

export default ZapUpdater
@@ -86,6 +86,7 @@ const FormButtons = props => (

const Settings = ({
intl,
configureAutoUpdater,
currentConfig,
onSubmit,
setLocale,
@@ -100,6 +101,9 @@ const Settings = ({
if (values.locale) {
await setLocale(values.locale)
}
if (values.autoupdate) {
configureAutoUpdater(values.autoupdate)
}

// Show a notification.
const message = intl.formatMessage({ ...messages.submit_success })
@@ -224,6 +228,7 @@ const Settings = ({
}

Settings.propTypes = {
configureAutoUpdater: PropTypes.func.isRequired,
currentConfig: PropTypes.object.isRequired,
intl: intlShape.isRequired,
onSubmit: PropTypes.func,
@@ -3,12 +3,14 @@ import Settings from 'components/Settings'
import { saveConfigOverrides, settingsSelectors } from 'reducers/settings'
import { showNotification } from 'reducers/notification'
import { setLocale } from 'reducers/locale'
import { configureAutoUpdater } from 'reducers/autoupdate'

const mapStateToProps = state => ({
currentConfig: settingsSelectors.currentConfig(state),
})

const mapDispatchToProps = {
configureAutoUpdater,
saveConfigOverrides,
showNotification,
setLocale,
@@ -0,0 +1,19 @@
import { send } from 'redux-electron-ipc'

// ------------------------------------
// Constants
// ------------------------------------

export const CONFIGURE_AUTOUPDATER = 'CONFIGURE_AUTOUPDATER'

// ------------------------------------
// Actions
// ------------------------------------

/**
* Tell the main process to reconfigure the autoupdater.
*/
export const configureAutoUpdater = settings => dispatch => {
dispatch({ type: CONFIGURE_AUTOUPDATER, settings })
dispatch(send('configureAutoUpdater', settings))
}

0 comments on commit 180c031

Please sign in to comment.
You can’t perform that action at this time.