Skip to content

Commit

Permalink
refactor(GUI): turn the update notifier modal into a native dialog
Browse files Browse the repository at this point in the history
Electron v1.6.1 introduced checkbox support to the native message
dialog, giving us everything that was needed to implement the update
notifier modal using a native dialog.

This change allows us to get rid of a lot code.

See: electron/electron#8590
Change-Type: patch
Changelog-Entry: Turn the update notifier modal into a native dialog.
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
  • Loading branch information
Juan Cruz Viotti committed May 7, 2017
1 parent 1cf4cdc commit df7ef95
Show file tree
Hide file tree
Showing 15 changed files with 606 additions and 395 deletions.
1 change: 1 addition & 0 deletions dictionary
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
boolen->boolean
aknowledge->acknowledge
seleted->selected
reming->remind
9 changes: 5 additions & 4 deletions lib/gui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const app = angular.module('Etcher', [

// Components
require('./components/svg-icon/svg-icon'),
require('./components/update-notifier/update-notifier'),
require('./components/warning-modal/warning-modal'),
require('./components/update-notifier'),

// Pages
require('./pages/main/main'),
Expand Down Expand Up @@ -94,13 +94,14 @@ app.run((AnalyticsService, ErrorService, UpdateNotifierService, SelectionStateMo
const currentVersion = packageJSON.version;
const currentReleaseType = release.getReleaseType(currentVersion);
const shouldCheckForUpdates = UpdateNotifierService.shouldCheckForUpdates({
ignoreSleepUpdateCheck: currentReleaseType !== release.RELEASE_TYPE.PRODUCTION
releaseType: currentReleaseType,
lastSleptUpdateNotifier: settings.get('lastSleptUpdateNotifier'),
lastSleptUpdateNotifierVersion: settings.get('lastSleptUpdateNotifierVersion') || packageJSON.version
});

if (_.some([
!shouldCheckForUpdates,
process.env.ETCHER_DISABLE_UPDATES,
currentReleaseType === release.RELEASE_TYPE.UNKNOWN
process.env.ETCHER_DISABLE_UPDATES
])) {
AnalyticsService.logEvent('Not checking for updates', {
shouldCheckForUpdates,
Expand Down
160 changes: 160 additions & 0 deletions lib/gui/components/update-notifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2016 resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* @module Etcher.Components.UpdateNotifier
*/

const angular = require('angular');
const electron = require('electron');
const Bluebird = require('bluebird');
const _ = require('lodash');
const settings = require('../models/settings');
const units = require('../../shared/units');
const release = require('../../shared/release');
const packageJSON = require('../../../package.json');

const MODULE_NAME = 'Etcher.Components.UpdateNotifier';
const UpdateNotifier = angular.module(MODULE_NAME, [
require('../os/open-external/open-external'),
require('../modules/analytics')
]);

UpdateNotifier.service('UpdateNotifierService', function(AnalyticsService, OSOpenExternalService) {

/**
* @summary The number of days the update notifier can be put to sleep
* @constant
* @private
* @type {Number}
*/
this.UPDATE_NOTIFIER_SLEEP_DAYS = packageJSON.updates.sleepDays;

/**
* @summary The current Electron browser window
* @constant
* @private
* @type {Object}
*/
const currentWindow = electron.remote.getCurrentWindow();

/**
* @summary Determine if it's time to check for updates
* @function
* @public
*
* @param {Object} options - options
* @param {Number} [options.lastSleptUpdateNotifier] - last slept update notifier time
* @param {String} [options.lastSleptUpdateNotifierVersion] - last slept update notifier version
* @param {String} options.releaseType - release type
* @returns {Boolean} should check for updates
*
* @example
* if (UpdateNotifierService.shouldCheckForUpdates({
* lastSleptUpdateNotifier: Date.now(),
* lastSleptUpdateNotifierVersion: '1.0.0',
* releaseType: release.RELEASE_TYPE.PRODUCTION
* })) {
* console.log('We should check for updates!');
* }
*/
this.shouldCheckForUpdates = (options) => {
if (_.some([
!options.lastSleptUpdateNotifier,
options.releaseType !== release.RELEASE_TYPE.PRODUCTION,
packageJSON.version !== options.lastSleptUpdateNotifierVersion
])) {
return true;
}

return Date.now() - options.lastSleptUpdateNotifier > units.daysToMilliseconds(this.UPDATE_NOTIFIER_SLEEP_DAYS);
};

/**
* @summary Open the update notifier widget
* @function
* @public
*
* @param {String} version - version
* @param {Object} [options] - options
* @param {Boolean} [options.allowSleepUpdateCheck=true] - allow sleeping the update check
* @returns {Promise}
*
* @example
* UpdateNotifierService.notify('1.0.0-beta.16', {
* allowSleepUpdateCheck: true
* });
*/
this.notify = (version, options = {}) => {
const BUTTONS = [
'Download',
'Skip'
];

const BUTTON_CONFIRMATION_INDEX = _.indexOf(BUTTONS, _.first(BUTTONS));
const BUTTON_REJECTION_INDEX = _.indexOf(BUTTONS, _.last(BUTTONS));

const dialogOptions = {
type: 'info',
buttons: BUTTONS,
defaultId: BUTTON_CONFIRMATION_INDEX,
cancelId: BUTTON_REJECTION_INDEX,
title: 'New Update Available!',
message: `Etcher ${version} is available for download`
};

if (_.get(options, [ 'allowSleepUpdateCheck' ], true)) {
_.merge(dialogOptions, {
checkboxLabel: `Remind me again in ${this.UPDATE_NOTIFIER_SLEEP_DAYS} days`,
checkboxChecked: false
});
}

return new Bluebird((resolve) => {
electron.remote.dialog.showMessageBox(currentWindow, dialogOptions, (response, checkboxChecked) => {
return resolve({
agreed: response === BUTTON_CONFIRMATION_INDEX,
sleepUpdateCheck: checkboxChecked || false
});
});
}).then((results) => {

// Only update the last slept update timestamp if the
// user ticked the "Remind me again in ..." checkbox,
// but didn't agree.
if (results.sleepUpdateCheck && !results.agreed) {
settings.set('lastSleptUpdateNotifier', Date.now());
settings.set('lastSleptUpdateNotifierVersion', packageJSON.version);
}

AnalyticsService.logEvent('Close update modal', {
sleepUpdateCheck: results.sleepUpdateCheck,
notifyVersion: version,
currentVersion: packageJSON.version,
agreed: results.agreed
});

if (results.agreed) {
OSOpenExternalService.open('https://etcher.io?ref=etcher_update');
}
});
};

});

module.exports = MODULE_NAME;
69 changes: 0 additions & 69 deletions lib/gui/components/update-notifier/controllers/update-notifier.js

This file was deleted.

89 changes: 0 additions & 89 deletions lib/gui/components/update-notifier/services/update-notifier.js

This file was deleted.

22 changes: 0 additions & 22 deletions lib/gui/components/update-notifier/styles/_update-notifier.scss

This file was deleted.

Loading

0 comments on commit df7ef95

Please sign in to comment.