First enhancement in the send flow architecture
This commit is contained in:
parent
b35ee316a8
commit
d864b35513
4 changed files with 197 additions and 21 deletions
|
|
@ -16,8 +16,7 @@ angular.module('copayApp.controllers').controller('tabsController', function($ro
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.startFreshSend = function() {
|
$scope.startFreshSend = function() {
|
||||||
sendFlowService.clear();
|
sendFlowService.start();
|
||||||
$state.go('tabs.send');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.importInit = function() {
|
$scope.importInit = function() {
|
||||||
|
|
|
||||||
52
src/js/services/send-flow-router.service.js
Normal file
52
src/js/services/send-flow-router.service.js
Normal 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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
angular
|
angular
|
||||||
.module('copayApp.services')
|
.module('copayApp.services')
|
||||||
.factory('sendFlowService', sendFlowService);
|
.factory('sendFlowStateService', sendFlowStateService);
|
||||||
|
|
||||||
function sendFlowService($log) {
|
function sendFlowStateService() {
|
||||||
|
|
||||||
var service = {
|
var service = {
|
||||||
// A separate state variable so we can ensure it is cleared of everything,
|
// A separate state variable so we can ensure it is cleared of everything,
|
||||||
|
|
@ -23,16 +23,21 @@ angular
|
||||||
previousStates: [],
|
previousStates: [],
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
|
init: init,
|
||||||
clear: clear,
|
clear: clear,
|
||||||
getStateClone: getStateClone,
|
getClone: getClone,
|
||||||
map: map,
|
map: map,
|
||||||
popState: popState,
|
pop: pop,
|
||||||
pushState: pushState,
|
push: push,
|
||||||
startSend: startSend
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return service;
|
return service;
|
||||||
|
|
||||||
|
function init(params) {
|
||||||
|
clear();
|
||||||
|
map(params);
|
||||||
|
}
|
||||||
|
|
||||||
function clear() {
|
function clear() {
|
||||||
console.log("sendFlow clear()");
|
console.log("sendFlow clear()");
|
||||||
clearCurrent();
|
clearCurrent();
|
||||||
|
|
@ -55,7 +60,7 @@ angular
|
||||||
/**
|
/**
|
||||||
* Handy for debugging
|
* Handy for debugging
|
||||||
*/
|
*/
|
||||||
function getStateClone() {
|
function getClone() {
|
||||||
var currentState = {};
|
var currentState = {};
|
||||||
Object.keys(service.state).forEach(function forCurrentParam(key) {
|
Object.keys(service.state).forEach(function forCurrentParam(key) {
|
||||||
if (typeof service.state[key] !== 'function' && key !== 'previousStates') {
|
if (typeof service.state[key] !== 'function' && key !== 'previousStates') {
|
||||||
|
|
@ -65,22 +70,13 @@ angular
|
||||||
return currentState;
|
return currentState;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears all previous state
|
|
||||||
*/
|
|
||||||
function startSend(params) {
|
|
||||||
console.log('startSend()');
|
|
||||||
clear();
|
|
||||||
map(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
function map(params) {
|
function map(params) {
|
||||||
Object.keys(params).forEach(function forNewParam(key) {
|
Object.keys(params).forEach(function forNewParam(key) {
|
||||||
service.state[key] = params[key];
|
service.state[key] = params[key];
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function popState() {
|
function pop() {
|
||||||
console.log('sendFlow pop');
|
console.log('sendFlow pop');
|
||||||
if (service.previousStates.length) {
|
if (service.previousStates.length) {
|
||||||
var params = service.previousStates.pop();
|
var params = service.previousStates.pop();
|
||||||
|
|
@ -91,9 +87,9 @@ angular
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function pushState(params) {
|
function push(params) {
|
||||||
console.log('sendFlow push');
|
console.log('sendFlow push');
|
||||||
var currentParams = getStateClone();
|
var currentParams = getClone();
|
||||||
service.previousStates.push(currentParams);
|
service.previousStates.push(currentParams);
|
||||||
clearCurrent();
|
clearCurrent();
|
||||||
map(params);
|
map(params);
|
||||||
129
src/js/services/send-flow.service.js
Normal file
129
src/js/services/send-flow.service.js
Normal 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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
||||||
Loading…
Add table
Add a link
Reference in a new issue