From c715fdcb41a12d3285d6fbbc6f19aa9d12f2c26f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Tue, 4 Sep 2018 11:24:07 +0900 Subject: [PATCH] Rename incomingData to incomingDataService --- src/js/controllers/tab-scan.js | 4 +- src/js/controllers/tab-send.js | 41 +++++-------- src/js/controllers/tabsController.js | 6 +- src/js/directives/shapeshiftCoinTrader.js | 4 +- ...comingData.js => incoming-data.service.js} | 4 +- src/js/services/openURL.js | 4 +- src/js/services/send-flow-router.service.js | 16 ++---- src/js/services/send-flow-state.service.js | 45 ++++++++++++--- src/js/services/send-flow.service.js | 57 +++++++++---------- src/js/services/shapeshiftService.js | 4 +- 10 files changed, 98 insertions(+), 87 deletions(-) rename src/js/services/{incomingData.js => incoming-data.service.js} (83%) diff --git a/src/js/controllers/tab-scan.js b/src/js/controllers/tab-scan.js index 7fc59dd6f..14368ee1c 100644 --- a/src/js/controllers/tab-scan.js +++ b/src/js/controllers/tab-scan.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabScanController', function(bitcoinUriService, gettextCatalog, popupService, $scope, $log, $timeout, scannerService, incomingData, $state, $ionicHistory, $rootScope, $ionicNavBarDelegate) { +angular.module('copayApp.controllers').controller('tabScanController', function(gettextCatalog, popupService, $scope, $log, $timeout, scannerService, incomingDataService, $state, $ionicHistory, $rootScope, $ionicNavBarDelegate) { var scannerStates = { unauthorized: 'unauthorized', @@ -111,7 +111,7 @@ angular.module('copayApp.controllers').controller('tabScanController', function( // Sometimes (testing in Chrome, when reading QR Code) data is an object // that has a string data.result. contents = contents.result || contents; - incomingData.redir(contents, function onError(err) { + incomingDataService.redir(contents, function onError(err) { if (err) { var title = gettextCatalog.getString('Scan Failed'); popupService.showAlert(title, err.message, function onAlertShown() { diff --git a/src/js/controllers/tab-send.js b/src/js/controllers/tab-send.js index dfacd465a..03a9562e8 100644 --- a/src/js/controllers/tab-send.js +++ b/src/js/controllers/tab-send.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabSendController', function(bitcoinUriService, $scope, $rootScope, $log, $timeout, $ionicScrollDelegate, $ionicLoading, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, sendFlowService, bwcError, gettextCatalog, scannerService, configService, bitcoinCashJsService, $ionicPopup, $ionicNavBarDelegate, clipboardService) { +angular.module('copayApp.controllers').controller('tabSendController', function(bitcoinUriService, $scope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, platformInfo, sendFlowService, gettextCatalog, configService, $ionicPopup, $ionicNavBarDelegate, clipboardService, incomingDataService) { var clipboardHasAddress = false; var clipboardHasContent = false; var originalList; @@ -62,14 +62,6 @@ angular.module('copayApp.controllers').controller('tabSendController', function( }); $scope.findContact = function(search) { - sendFlowService.start({ - data: search - }); - return; - //if (incomingData.redir(search)) { - //return; - //} - if (!search || search.length < 1) { $scope.list = originalList; $timeout(function() { @@ -78,12 +70,16 @@ angular.module('copayApp.controllers').controller('tabSendController', function( return; } - var result = lodash.filter(originalList, function(item) { - var val = item.name; - return lodash.startsWith(val.toLowerCase(), search.toLowerCase()); + var params = sendFlowService.state.getClone(); + params.data = search; + sendFlowService.start(params, function onError() { + var result = lodash.filter(originalList, function(item) { + var val = item.name; + return lodash.startsWith(val.toLowerCase(), search.toLowerCase()); + }); + + $scope.list = result; }); - - $scope.list = result; }; var hasWallets = function() { @@ -190,26 +186,17 @@ angular.module('copayApp.controllers').controller('tabSendController', function( $log.debug('Got toAddress:' + toAddress + ' | ' + item.name); var stateParams = sendFlowService.state.getClone(); - stateParams.toAddress = toAddress, + stateParams.toAddress = toAddress; stateParams.coin = item.coin; - sendFlowService.goNext(stateParams); - - /*if (!stateParams.fromWalletId) { // If we have no toAddress or fromWallet - $state.transitionTo('tabs.send.origin'); - } else { - $state.transitionTo('tabs.send.amount'); - }*/ - + sendFlowService.start(stateParams); }); }; $scope.startWalletToWalletTransfer = function() { console.log('startWalletToWalletTransfer()'); var params = sendFlowService.state.getClone(); - sendFlowService.goNext({ - isWalletTransfer: true, - fromWalletId: params.fromWalletId - }); + params.isWalletTransfer = true; + sendFlowService.start(params); } // This could probably be enhanced refactoring the routes abstract states diff --git a/src/js/controllers/tabsController.js b/src/js/controllers/tabsController.js index 4d01acbf3..b78274ecb 100644 --- a/src/js/controllers/tabsController.js +++ b/src/js/controllers/tabsController.js @@ -1,9 +1,9 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabsController', function($rootScope, $log, $scope, $state, $stateParams, $timeout, platformInfo, incomingData, lodash, popupService, gettextCatalog, scannerService, sendFlowService) { +angular.module('copayApp.controllers').controller('tabsController', function($rootScope, $log, $scope, $state, $stateParams, $timeout, platformInfo, incomingDataService, lodash, popupService, gettextCatalog, scannerService, sendFlowService) { $scope.onScan = function(data) { - incomingData.redir(data, function onError(err) { + incomingDataService.redir(data, function onError(err) { if (err) { popupService.showAlert(gettextCatalog.getString('Error'), err.message); } @@ -40,7 +40,7 @@ angular.module('copayApp.controllers').controller('tabsController', function($ro if (err) { popupService.showAlert(gettextCatalog.getString('Error'), err.message); } else { - incomingData.redir(contents, function onError(err) { + incomingDataService.redir(contents, function onError(err) { if (err) { popupService.showAlert(gettextCatalog.getString('Error'), err.message); } diff --git a/src/js/directives/shapeshiftCoinTrader.js b/src/js/directives/shapeshiftCoinTrader.js index c7fe6c4fd..793f380fb 100644 --- a/src/js/directives/shapeshiftCoinTrader.js +++ b/src/js/directives/shapeshiftCoinTrader.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.directives').directive('shapeshiftCoinTrader', function($interval, shapeshiftApiService, profileService, incomingData, ongoingProcess) { +angular.module('copayApp.directives').directive('shapeshiftCoinTrader', function($interval, shapeshiftApiService, profileService, incomingDataService, ongoingProcess) { return { restrict: 'E', transclude: true, @@ -112,7 +112,7 @@ angular.module('copayApp.directives').directive('shapeshiftCoinTrader', function }; // How to handle this - if (incomingData.redir(sendAddress, 'shapeshift', shapeshiftData)) { + if (incomingDataService.redir(sendAddress, 'shapeshift', shapeshiftData)) { ongoingProcess.set('connectingShapeshift', false); return; } diff --git a/src/js/services/incomingData.js b/src/js/services/incoming-data.service.js similarity index 83% rename from src/js/services/incomingData.js rename to src/js/services/incoming-data.service.js index c2c44c063..eece6d17c 100644 --- a/src/js/services/incomingData.js +++ b/src/js/services/incoming-data.service.js @@ -1,10 +1,10 @@ 'use strict'; /** - * incomingData is an intermediate to redirect either to the sendFlow + * incomingDataService is an intermediate to redirect either to the sendFlow * or to import/join a wallet. */ -angular.module('copayApp.services').factory('incomingData', function(externalLinkService, bitcoinUriService, $log, $state, $timeout, $ionicHistory, bitcore, bitcoreCash, $rootScope, payproService, scannerService, sendFlowService, appConfigService, popupService, gettextCatalog, bitcoinCashJsService) { +angular.module('copayApp.services').factory('incomingDataService', function(bitcoinUriService, $log, $state, $rootScope, scannerService, sendFlowService, gettextCatalog) { var root = {}; diff --git a/src/js/services/openURL.js b/src/js/services/openURL.js index 6f972d291..2cf8d95a5 100644 --- a/src/js/services/openURL.js +++ b/src/js/services/openURL.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('openURLService', function($rootScope, $ionicHistory, $document, $log, $state, platformInfo, lodash, profileService, incomingData, appConfigService) { +angular.module('copayApp.services').factory('openURLService', function($rootScope, $ionicHistory, $document, $log, $state, platformInfo, lodash, profileService, incomingDataService, appConfigService) { var root = {}; var handleOpenURL = function(args) { @@ -23,7 +23,7 @@ angular.module('copayApp.services').factory('openURLService', function($rootScop document.addEventListener('handleopenurl', handleOpenURL, false); - incomingData.redir(url, function onError(err) { + incomingDataService.redir(url, function onError(err) { if (err) { $log.warn('Unknown URL! : ' + url); popupService.showAlert(gettextCatalog.getString('Error'), err.message); diff --git a/src/js/services/send-flow-router.service.js b/src/js/services/send-flow-router.service.js index d31c585f7..883a08971 100644 --- a/src/js/services/send-flow-router.service.js +++ b/src/js/services/send-flow-router.service.js @@ -12,9 +12,6 @@ angular ) { var service = { - // A separate state variable so we can ensure it is cleared of everything, - // even other properties added that this service does not know about. (such as "coin") - // Functions start: start, goNext: goNext, @@ -24,7 +21,7 @@ angular return service; /** - * + * Start new send flow */ function start() { var state = sendFlowStateService.state; @@ -48,8 +45,8 @@ angular } /** - * Strategy - * https://bitcoindotcom.atlassian.net/wiki/x/BQDWKQ + * Go to the next page + * Routing strategy : https://bitcoindotcom.atlassian.net/wiki/x/BQDWKQ */ function goNext() { var state = sendFlowStateService.state; @@ -77,11 +74,10 @@ angular } } + /** + * Go to the previous page + */ function goBack() { - - /** - * Strategy - */ $ionicHistory.goBack(); } }; diff --git a/src/js/services/send-flow-state.service.js b/src/js/services/send-flow-state.service.js index 0d2912b59..0007ccf4c 100644 --- a/src/js/services/send-flow-state.service.js +++ b/src/js/services/send-flow-state.service.js @@ -6,11 +6,10 @@ angular .module('copayApp.services') .factory('sendFlowStateService', sendFlowStateService); - function sendFlowStateService() { + function sendFlowStateService($log) { var service = { - // A separate state variable so we can ensure it is cleared of everything, - // even other properties added that this service does not know about. (such as "coin") + // Variables state: { amount: '', displayAddress: null, @@ -34,7 +33,13 @@ angular return service; + /** + * Init state & stack + * @param {Object} params + */ function init(params) { + $log.debug("send-flow-state init()"); + clear(); if (params) { @@ -42,14 +47,22 @@ angular } } + /** + * Clear a state & stack + */ function clear() { - console.log("sendFlow clear()"); + $log.debug("send-flow-state clear()"); + clearCurrent(); service.previousStates = []; } + /** + * Clear current state only + */ function clearCurrent() { - console.log("sendFlow clearCurrent()"); + $log.debug("send-flow-state clearCurrent()"); + service.state = { amount: '', displayAddress: null, @@ -62,7 +75,7 @@ angular } /** - * Handy for debugging + * Get a clone of the current state */ function getClone() { var currentState = {}; @@ -74,14 +87,22 @@ angular return currentState; } + /** + * Fill in the current state from the params + * @param {Object} params + */ function map(params) { Object.keys(params).forEach(function forNewParam(key) { service.state[key] = params[key]; }); }; + /** + * Pop state + */ function pop() { - console.log('sendFlow pop'); + $log.debug('send-flow-state pop'); + if (service.previousStates.length) { var params = service.previousStates.pop(); clearCurrent(); @@ -91,14 +112,22 @@ angular } }; + /** + * Push state + * @param {Object} params + */ function push(params) { - console.log('sendFlow push'); + $log.debug('send-flow-state push'); + var currentParams = getClone(); service.previousStates.push(currentParams); clearCurrent(); map(params); }; + /** + * Is empty stack + */ function isEmpty() { return service.previousStates.length == 0; }; diff --git a/src/js/services/send-flow.service.js b/src/js/services/send-flow.service.js index 2be375aa8..41929319d 100644 --- a/src/js/services/send-flow.service.js +++ b/src/js/services/send-flow.service.js @@ -10,12 +10,11 @@ angular sendFlowStateService, sendFlowRouterService , bitcoinUriService, payproService, bitcoinCashJsService , popupService, gettextCatalog - , $state + , $state, $log ) { var service = { - // A separate state variable so we can ensure it is cleared of everything, - // even other properties added that this service does not know about. (such as "coin") + // Variables state: sendFlowStateService, router: sendFlowRouterService, @@ -28,19 +27,19 @@ angular return service; /** - * Clears all previous state + * Start a new send flow + * @param {Object} params + * @param {Function} onError */ function start(params, onError) { - console.log('start()'); + $log.debug('send-flow start()'); if (params && params.data) { var res = bitcoinUriService.parse(params.data); if (res.isValid) { - /** - * If BIP70 - */ + // If BIP70 (url) if (res.url) { var url = res.url; var coin = res.coin || ''; @@ -81,18 +80,14 @@ angular verified: true }; - /** - * Fill in params - */ + // Fill in params params.amount = thirdPartyData.amount, params.toAddress = thirdPartyData.toAddress, params.coin = coin, params.thirdParty = thirdPartyData } - /** - * Resolve - */ + // Resolve _next(); }); } else { @@ -107,7 +102,8 @@ angular if (res.publicAddress) { var prefix = res.isTestnet ? 'bchtest:' : 'bitcoincash:'; params.displayAddress = res.publicAddress.cashAddr || res.publicAddress.legacy || res.publicAddress.bitpay; - params.toAddress = bitcoinCashJsService.readAddress((prefix + res.publicAddress.cashAddr) || res.publicAddress.legacy || res.publicAddress.bitpay).legacy; + var formatAddress = res.publicAddress.cashAddr ? prefix + params.displayAddress : params.displayAddress; + params.toAddress = bitcoinCashJsService.readAddress(formatAddress).legacy; } _next(); @@ -126,32 +122,35 @@ angular function _next() { sendFlowStateService.init(params); - /** - * Routing strategy to -> send-flow-router.service - */ + // Routing strategy to -> send-flow-router.service sendFlowRouterService.start(); } } + /** + * Go to the next step + * @param {Object} state + */ function goNext(state) { - /** - * Save the current route before leaving - */ + $log.debug('send-flow goNext()'); + + // Save the current route before leaving state.route = $state.current.name; - /** - * Save the state and redirect the user - */ + // Save the state and redirect the user sendFlowStateService.push(state); sendFlowRouterService.goNext(); } + /** + * Go to the previous step + */ function goBack() { - /** - * Remove the state on top and redirect the user - */ - sendFlowStateService.pop(); - sendFlowRouterService.goBack(); + $log.debug('send-flow goBack()'); + + // Remove the state on top and redirect the user + sendFlowStateService.pop(); + sendFlowRouterService.goBack(); } }; diff --git a/src/js/services/shapeshiftService.js b/src/js/services/shapeshiftService.js index 1ce9672ce..b1d2f6e7d 100644 --- a/src/js/services/shapeshiftService.js +++ b/src/js/services/shapeshiftService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('shapeshiftService', function ($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingData, platformInfo, servicesService) { +angular.module('copayApp.services').factory('shapeshiftService', function ($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingDataService, platformInfo, servicesService) { var root = {}; root.ShiftState = 'Shift'; root.coinIn = ''; @@ -111,7 +111,7 @@ angular.module('copayApp.services').factory('shapeshiftService', function ($http toAddress: txData.deposit }; // - // if (incomingData.redir(sendAddress, 'shapeshift', shapeshiftData)) { + // if (incomingDataService.redir(sendAddress, 'shapeshift', shapeshiftData)) { ongoingProcess.set('connectingShapeshift', false); // return; // }