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 3, 2017
1 parent 1cf4cdc commit cff6196
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 354 deletions.
8 changes: 4 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,13 @@ 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')
});

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
148 changes: 148 additions & 0 deletions lib/gui/components/update-notifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* 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 Determine if it's time to check for updates
* @function
* @public
*
* @param {Object} [options] - options
* @param {Number} [options.lastSleptUpdateNotifier] - last slept update notify time
* @param {String} options.releaseType - release type
* @returns {Boolean} should check for updates
*
* @example
* if (UpdateNotifierService.shouldCheckForUpdates({
* lastSleptUpdateNotifier: Date.now(),
* releaseType: release.RELEASE_TYPE.PRODUCTION
* })) {
* console.log('We should check for updates!');
* }
*/
this.shouldCheckForUpdates = (options = {}) => {
if (_.some([
!_.get(options, [ 'lastSleptUpdateNotifier' ], false),
options.releaseType !== release.RELEASE_TYPE.PRODUCTION
])) {
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) => {
const currentWindow = electron.remote.getCurrentWindow();
electron.remote.dialog.showMessageBox(currentWindow, dialogOptions, (response, checkboxChecked) => {
return resolve({
agreed: response === BUTTON_CONFIRMATION_INDEX,
sleepUpdateCheck: checkboxChecked || false
});
});
}).then((results) => {

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

AnalyticsService.logEvent('Close update modal', {
sleepUpdateCheck: results.sleepUpdateCheck,
notifyVersion: 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.

This file was deleted.

Loading

0 comments on commit cff6196

Please sign in to comment.