First enhancement in the send flow architecture

This commit is contained in:
Jean-Baptiste Dominguez 2018-08-29 15:38:23 +09:00
commit d864b35513
4 changed files with 197 additions and 21 deletions

View file

@ -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() {

View file

@ -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();
}
};
})();

View file

@ -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);

View file

@ -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();
}
};
})();