From 16f2c780ce70f24e285eab40d38f53833be5dca9 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Tue, 9 May 2017 12:45:15 -0300 Subject: [PATCH] Simple refactor. Update address if receives tx --- src/js/controllers/tab-receive.js | 56 ++++++++++++------------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/src/js/controllers/tab-receive.js b/src/js/controllers/tab-receive.js index 6953f7b1f..8bc07ef07 100644 --- a/src/js/controllers/tab-receive.js +++ b/src/js/controllers/tab-receive.js @@ -14,11 +14,11 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi }); }; - $scope.setAddress = function() { - if (!$scope.wallet || $scope.generatingAddress || !$scope.wallet.isComplete()) return; + $scope.setAddress = function(newAddr) { $scope.addr = null; + if (!$scope.wallet || $scope.generatingAddress || !$scope.wallet.isComplete()) return; $scope.generatingAddress = true; - walletService.getAddress($scope.wallet, false, function(err, addr) { + walletService.getAddress($scope.wallet, newAddr, function(err, addr) { $scope.generatingAddress = false; if (err) { @@ -74,20 +74,6 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi }); }; - $scope.updateCurrentWallet = function(wallet) { - walletService.getStatus(wallet, {}, function(err, status) { - if (err) { - return popupService.showAlert(bwcError.msg(err, gettextCatalog.getString('Could not update wallet'))); - } - $timeout(function() { - $scope.wallet = profileService.getWallet(wallet.id); - $scope.wallet.status = status; - $scope.setAddress(); - $scope.$apply(); - }, 200); - }); - }; - $scope.shouldShowReceiveAddressFromHardware = function() { var wallet = $scope.wallet; if (wallet.isPrivKeyExternal() && wallet.credentials.hwInfo) { @@ -108,36 +94,38 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi $scope.wallets = profileService.getWallets(); $scope.singleWallet = $scope.wallets.length == 1; + if (!$scope.wallets[0]) return; + + // select first wallet if no wallet selected previously + var selectedWallet = checkSelectedWallet($scope.wallet, $scope.wallets); + $scope.onWalletSelect(selectedWallet); + listeners = [ $rootScope.$on('bwsEvent', function(e, walletId, type, n) { // Update current address - if ($scope.wallet && walletId == $scope.wallet.id) $scope.updateCurrentWallet($scope.wallet); + if ($scope.wallet && walletId == $scope.wallet.id && type == 'NewIncomingTx') $scope.setAddress(true); }) - ]; - - if (!$scope.wallets[0]) return; - if (!$scope.wallet) return $scope.init(); - - var w = lodash.find($scope.wallets, function(w) { - return w.id == $scope.wallet.id; - }); - if (w) $scope.updateCurrentWallet($scope.wallet); - else $scope.init(); + ]; }); - $scope.init = function() { - $scope.wallet = $scope.wallets[0]; - $scope.updateCurrentWallet($scope.wallet); - }; - $scope.$on("$ionicView.leave", function(event, data) { lodash.each(listeners, function(x) { x(); }); }); + var checkSelectedWallet = function(wallet, wallets) { + if (!wallet) return wallets[0]; + var w = lodash.find(wallets, function(w) { + return w.id == wallet.id; + }); + if (!w) return wallets[0]; + return wallet; + } + $scope.onWalletSelect = function(wallet) { - $scope.updateCurrentWallet(wallet); + $scope.wallet = wallet; + $scope.setAddress(); }; $scope.showWalletSelector = function() {