From dff0d51ef3699df1d5ccaa5299637510b63c0ab6 Mon Sep 17 00:00:00 2001 From: Brendon Duncan Date: Thu, 9 Aug 2018 11:10:26 +1200 Subject: [PATCH] Refactored sendFlowService and cleared its state before entering the send tab. --- src/js/controllers/addressbookView.js | 3 +- src/js/controllers/amount.js | 1 + src/js/controllers/shapeshift.js | 3 +- src/js/controllers/tab-home.js | 8 +- src/js/controllers/tab-send.js | 7 ++ src/js/controllers/tabsController.js | 9 ++- src/js/controllers/walletDetails.js | 10 ++- src/js/services/sendFlowService.js | 106 +++++++++++++++++--------- www/views/tab-home.html | 2 +- www/views/tab-send.html | 21 ++++- www/views/tabs.html | 2 +- 11 files changed, 124 insertions(+), 48 deletions(-) diff --git a/src/js/controllers/addressbookView.js b/src/js/controllers/addressbookView.js index aab6fb5b4..5f6881d10 100644 --- a/src/js/controllers/addressbookView.js +++ b/src/js/controllers/addressbookView.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('addressbookViewController', function($scope, $state, $timeout, lodash, addressbookService, popupService, $ionicHistory, platformInfo, gettextCatalog, configService, bitcoinCashJsService) { +angular.module('copayApp.controllers').controller('addressbookViewController', function($scope, sendFlowService, $state, $timeout, lodash, addressbookService, popupService, $ionicHistory, platformInfo, gettextCatalog, configService, bitcoinCashJsService) { var config = configService.getSync(); var defaults = configService.getDefaults(); @@ -22,6 +22,7 @@ angular.module('copayApp.controllers').controller('addressbookViewController', f $scope.sendTo = function() { $ionicHistory.removeBackView(); + sendFlowService.clear(); $state.go('tabs.send'); $timeout(function() { var to = ''; diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index 68d4e869b..8ff214d87 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -209,6 +209,7 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, } function goBack() { + sendFlowService.previousState(); if (vm.thirdParty && vm.thirdParty.id === 'shapeshift') { $state.go('tabs.send').then(function() { $ionicHistory.clearHistory(); diff --git a/src/js/controllers/shapeshift.js b/src/js/controllers/shapeshift.js index 4bf3db764..fcb43026d 100644 --- a/src/js/controllers/shapeshift.js +++ b/src/js/controllers/shapeshift.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('shapeshiftController', function($scope, $state, $timeout, $ionicHistory, profileService, walletService, popupService, lodash, $ionicNavBarDelegate) { +angular.module('copayApp.controllers').controller('shapeshiftController', function($scope, sendFlowService, $state, $timeout, $ionicHistory, profileService, walletService, popupService, lodash, $ionicNavBarDelegate) { var walletsBtc = []; var walletsBch = []; @@ -67,6 +67,7 @@ angular.module('copayApp.controllers').controller('shapeshiftController', functi thirdParty: JSON.stringify({id: 'shapeshift'}) }; + sendFlowService.clear(); $state.go('tabs.home').then(function() { $ionicHistory.clearHistory(); $state.go('tabs.send').then(function() { diff --git a/src/js/controllers/tab-home.js b/src/js/controllers/tab-home.js index 7671d524a..3d63ae41f 100644 --- a/src/js/controllers/tab-home.js +++ b/src/js/controllers/tab-home.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('tabHomeController', - function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, $window, gettextCatalog, lodash, popupService, ongoingProcess, bannerService, externalLinkService, latestReleaseService, profileService, walletService, configService, $log, platformInfo, sendFlowService, storageService, txpModalService, appConfigService, startupService, addressbookService, bwcError, nextStepsService, buyAndSellService, homeIntegrationsService, bitpayCardService, pushNotificationsService, timeService, bitcoincomService, pricechartService, firebaseEventsService, servicesService, shapeshiftService, $ionicNavBarDelegate, signVerifyMessageService) { + function($rootScope, sendFlowService, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, $window, gettextCatalog, lodash, popupService, ongoingProcess, bannerService, externalLinkService, latestReleaseService, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, appConfigService, startupService, addressbookService, bwcError, nextStepsService, buyAndSellService, homeIntegrationsService, bitpayCardService, pushNotificationsService, timeService, bitcoincomService, pricechartService, firebaseEventsService, servicesService, shapeshiftService, $ionicNavBarDelegate, signVerifyMessageService) { var wallet; var listeners = []; var notifications = []; @@ -31,7 +31,6 @@ angular.module('copayApp.controllers').controller('tabHomeController', }); $scope.$on("$ionicView.beforeEnter", function(event, data) { - sendFlowService.clear(); if (!$scope.homeTip) { storageService.getHomeTipAccepted(function(error, value) { @@ -119,6 +118,11 @@ angular.module('copayApp.controllers').controller('tabHomeController', return timeService.withinPastDay(time); }; + $scope.startFreshSend = function() { + sendFlowService.clear(); + $state.go('tabs.send'); + } + $scope.openExternalLink = function() { var url = 'https://github.com/Bitcoin-com/Wallet/releases/latest'; var optIn = true; diff --git a/src/js/controllers/tab-send.js b/src/js/controllers/tab-send.js index 0c0c1bfe1..33befb8f2 100644 --- a/src/js/controllers/tab-send.js +++ b/src/js/controllers/tab-send.js @@ -187,6 +187,13 @@ angular.module('copayApp.controllers').controller('tabSendController', function( }); }; + $scope.startWalletToWalletTransfer = function() { + console.log('startWalletToWalletTransfer()'); + $state.transitionTo('tabs.send.wallet-to-wallet', { + fromWalletId: sendFlowService.fromWalletId + }); + } + // This could probably be enhanced refactoring the routes abstract states $scope.createWallet = function() { $state.go('tabs.home').then(function() { diff --git a/src/js/controllers/tabsController.js b/src/js/controllers/tabsController.js index a186879e3..b3de6c70f 100644 --- a/src/js/controllers/tabsController.js +++ b/src/js/controllers/tabsController.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabsController', function($rootScope, $log, $scope, $state, $stateParams, $timeout, platformInfo, incomingData, lodash, popupService, gettextCatalog, scannerService) { +angular.module('copayApp.controllers').controller('tabsController', function($rootScope, $log, $scope, $state, $stateParams, $timeout, platformInfo, incomingData, lodash, popupService, gettextCatalog, scannerService, sendFlowService) { $scope.onScan = function(data) { if (!incomingData.redir(data)) { @@ -15,6 +15,11 @@ angular.module('copayApp.controllers').controller('tabsController', function($ro }; }; + $scope.startFreshSend = function() { + sendFlowService.clear(); + $state.go('tabs.send'); + }; + $scope.importInit = function() { $scope.fromOnboarding = $stateParams.fromOnboarding; $timeout(function() { @@ -23,7 +28,7 @@ angular.module('copayApp.controllers').controller('tabsController', function($ro }; $scope.chooseScanner = function() { - + sendFlowService.clear(); var isWindowsPhoneApp = platformInfo.isCordova && platformInfo.isWP; if (!isWindowsPhoneApp) { diff --git a/src/js/controllers/walletDetails.js b/src/js/controllers/walletDetails.js index 5200c5987..b159f7bc4 100644 --- a/src/js/controllers/walletDetails.js +++ b/src/js/controllers/walletDetails.js @@ -471,13 +471,20 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun function rgbToHex(r, g, b) { return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b); } + $scope.goToSend = function() { - sendFlowService.fromWalletId = $scope.wallet.id; + sendFlowService.startSend({ + fromWalletId: $scope.wallet.id + }); + + // Go home first so that the Home tab works properly $state.go('tabs.home').then(function () { $ionicHistory.clearHistory(); $state.go('tabs.send'); }); + }; + $scope.goToReceive = function() { $state.go('tabs.home', { walletId: $scope.wallet.id @@ -488,6 +495,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun }); }); }; + $scope.goToBuy = function() { $state.go('tabs.home', { walletId: $scope.wallet.id diff --git a/src/js/services/sendFlowService.js b/src/js/services/sendFlowService.js index 58b457b3a..1af112ed2 100644 --- a/src/js/services/sendFlowService.js +++ b/src/js/services/sendFlowService.js @@ -1,48 +1,78 @@ 'use strict'; -angular.module('copayApp.services').factory('sendFlowService', function ($log) { - var vm = this; +(function(){ - vm.amount = false; +angular + .module('copayApp.services') + .factory('sendFlowService', sendFlowService); + + function sendFlowService($log) { - vm.fromWalletId = false; - vm.previousStates = []; - vm.thirdParty = false; - vm.sendMax = false; - vm.toAddress = false; - vm.toWalletId = false; + var service = { + amount: '', + fromWalletId: '', + sendMax: false, + thirdParty: null, + toAddress: '', + toWalletId: '', + previousStates: [], - vm.clear = function() { - $log.debug("Reinitialize Send Flow variables"); - vm.amount = false; - vm.fromWalletId = false; - vm.thirdParty = false; - vm.sendMax = false; - vm.toAddress = false; - vm.toWalletId = false; - vm.previousStates = []; - }; + // Functions + clear: clear, + map: map, + previousState: previousState, + startSend: startSend + }; - vm.map = function(params) { + return service; - var tempState = {}; - Object.keys(vm).map(function(key, index) { - if (typeof vm[key] !== 'function' && key !== 'previousStates') { - tempState[key] = vm[key]; - } - }); - vm.previousStates.push(tempState); - - Object.keys(params).map(function(key, index) { - vm[key] = params[key]; - }); - }; - - vm.previousState = function() { - if (vm.previousStates.length) { - vm.map(vm.previousStates.pop()); + function clear() { + $log.debug("Reinitialize Send Flow variables with clear()"); + service.amount = ''; + service.fromWalletId = ''; + service.sendMax = false; + service.thirdParty = null; + service.toAddress = ''; + service.toWalletId = ''; + service.previousStates = []; } + + /** + * Clears all previous state + * @param {} params + */ + function startSend(params) { + console.log('startSend()'); + clear(); + Object.keys(params).forEach(function forNewParam(key) { + service[key] = params[key]; + }); + } + + function map(params) { + + var currentState = {}; + Object.keys(service).forEach(function forCurrentParam(key) { + if (typeof service[key] !== 'function' && key !== 'previousStates') { + currentState[key] = service[key]; + } + }); + service.previousStates.push(currentState); + + // Do we want to inherit the previous state here, or clear first before adding new params? + + Object.keys(params).forEach(function forNewParam(key) { + service[key] = params[key]; + }); + }; + + function previousState() { + if (service.previousStates.length) { + map(service.previousStates.pop()); + } else { + clear(); + } + }; }; - return vm; -}); \ No newline at end of file +})(); \ No newline at end of file diff --git a/www/views/tab-home.html b/www/views/tab-home.html index 618f2e128..8ef922918 100644 --- a/www/views/tab-home.html +++ b/www/views/tab-home.html @@ -34,7 +34,7 @@ Buy Bitcoin
+ ng-click="startFreshSend()"> Send
diff --git a/www/views/tab-send.html b/www/views/tab-send.html index 20198c7f0..3378b53ed 100644 --- a/www/views/tab-send.html +++ b/www/views/tab-send.html @@ -4,6 +4,25 @@
+ +
+
+
From:
+
+
+ +
+
+

{{fromWallet.name}}

+ + +
+
+
+
+
@@ -22,7 +41,7 @@
- diff --git a/www/views/tabs.html b/www/views/tabs.html index 3c2683a13..39d0acf73 100644 --- a/www/views/tabs.html +++ b/www/views/tabs.html @@ -11,7 +11,7 @@ - +