save lang and unit in BWS

This commit is contained in:
Matias Alejo Garcia 2015-06-29 21:46:34 -03:00
commit c65b2d7c07
6 changed files with 140 additions and 74 deletions

View file

@ -1,7 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment) { angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment) {
var self = this; var self = this;
self.isCordova = isCordova; self.isCordova = isCordova;
self.onGoingProcess = {}; self.onGoingProcess = {};
@ -154,15 +153,50 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}; };
self.updatePreferences = function(cb) { 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) {
if (err) return cb(err);
self._updateRemotePreferencesFor(clients, prefs, cb);
});
};
self.updateRemotePreferences = function(opts, cb) {
var prefs = opts.preferences || {};
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
fc.getPreferences(function(err, preferences) {
//prefs.email (may come from arguments)
prefs.language = self.defaultLanguageIsoCode;
prefs.unit = self.unitName;
var clients = [];
if (opts.saveAll) {
clients = lodash.values(profileService.walletClients);
} else {
clients = [fc];
};
self._updateRemotePreferencesFor(clients, prefs, function(err) {
if (err) { if (err) {
self.handleError(err); self.handleError(err);
return cb(err); return cb(err);
} }
self.preferences = preferences;
return cb(err, preferences); fc.getPreferences(function(err, preferences) {
if (err) {
self.handleError(err);
return cb(err);
}
self.preferences = preferences;
return cb();
});
}); });
}; };
@ -211,12 +245,15 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$log.debug('Updating Status:', fc, tries); $log.debug('Updating Status:', fc, tries);
get(function(err, walletStatus) { get(function(err, walletStatus) {
var currentStatusHash = _walletStatusHash(walletStatus); var currentStatusHash = _walletStatusHash(walletStatus);
$log.debug('Status update. hash:' + currentStatusHash + ' Try:'+ tries); $log.debug('Status update. hash:' + currentStatusHash + ' Try:' + tries);
if (!err && opts.untilItChanges && initStatusHash == currentStatusHash && tries < 7) { if (!err && opts.untilItChanges && initStatusHash == currentStatusHash && tries < 7) {
return $timeout(function() { return $timeout(function() {
$log.debug('Retrying update... Try:' + tries) $log.debug('Retrying update... Try:' + tries)
return self.updateAll({walletStatus: null, untilItChanges: true}, initStatusHash, ++tries); return self.updateAll({
walletStatus: null,
untilItChanges: true
}, initStatusHash, ++tries);
}, 1400 * tries); }, 1400 * tries);
} }
if (!opts.quiet) if (!opts.quiet)
@ -340,7 +377,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r
return; return;
} }
$log.debug('Wallet Opened'); $log.debug('Wallet Opened');
self.updateAll(lodash.isObject(walletStatus) ? {walletStatus: walletStatus} : null); self.updateAll(lodash.isObject(walletStatus) ? {
walletStatus: walletStatus
} : null);
$rootScope.$apply(); $rootScope.$apply();
}); });
}); });
@ -466,8 +505,8 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.alternativeIsoCode = config.alternativeIsoCode; self.alternativeIsoCode = config.alternativeIsoCode;
// Check address // Check address
addressService.isUsed(self.walletId, balance.byAddress, function(err, used){ addressService.isUsed(self.walletId, balance.byAddress, function(err, used) {
if (used) { if (used) {
$log.debug('Address used. Creating new'); $log.debug('Address used. Creating new');
$rootScope.$emit('Local/NeedNewAddress'); $rootScope.$emit('Local/NeedNewAddress');
} }
@ -571,40 +610,27 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
}; };
self.setDefaultLanguage = function(setLang) { self.setUxLanguage = function() {
var userLang; var userLang = configService.getSync().wallet.settings.defaultLanguage;
if (!setLang) { if (!userLang) {
userLang = configService.getSync().wallet.settings.defaultLanguage; // Auto-detect browser language
if (!userLang) { var androidLang;
// Auto-detect browser language
var androidLang;
if (navigator && navigator.userAgent && (androidLang = navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) { if (navigator && navigator.userAgent && (androidLang = navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) {
userLang = androidLang[1]; userLang = androidLang[1];
} else { } else {
// works for iOS and Android 4.x // works for iOS and Android 4.x
userLang = navigator.userLanguage || navigator.language; userLang = navigator.userLanguage || navigator.language;
}
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
} }
if (userLang != gettextCatalog.getCurrentLanguage()) { userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
$log.debug('Setting default language: ' + userLang);
gettextCatalog.setCurrentLanguage(userLang);
amMoment.changeLocale(userLang);
}
} else {
configService.set({
wallet: {
settings: {
defaultLanguage: setLang
}
}
}, function() {
gettextCatalog.setCurrentLanguage(setLang);
amMoment.changeLocale(setLang);
});
} }
self.defaultLanguageIsoCode = setLang || userLang; if (userLang != gettextCatalog.getCurrentLanguage()) {
$log.debug('Setting default language: ' + userLang);
gettextCatalog.setCurrentLanguage(userLang);
amMoment.changeLocale(userLang);
}
self.defaultLanguageIsoCode = userLang;
self.defaultLanguageName = lodash.result(lodash.find(self.availableLanguages, { self.defaultLanguageName = lodash.result(lodash.find(self.availableLanguages, {
'isoCode': self.defaultLanguageIsoCode 'isoCode': self.defaultLanguageIsoCode
}), 'name'); }), 'name');
@ -625,14 +651,41 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
}); });
$rootScope.$on('Local/UnitSettingUpdated', function(event) {
self.updateAll(); $rootScope.$on('Local/NewFocusedWallet', function() {
self.updateTxHistory(); self.setUxLanguage();
}); });
$rootScope.$on('Local/LanguageSettingUpdated', function() {
self.setUxLanguage();
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved')
});
});
$rootScope.$on('Local/EmailUpdated', function(event, cb) { $rootScope.$on('Local/UnitSettingUpdated', function(event) {
self.updatePreferences(cb); // This need to be done first, to update unitName
// updateAll do it, but async, later.
var config = configService.getSync().wallet.settings;
self.unitName = config.unitName;
self.updateAll();
self.updateTxHistory();
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved')
});
});
$rootScope.$on('Local/EmailSettingUpdated', function(event, email, cb) {
self.updateRemotePreferences({
preferences: {
email: email
},
}, cb);
}); });
$rootScope.$on('Local/BWSUpdated', function(event) { $rootScope.$on('Local/BWSUpdated', function(event) {
@ -646,7 +699,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
self.debouncedUpdate = lodash.throttle(function() { self.debouncedUpdate = lodash.throttle(function() {
self.updateAll({quiet: true}); self.updateAll({
quiet: true
});
self.updateTxHistory(); self.updateTxHistory();
}, 4000, { }, 4000, {
leading: false, leading: false,
@ -708,10 +763,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
}); });
$rootScope.$on('Local/DefaultLanguage', function(event, setLang) {
self.setDefaultLanguage(setLang);
});
$rootScope.$on('NewIncomingTx', function() { $rootScope.$on('NewIncomingTx', function() {
self.updateBalance(); self.updateBalance();
$timeout(function() { $timeout(function() {
@ -720,14 +771,20 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
$rootScope.$on('NewOutgoingTx', function() { $rootScope.$on('NewOutgoingTx', function() {
self.updateAll({walletStatus: null, untilItChanges: true}); self.updateAll({
walletStatus: null,
untilItChanges: true
});
}); });
lodash.each(['NewTxProposal', 'TxProposalFinallyRejected', 'TxProposalRemoved', lodash.each(['NewTxProposal', 'TxProposalFinallyRejected', 'TxProposalRemoved',
'Local/NewTxProposal', 'Local/TxProposalAction', 'ScanFinished' 'Local/NewTxProposal', 'Local/TxProposalAction', 'ScanFinished'
], function(eventName) { ], function(eventName) {
$rootScope.$on(eventName, function(event, untilItChanges) { $rootScope.$on(eventName, function(event, untilItChanges) {
self.updateAll({walletStatus: null, untilItChanges: untilItChanges}); self.updateAll({
walletStatus: null,
untilItChanges: untilItChanges
});
$timeout(function() { $timeout(function() {
self.updateTxHistory(); self.updateTxHistory();
}, 3000); }, 3000);
@ -772,7 +829,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
storageService.removeCleanAndScanAddresses(function() {}); storageService.removeCleanAndScanAddresses(function() {});
} }
}); });
}); });
$rootScope.$on('Local/SetTab', function(event, tab, reset) { $rootScope.$on('Local/SetTab', function(event, tab, reset) {

View file

@ -8,20 +8,9 @@ angular.module('copayApp.controllers').controller('preferencesEmailController',
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
this.saving = true; this.saving = true;
fc.savePreferences({ $scope.$emit('Local/EmailSettingUpdated', self.email, function() {
email: this.email
}, function(err) {
self.saving = false; self.saving = false;
if (err) { go.path('preferences');
$log.warn(err);
$scope.$emit('Local/ClientError', err);
return;
}
$scope.$emit('Local/EmailUpdated', function(err){
go.path('preferences');
});
}); });
}; };
}); });

View file

@ -1,12 +1,24 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('preferencesLanguageController', angular.module('copayApp.controllers').controller('preferencesLanguageController',
function($scope, $timeout, go) { function($scope, $log, $timeout, configService, go) {
this.save = function(newLang) { this.save = function(newLang) {
$scope.$emit('Local/DefaultLanguage', newLang);
$timeout(function() { var opts = {
go.preferences(); wallet: {
}, 100); settings: {
defaultLanguage: newLang
}
}
};
configService.set(opts, function(err) {
if (err) $log.warn(err);
$scope.$emit('Local/LanguageSettingUpdated');
$timeout(function() {
go.preferences();
}, 100);
});
}; };
}); });

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('preferencesUnitController', angular.module('copayApp.controllers').controller('preferencesUnitController',
function($rootScope, $scope, configService, go) { function($rootScope, $scope, $log, configService, go) {
var config = configService.getSync(); var config = configService.getSync();
this.unitName = config.wallet.settings.unitName; this.unitName = config.wallet.settings.unitName;
this.unitOpts = [ this.unitOpts = [
@ -51,8 +51,9 @@ angular.module('copayApp.controllers').controller('preferencesUnitController',
this.unitName = newUnit.shortName; this.unitName = newUnit.shortName;
configService.set(opts, function(err) { configService.set(opts, function(err) {
if (err) console.log(err); if (err) $log.warn(err);
$scope.$emit('Local/UnitSettingUpdated'); $scope.$emit('Local/UnitSettingUpdated');
go.preferences();
}); });
}; };

View file

@ -125,7 +125,6 @@ angular.module('copayApp.services')
$log.debug('Preferences read'); $log.debug('Preferences read');
if (err) return cb(err); if (err) return cb(err);
root.applyConfig(); root.applyConfig();
$rootScope.$emit('Local/DefaultLanguage');
root.setWalletClients(); root.setWalletClients();
storageService.getFocusedWalletId(function(err, focusedWalletId) { storageService.getFocusedWalletId(function(err, focusedWalletId) {
if (err) return cb(err); if (err) return cb(err);

View file

@ -184,5 +184,13 @@ angular.module('copayApp.services')
storage.get('agreeDisclaimer', cb); storage.get('agreeDisclaimer', cb);
}; };
root.setRemotePreferencesStored = function(cb) {
storage.set('remotePrefStored', true, cb);
};
root.getRemovePreferencesStored = function(cb) {
storage.get('remotePrefStored', cb);
};
return root; return root;
}); });