From ce976a1b42a7f743900cc3f65ebed1e1206a5ed4 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 22 Sep 2014 12:13:51 -0300 Subject: [PATCH] Rebased --- js/controllers/open.js | 52 +++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/js/controllers/open.js b/js/controllers/open.js index 95091cdbe..a98ff6a5e 100644 --- a/js/controllers/open.js +++ b/js/controllers/open.js @@ -14,15 +14,32 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc }; $rootScope.fromSetup = false; $scope.loading = false; - $scope.wallets = walletFactory.getWallets().sort(cmp); - $scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id); + $scope.retreiving = true; + + walletFactory.getWallets(function(err, wallets) { + + if (err || !wallets || !wallets.length) { + $location.path('/'); + } else { + $scope.retreiving = false; + $scope.wallets = wallets.sort(cmp); + + walletFactory.storage.getLastOpened(function(ret) { + if (ret && _.indexOf(_.pluck($scope.wallets, 'id')) == -1) + ret = null; + + $scope.selectedWalletId = ret || ($scope.wallets[0] && $scope.wallets[0].id); + + setTimeout(function() { + $rootScope.$digest(); + }, 0); + }); + } + }); + $scope.openPassword = ''; $scope.isMobile = !!window.cordova; - if (!$scope.wallets.length) { - $location.path('/'); - } - $scope.open = function(form) { if (form && form.$invalid) { notification.error('Error', 'Please enter the required fields'); @@ -34,19 +51,16 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc Passphrase.getBase64Async(password, function(passphrase) { var w, errMsg; - try { - w = walletFactory.open($scope.selectedWalletId, passphrase); - } catch (e) { - errMsg = e.message; - }; - if (!w) { - $scope.loading = false; - notification.error('Error', errMsg || 'Wrong password'); - $rootScope.$digest(); - return; - } - $rootScope.updatingBalance = true; - controllerUtils.startNetwork(w, $scope); + walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) { + if (!w) { + $scope.loading = false; + notification.error('Error', err.errMsg || 'Wrong password'); + $rootScope.$digest(); + } else { + $rootScope.updatingBalance = true; + controllerUtils.startNetwork(w, $scope); + } + }); }); };