From d864b355133addddeaa37ff9e4e683d7b9ee0dce Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 29 Aug 2018 15:38:23 +0900 Subject: [PATCH] First enhancement in the send flow architecture --- src/js/controllers/tabsController.js | 3 +- src/js/services/send-flow-router.service.js | 52 +++++++ ...wService.js => send-flow-state.service.js} | 34 ++--- src/js/services/send-flow.service.js | 129 ++++++++++++++++++ 4 files changed, 197 insertions(+), 21 deletions(-) create mode 100644 src/js/services/send-flow-router.service.js rename src/js/services/{sendFlowService.js => send-flow-state.service.js} (80%) create mode 100644 src/js/services/send-flow.service.js diff --git a/src/js/controllers/tabsController.js b/src/js/controllers/tabsController.js index b3de6c70f..fff98936b 100644 --- a/src/js/controllers/tabsController.js +++ b/src/js/controllers/tabsController.js @@ -16,8 +16,7 @@ angular.module('copayApp.controllers').controller('tabsController', function($ro }; $scope.startFreshSend = function() { - sendFlowService.clear(); - $state.go('tabs.send'); + sendFlowService.start(); }; $scope.importInit = function() { diff --git a/src/js/services/send-flow-router.service.js b/src/js/services/send-flow-router.service.js new file mode 100644 index 000000000..ac4a63668 --- /dev/null +++ b/src/js/services/send-flow-router.service.js @@ -0,0 +1,52 @@ +'use strict'; + +(function(){ + +angular + .module('copayApp.services') + .factory('sendFlowRouterService', sendFlowRouterService); + + function sendFlowRouterService($state, $ionicHistory) { + + var router = { + // 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, + goBack: goBack, + }; + + return router; + + /** + * + */ + function start() { + $ionicHistory.clearHistory(); + $state.go('tabs.send'); + } + + /** + * + */ + function goNext(state) { + + /** + * Strategy + * Clean the history & and go to the send tab. + */ + // need to complete here + } + + function goBack() { + + /** + * Strategy + */ + $ionicHistory.goBack(); + } + }; + +})(); \ No newline at end of file diff --git a/src/js/services/sendFlowService.js b/src/js/services/send-flow-state.service.js similarity index 80% rename from src/js/services/sendFlowService.js rename to src/js/services/send-flow-state.service.js index 62989b3c5..bdca7ee91 100644 --- a/src/js/services/sendFlowService.js +++ b/src/js/services/send-flow-state.service.js @@ -4,9 +4,9 @@ angular .module('copayApp.services') - .factory('sendFlowService', sendFlowService); + .factory('sendFlowStateService', sendFlowStateService); - function sendFlowService($log) { + function sendFlowStateService() { var service = { // A separate state variable so we can ensure it is cleared of everything, @@ -23,16 +23,21 @@ angular previousStates: [], // Functions + init: init, clear: clear, - getStateClone: getStateClone, + getClone: getClone, map: map, - popState: popState, - pushState: pushState, - startSend: startSend + pop: pop, + push: push, }; return service; + function init(params) { + clear(); + map(params); + } + function clear() { console.log("sendFlow clear()"); clearCurrent(); @@ -55,7 +60,7 @@ angular /** * Handy for debugging */ - function getStateClone() { + function getClone() { var currentState = {}; Object.keys(service.state).forEach(function forCurrentParam(key) { if (typeof service.state[key] !== 'function' && key !== 'previousStates') { @@ -65,22 +70,13 @@ angular return currentState; } - /** - * Clears all previous state - */ - function startSend(params) { - console.log('startSend()'); - clear(); - map(params); - } - function map(params) { Object.keys(params).forEach(function forNewParam(key) { service.state[key] = params[key]; }); }; - function popState() { + function pop() { console.log('sendFlow pop'); if (service.previousStates.length) { var params = service.previousStates.pop(); @@ -91,9 +87,9 @@ angular } }; - function pushState(params) { + function push(params) { console.log('sendFlow push'); - var currentParams = getStateClone(); + var currentParams = getClone(); service.previousStates.push(currentParams); clearCurrent(); map(params); diff --git a/src/js/services/send-flow.service.js b/src/js/services/send-flow.service.js new file mode 100644 index 000000000..9f6cbc70b --- /dev/null +++ b/src/js/services/send-flow.service.js @@ -0,0 +1,129 @@ +'use strict'; + +(function(){ + +angular + .module('copayApp.services') + .factory('sendFlowService', sendFlowService); + + function sendFlowService( + sendFlowStateService, sendFlowRouterService + , bitcoinUriService, payproService + , popupService + ) { + + 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, + goBack: goBack, + getStateClone: getStateClone + }; + + return service; + + /** + * Clears all previous state + */ + async function start(params) { + console.log('start()'); + + if (params) { + if (params.data) { + var res = bitcoinUriService.parse(params.data); + + if (res.isValid) { + + // If BIP70 + if (res.url) { + var url = res.url; + var coin = res.coin || ''; + await new Promise(function (resolve) { + payproService.getPayProDetails(url, coin, function onGetPayProDetails(err, payProData) { + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), err); + } else { + // Fill in the params + var toAddr = payProData.toAddress; + var amount = payProData.amount; + var paymentUrl = payProData.url; + var expires = payProData.expires; + var time = payProData.time; + var name = payProData.domain; + + // Detect some merchant that we know + if (payProData.memo.indexOf('eGifter') > -1) { + name = 'eGifter' + } else if (paymentUrl.indexOf('https://bitpay.com') > -1) { + name = 'BitPay'; + } + + // Init thirdParty + var thirdPartyData = { + id: 'bip70', + amount: amount, + caTrusted: true, + name: name, + domain: payProData.domain, + expires: expires, + memo: payProData.memo, + network: 'livenet', + requiredFeeRate: payProData.requiredFeeRate, + selfSigned: 0, + time: time, + toAddress: toAddr, + url: paymentUrl, + verified: true + }; + + // Fill in params + params.amount = thirdPartyData.amount, + params.toAddress = thirdPartyData.toAddress, + params.coin = coin, + params.thirdParty = thirdPartyData + } + + // Resolve + resolve(); + }); + }); + } + } + } + + // Init the state if params is defined + sendFlowStateService.init(params); + console.log(params); + } + + /** + * Routing strategy to -> send-flow-router.service + */ + sendFlowRouterService.start(); + } + + function goNext(state) { + // Push the new state + sendFlowStateService.push(state); + + // Go next + sendFlowRouterService.goNext(state); + } + + function goBack() { + // Pop the current state + sendFlowStateService.pop(); + + // Go back + sendFlowRouterService.goBack(); + } + + function getStateClone () { + return sendFlowStateService.getClone(); + } + }; + +})(); \ No newline at end of file