refactor preferences

This commit is contained in:
Matias Alejo Garcia 2016-06-06 18:26:45 -03:00
commit 1e2555dad9
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
15 changed files with 222 additions and 200 deletions

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('buyCoinbaseController',
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, bwsError, addressService, walletService) {
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, bwsError, addressService) {
window.ignoreMobilePause = true;
var self = this;
@ -72,7 +72,7 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController',
$scope.selectWallet = function(walletId, walletName) {
var client = profileService.getClient(walletId);
walletService.isReady(client, function(err) {
profileService.isReady(client, function(err) {
if (err) {
self.error = {errors: [{ message: err }]};
$modalInstance.dismiss('cancel');

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('buyGlideraController',
function($scope, $timeout, $modal, profileService, addressService, glideraService, bwsError, lodash, animationService, walletService) {
function($scope, $timeout, $modal, profileService, addressService, glideraService, bwsError, lodash, animationService) {
var self = this;
this.show2faCodeInput = null;
@ -51,7 +51,7 @@ angular.module('copayApp.controllers').controller('buyGlideraController',
$scope.selectWallet = function(walletId, walletName) {
var client = profileService.getClient(walletId);
walletService.isReady(client, function(err) {
profileService.isReady(client, function(err) {
if (err) {
self.error = err;
$modalInstance.dismiss('cancel');

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, $ionicSideMenuDelegate, $ionicScrollDelegate, $ionicPopup, latestReleaseService, feeService, bwcService, pushNotificationsService, lodash, go, profileService, configService, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, addonManager, bwsError, txFormatService, uxLanguage, glideraService, coinbaseService, platformInfo, addressbookService, walletService) {
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, $ionicSideMenuDelegate, $ionicScrollDelegate, $ionicPopup, latestReleaseService, feeService, bwcService, pushNotificationsService, lodash, go, profileService, configService, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, addonManager, bwsError, txFormatService, uxLanguage, glideraService, coinbaseService, platformInfo, addressbookService) {
var self = this;
var SOFT_CONFIRMATION_LIMIT = 12;
var errors = bwcService.getErrors();
@ -184,7 +184,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}
}
walletService.isBackupNeeded(fc, function(needsBackup) {
profileService.needsBackup(fc, function(needsBackup) {
self.needsBackup = needsBackup;
self.openWallet(function() {
if (!self.isComplete) {
@ -268,50 +268,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
};
self._updateRemotePreferencesFor = function(clients, prefs, cb) {
var client = clients.shift();
if (!client)
return cb();
$log.debug('Saving remote preferences', client.credentials.walletName, prefs);
client.savePreferences(prefs, function(err) {
// we ignore errors here
if (err) $log.warn(err);
self._updateRemotePreferencesFor(clients, prefs, cb);
});
};
// TODO: should not be in index.js
self.updateRemotePreferences = function(opts, cb) {
var clients;
var prefs = opts.preferences || {};
// Update this JIC.
var config = configService.getSync().wallet.settings;
//prefs.email (may come from arguments)
prefs.language = self.defaultLanguageIsoCode;
prefs.unit = config.unitCode;
if (opts.client) {
clients = [opts.client];
} else {
clients = lodash.values(profileService.walletClients);
};
self._updateRemotePreferencesFor(clients, prefs, function(err) {
if (err) return cb(err);
lodash.each(clients, function(c) {
c.preferences = lodash.assign(prefs, c.preferences);
});
return cb();
});
};
var _walletStatusHash = function(walletStatus) {
var bal;
@ -323,6 +279,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
return bal;
};
// TODO move this to wallet service
self.updateAll = function(opts, initStatusHash, tries) {
$scope.$broadcast('scroll.refreshComplete');
tries = tries || 0;
@ -1091,14 +1048,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
};
self.setUxLanguage = function(cb) {
uxLanguage.update(function(lang) {
var userLang = lang;
self.defaultLanguageIsoCode = userLang;
self.defaultLanguageName = uxLanguage.getName(userLang);
if (cb) return cb();
});
};
self.initGlidera = function(accessToken) {
self.glideraEnabled = configService.getSync().glidera.enabled;
@ -1509,24 +1458,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateAll();
});
$rootScope.$on('Local/ProfileBound', function() {
storageService.getRemotePrefsStoredFlag(function(err, val) {
if (err || val) return;
self.updateRemotePreferences({}, function() {
$log.debug('Remote preferences saved');
storageService.setRemotePrefsStoredFlag(function() {});
});
});
});
$rootScope.$on('Local/LanguageSettingUpdated', function() {
self.setUxLanguage(function() {
self.updateRemotePreferences({}, function() {
$log.debug('Remote preferences saved')
});
});
});
$rootScope.$on('Local/GlideraUpdated', function(event, accessToken) {
self.initGlidera(accessToken);
});
@ -1553,18 +1484,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateAll({
triggerTxUpdate: true,
});
self.updateRemotePreferences({}, function() {
$log.debug('Remote preferences saved')
});
});
$rootScope.$on('Local/EmailSettingUpdated', function(event, client, email, cb) {
self.updateRemotePreferences({
client: client,
preferences: {
email: email || null
},
}, cb);
});
$rootScope.$on('Local/WalletCompleted', function(event, walletId) {
@ -1576,17 +1495,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}
});
lodash.each(['Local/ProfileCreated', 'Local/WalletListUpdated'], function(eventName) {
$rootScope.$on(eventName, function(event, client) {
self.updateRemotePreferences({
client: client
}, function() {
$log.debug('Remote preferences saved');
});
});
});
self.debouncedUpdate = lodash.throttle(function() {
self.updateAll({
quiet: true
@ -1740,14 +1648,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
$rootScope.$on('Local/NoWallets', function(event) {
$log.debug('Event: NoWallets');
$timeout(function() {
self.hasProfile = true;
self.noFocusedWallet = true;
self.isComplete = null;
self.walletName = null;
self.setUxLanguage();
uxLanguage.update();
profileService.isDisclaimerAccepted(function(v) {
if (v) {
go.path('import');
@ -1757,7 +1664,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
$rootScope.$on('Local/NewFocusedWallet', function() {
self.setUxLanguage();
uxLanguage.update();
self.setFocusedWallet();
self.updateHistory();
storageService.getCleanAndScanAddresses(function(err, walletId) {
@ -1866,6 +1773,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
$rootScope.$on('Local/EmailUpdated', function(event, email) {
self.preferences.email = email
});
lodash.each(['NewCopayer', 'CopayerUpdated'], function(eventName) {
$rootScope.$on(eventName, function() {
// Re try to open wallet (will triggers)

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('addressbookController', function($rootScope, $scope, $timeout, profileService, walletService, addressService, addressbookService) {
angular.module('copayApp.controllers').controller('addressbookController', function($rootScope, $scope, $timeout, profileService, addressService, addressbookService) {
var self = $scope.self;
var fc = profileService.focusedClient;
@ -118,7 +118,7 @@ angular.module('copayApp.controllers').controller('addressbookController', funct
var client = profileService.getClient(walletId);
$scope.errorSelectedWallet = {};
walletService.isReady(client, function(err) {
profileService.isReady(client, function(err) {
if (err) $scope.errorSelectedWallet[walletId] = err;
else {
$scope.gettingAddress = true;

View file

@ -3,15 +3,20 @@
angular.module('copayApp.controllers').controller('preferencesController',
function($scope, $rootScope, $timeout, $log, configService, profileService, fingerprintService, walletService) {
var fc = profileService.focusedClient;
var self = this;
var fc;
var config = configService.getSync();
$scope.deleted = false;
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
$scope.deleted = true;
}
var disableFocusListener = $rootScope.$on('Local/NewFocusedWalletReady', function() {
self.init();
});
$scope.$on('$destroy', function() {
disableFocusListener();
});
this.init = function() {
fc = profileService.focusedClient;
if (fc) {
$scope.encryptEnabled = walletService.isEncrypted(fc);
this.externalSource = fc.getPrivKeyExternalSourceName() == 'ledger' ? "Ledger" : null;
@ -21,38 +26,69 @@ angular.module('copayApp.controllers').controller('preferencesController',
this.touchidAvailable = fingerprintService.isAvailable();
$scope.touchidEnabled = config.touchIdFor ? config.touchIdFor[fc.credentials.walletId] : null;
$scope.deleted = false;
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
$scope.deleted = true;
}
};
var handleEncryptedWallet = function(client, cb) {
var handleEncryptedWallet = function(cb) {
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
return cb(walletService.unlock(client, password));
return cb(walletService.unlock(fc, password));
});
};
$scope.encryptChange = function() {
var self = this;
if (!fc) return;
var val = $scope.encryptEnabled;
var setPrivateKeyEncryption = function(password, cb) {
$log.debug('Encrypting private key for', fc.credentials.walletName);
fc.setPrivateKeyEncryption(password);
fc.lock();
profileService.updateCredentials(fc.export(), function() {
$log.debug('Wallet encrypted');
return cb();
});
};
var disablePrivateKeyEncryption = function(cb) {
$log.debug('Disabling private key encryption for', fc.credentials.walletName);
try {
fc.disablePrivateKeyEncryption();
} catch (e) {
return cb(e);
}
profileService.updateCredentials(fc.export(), function() {
$log.debug('Wallet encryption disabled');
return cb();
});
};
if (val && !walletService.isEncrypted(fc)) {
$rootScope.$emit('Local/NeedsPassword', true, function(err, password) {
if (err || !password) {
$scope.encryptEnabled = false;
return;
}
profileService.setPrivateKeyEncryptionFC(password, function() {
setPrivateKeyEncryption(password, function() {
$rootScope.$emit('Local/NewEncryptionSetting');
$scope.encryptEnabled = true;
});
});
} else {
if (!val && walletService.isEncrypted(fc)) {
handleEncryptedWallet(fc, function(err) {
handleEncryptedWallet(function(err) {
if (err) {
$scope.encryptEnabled = true;
return;
}
profileService.disablePrivateKeyEncryptionFC(function(err) {
disablePrivateKeyEncryption(function(err) {
$rootScope.$emit('Local/NewEncryptionSetting');
if (err) {
$scope.encryptEnabled = true;

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesAltCurrencyController',
function($scope, $timeout, $log, configService, rateService, lodash, go) {
function($scope, $timeout, $log, configService, rateService, lodash, go, profileService, walletService) {
this.hideAdv = true;
this.hidePriv = true;
this.hideSecret = true;
@ -51,6 +51,9 @@ angular.module('copayApp.controllers').controller('preferencesAltCurrencyControl
if (err) $log.warn(err);
go.preferencesGlobal();
$scope.$emit('Local/UnitSettingUpdated');
walletService.updateRemotePreferences(profileService.walletClients, {}, function() {
$log.debug('Remote preferences saved')
});
$timeout(function() {
$scope.$apply();
}, 100);

View file

@ -13,7 +13,7 @@ angular.module('copayApp.controllers').controller('preferencesDeleteWordsControl
confirmDialog.show(msg, function(ok) {
if (ok) {
fc.clearMnemonic();
profileService.updateCredentialsFC(function() {
profileService.updateCredentials(fc.export(), function() {
notification.success(successMsg);
go.walletHome();
});

View file

@ -1,14 +1,22 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesEmailController',
function($scope, go, profileService, gettext, $log) {
function($rootScope, go, profileService, gettext, $log, walletService) {
this.save = function(form) {
var self = this;
this.error = null;
var fc = profileService.focusedClient;
this.saving = true;
$scope.$emit('Local/EmailSettingUpdated', fc,self.email, function() {
var email = self.email || '';
walletService.updateRemotePreferences(fc, {
email: email,
}, function(err) {
if (!err)
$rootScope.$emit('Local/EmailUpdated', email);
self.saving = false;
go.path('preferences');
});

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesLanguageController',
function($scope, $log, $timeout, configService, uxLanguage, go) {
function($scope, $log, $timeout, configService, profileService, uxLanguage, walletService, go) {
this.availableLanguages = uxLanguage.getLanguages();
this.currentLanguage = uxLanguage.getCurrentLanguage();
@ -19,10 +19,18 @@ angular.module('copayApp.controllers').controller('preferencesLanguageController
configService.set(opts, function(err) {
if (err) $log.warn(err);
go.preferencesGlobal();
$scope.$emit('Local/LanguageSettingUpdated');
$timeout(function() {
$scope.$apply();
}, 100);
uxLanguage.update(function() {
$timeout(function() {
$scope.$apply();
}, 100);
walletService.updateRemotePreferences(profileService.getClients(), {},
function() {
$log.debug('Remote preferences saved')
});
});
});
};
});

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesUnitController',
function($scope, $timeout, $log, configService, go) {
function($scope, $timeout, $log, configService, go, walletService, profileService) {
var config = configService.getSync();
this.unitName = config.wallet.settings.unitName;
this.unitOpts = [
@ -54,6 +54,9 @@ angular.module('copayApp.controllers').controller('preferencesUnitController',
if (err) $log.warn(err);
go.preferencesGlobal();
$scope.$emit('Local/UnitSettingUpdated');
walletService.updateRemotePreferences(profileService.getClients(), {}, function() {
$log.debug('Remote preferences saved')
});
$timeout(function() {
$scope.$apply();
}, 100);