From ebe415a3d6edc15e579a02eda922e7df789f3a48 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 24 Oct 2014 11:37:03 -0300 Subject: [PATCH 1/2] Simplified controllers with identityService --- js/controllers/createProfile.js | 27 +++------------------- js/controllers/home.js | 5 +++-- js/services/identity.js | 0 js/services/identityService.js | 40 +++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 26 deletions(-) delete mode 100644 js/services/identity.js create mode 100644 js/services/identityService.js diff --git a/js/controllers/createProfile.js b/js/controllers/createProfile.js index 20d9da77e..485de574c 100644 --- a/js/controllers/createProfile.js +++ b/js/controllers/createProfile.js @@ -1,23 +1,10 @@ 'use strict'; -angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager) { +angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) { controllerUtils.redirIfLogged(); $scope.retreiving = true; - - copay.Identity.anyProfile({ - pluginManager: pluginManager, - }, function(anyProfile) { - copay.Identity.anyWallet({ - pluginManager: pluginManager, - }, function(anyWallet) { - $scope.retreiving = false; - $scope.anyProfile = anyProfile; - $scope.anyWallet = anyWallet; - }); - - }); - + identityService.checkIdentity($scope); $scope.createProfile = function(form) { if (form && form.$invalid) { @@ -25,15 +12,7 @@ angular.module('copayApp.controllers').controller('CreateProfileController', fun return; } $scope.loading = true; - copay.Identity.create(form.email.$modelValue, form.password.$modelValue, { - pluginManager: pluginManager, - network: config.network, - networkName: config.networkName, - walletDefaults: config.wallet, - passphraseConfig: config.passphraseConfig, - }, function(err, iden, firstWallet) { - controllerUtils.bindProfile($scope, iden, firstWallet); - }); + identityService.createIdentity($scope, form); } }); diff --git a/js/controllers/home.js b/js/controllers/home.js index 46ac43ac0..a42475a0f 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -1,9 +1,10 @@ 'use strict'; -angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager) { +angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) { controllerUtils.redirIfLogged(); + $scope.retreiving = true; - $scope.retreiving = false; + identityService.checkIdentity($scope); $scope.openProfile = function(form) { if (form && form.$invalid) { diff --git a/js/services/identity.js b/js/services/identity.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/js/services/identityService.js b/js/services/identityService.js new file mode 100644 index 000000000..943ee609c --- /dev/null +++ b/js/services/identityService.js @@ -0,0 +1,40 @@ +'use strict'; + +angular.module('copayApp.services') +.factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) { + var root = {}; + + root.checkIdentity = function (scope) { + copay.Identity.anyProfile({ + pluginManager: pluginManager, + }, function(anyProfile) { + copay.Identity.anyWallet({ + pluginManager: pluginManager, + }, function(anyWallet) { + scope.retreiving = false; + scope.anyProfile = anyProfile ? true : false; + scope.anyWallet = anyWallet ? true : false; + + if (!scope.anyProfile) { + $location.path('/createProfile'); + } + }); + }); + }; + + root.createIdentity = function (scope, form) { + copay.Identity.create(form.email.$modelValue, form.password.$modelValue, { + pluginManager: pluginManager, + network: config.network, + networkName: config.networkName, + walletDefaults: config.wallet, + passphraseConfig: config.passphraseConfig, + }, function(err, iden, firstWallet) { + controllerUtils.bindProfile(scope, iden, firstWallet); + scope.loading = false; + }); + }; + + return root; +}); + From ad87c4fc565a8ccddce98b0cc37be0d1dba4ea1f Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 24 Oct 2014 11:58:22 -0300 Subject: [PATCH 2/2] refactor identityService --- js/controllers/createProfile.js | 4 ++-- js/controllers/home.js | 18 ++---------------- js/services/identityService.js | 24 ++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/js/controllers/createProfile.js b/js/controllers/createProfile.js index 485de574c..5dae983b9 100644 --- a/js/controllers/createProfile.js +++ b/js/controllers/createProfile.js @@ -4,7 +4,7 @@ angular.module('copayApp.controllers').controller('CreateProfileController', fun controllerUtils.redirIfLogged(); $scope.retreiving = true; - identityService.checkIdentity($scope); + identityService.check($scope); $scope.createProfile = function(form) { if (form && form.$invalid) { @@ -12,7 +12,7 @@ angular.module('copayApp.controllers').controller('CreateProfileController', fun return; } $scope.loading = true; - identityService.createIdentity($scope, form); + identityService.create($scope, form); } }); diff --git a/js/controllers/home.js b/js/controllers/home.js index a42475a0f..96f893fe4 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -4,7 +4,7 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc controllerUtils.redirIfLogged(); $scope.retreiving = true; - identityService.checkIdentity($scope); + identityService.check($scope); $scope.openProfile = function(form) { if (form && form.$invalid) { @@ -12,20 +12,6 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc return; } $scope.loading = true; - copay.Identity.open(form.email.$modelValue, form.password.$modelValue, { - pluginManager: pluginManager, - network: config.network, - networkName: config.networkName, - walletDefaults: config.wallet, - passphraseConfig: config.passphraseConfig, - }, function(err, iden, lastFocusedWallet) { - if (err && !iden) { - console.log('Error:' + err) - controllerUtils.onErrorDigest( - $scope, (err.toString() || '').match('PNOTFOUND') ? 'Profile not found' : 'Unknown error'); - } else { - controllerUtils.bindProfile($scope, iden, lastFocusedWallet); - } - }); + identityService.open($scope, form); } }); diff --git a/js/services/identityService.js b/js/services/identityService.js index 943ee609c..c2fe34f93 100644 --- a/js/services/identityService.js +++ b/js/services/identityService.js @@ -4,7 +4,7 @@ angular.module('copayApp.services') .factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) { var root = {}; - root.checkIdentity = function (scope) { + root.check = function (scope) { copay.Identity.anyProfile({ pluginManager: pluginManager, }, function(anyProfile) { @@ -22,7 +22,7 @@ angular.module('copayApp.services') }); }; - root.createIdentity = function (scope, form) { + root.create = function (scope, form) { copay.Identity.create(form.email.$modelValue, form.password.$modelValue, { pluginManager: pluginManager, network: config.network, @@ -35,6 +35,26 @@ angular.module('copayApp.services') }); }; + + root.open = function (scope, form) { + copay.Identity.open(form.email.$modelValue, form.password.$modelValue, { + pluginManager: pluginManager, + network: config.network, + networkName: config.networkName, + walletDefaults: config.wallet, + passphraseConfig: config.passphraseConfig, + }, function(err, iden, lastFocusedWallet) { + if (err && !iden) { + console.log('Error:' + err) + controllerUtils.onErrorDigest( + scope, (err.toString() || '').match('PNOTFOUND') ? 'Profile not found' : 'Unknown error'); + } else { + controllerUtils.bindProfile(scope, iden, lastFocusedWallet); + } + scope.loading = false; + }); + } + return root; });