This commit is contained in:
Matias Alejo Garcia 2016-08-16 18:38:18 -03:00
commit b5023ae9e7
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
10 changed files with 547 additions and 251 deletions

View file

@ -1,6 +1,8 @@
'use strict';
angular.module('copayApp.controllers').controller('inputAmountController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, profileService, platformInfo, lodash, configService, go, rateService) {
angular.module('copayApp.controllers').controller('amountController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, walletService, platformInfo, lodash, configService, go, rateService, $stateParams, $window, $state, $log) {
var unitToSatoshi;
var satToUnit;
var unitDecimals;
@ -10,6 +12,45 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
var LENGTH_EXPRESSION_LIMIT = 19;
$scope.init = function() {
if (!$stateParams.toAddress) {
$log.error('Bad params at amount')
throw ('bad params');
}
var reNr = /^[1234567890\.]$/;
var reOp = /^[\*\+\-\/]$/;
var disableKeys = angular.element($window).on('keydown', function(e) {
if (e.which === 8) { // you can add others here inside brackets.
e.preventDefault();
$scope.removeDigit();
}
if (e.key && e.key.match(reNr))
$scope.pushDigit(e.key);
else if (e.key && e.key.match(reOp))
$scope.pushOperator(e.key);
else if (e.key && e.key == 'Enter')
$scope.finish();
$timeout(function() {
$scope.$apply();
}, 10);
});
$scope.$on('$destroy', function() {
angular.element($window).off('keydown');
});
$scope.toAddress = $stateParams.toAddress;
$scope.toName = $stateParams.toName;
var config = configService.getSync().wallet.settings;
$scope.unitName = config.unitName;
$scope.alternativeIsoCode = config.alternativeIsoCode;
@ -19,18 +60,20 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
satToUnit = 1 / unitToSatoshi;
satToBtc = 1 / 100000000;
unitDecimals = config.unitDecimals;
// in SAT ALWAYS
if ($stateParams.toAmount) {
$scope.amount = (($stateParams.toAmount) * satToUnit).toFixed(unitDecimals) ;
}
processAmount($scope.amount);
$timeout(function() {
$ionicScrollDelegate.resize();
}, 100);
};
$scope.shareAddress = function(uri) {
if ($scope.isCordova) {
window.plugins.socialsharing.share(uri, null, null, null);
}
};
$scope.toggleAlternative = function() {
$scope.showAlternativeAmount = !$scope.showAlternativeAmount;
@ -54,6 +97,7 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
};
$scope.pushOperator = function(operator) {
console.log('[amount.js.90:operator:]', operator); //TODO
if (!$scope.amount || $scope.amount.length == 0) return;
$scope.amount = _pushOperator($scope.amount);
@ -100,7 +144,7 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
if (lodash.isNumber(result)) {
$scope.globalResult = isExpression(val) ? '= ' + processResult(result) : '';
$scope.amountResult = $filter('formatFiatAmount')(toFiat(result));
$scope.alternativeResult = profileService.formatAmount(fromFiat(result) * unitToSatoshi, true);
$scope.alternativeResult = walletService.formatAmount(fromFiat(result) * unitToSatoshi, true);
}
};
@ -108,7 +152,7 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
if ($scope.showAlternativeAmount)
return $filter('formatFiatAmount')(val);
else
return profileService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true);
return walletService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true);
};
function fromFiat(val) {
@ -142,31 +186,15 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
$scope.finish = function() {
var _amount = evaluate(format($scope.amount));
var amount = $scope.showAlternativeAmount ? fromFiat(_amount).toFixed(unitDecimals) : _amount.toFixed(unitDecimals);
var alternativeAmount = $scope.showAlternativeAmount ? _amount : toFiat(_amount);
if (amount % 1 == 0) amount = parseInt(amount);
if ($scope.addr) {
$scope.specificAmount = profileService.formatAmount(amount * unitToSatoshi, true);
$scope.specificAlternativeAmount = $filter('formatFiatAmount')(alternativeAmount);
if ($scope.unitName == 'bits') {
var amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
amount = (amountSat * satToBtc).toFixed(8);
}
$scope.customizedAmountBtc = amount;
$timeout(function() {
$ionicScrollDelegate.resize();
}, 100);
} else {
$scope.sending = true;
$scope.sendingAmount = profileService.formatAmount(amount * unitToSatoshi, true);
$scope.sendingAlternativeAmount = $filter('formatFiatAmount')(alternativeAmount);
}
$state.transitionTo('confirm', {
toAmount:amount * unitToSatoshi,
toAddress: $scope.toAddress,
toName: $scope.toName,
});
};
$scope.cancel = function() {
$scope.inputAmountModal.hide();
$state.transitionTo('tabs.send');
};
});

View file

@ -0,0 +1,172 @@
'use strict';
angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, walletService, platformInfo, lodash, configService, go, rateService, $stateParams, $window, $state, $log, profileService, bitcore) {
var unitToSatoshi;
var satToUnit;
var unitDecimals;
var satToBtc;
var self = $scope.self;
var SMALL_FONT_SIZE_LIMIT = 13;
var LENGTH_EXPRESSION_LIMIT = 19;
$scope.init = function() {
console.log('[confirm.js.23:$scope:]',$stateParams); //TODO
// TODO (URL , etc)
if (!$stateParams.toAddress || !$stateParams.toAmount) {
$log.error('Bad params at amount')
throw ('bad params');
}
$scope.isCordova = platformInfo.isCordova;
var config = configService.getSync().wallet.settings;
$scope.unitName = config.unitName;
$scope.alternativeIsoCode = config.alternativeIsoCode;
unitToSatoshi = config.unitToSatoshi;
satToUnit = 1 / unitToSatoshi;
satToBtc = 1 / 100000000;
$scope.toAmount = $stateParams.toAmount;
$scope.amount = (($stateParams.toAmount) * satToUnit).toFixed(unitDecimals) ;
$scope.toAddress = $stateParams.toAddress;
$scope.toName = $stateParams.toName;
var network = (new bitcore.Address($scope.toAddress)).network.name;
$scope.setWallets(network);
$scope.alternativeAmount = toFiat($scope.toAmount);
unitDecimals = config.unitDecimals;
$timeout(function() {
$ionicScrollDelegate.resize();
}, 100);
};
var send = function() {
if (!$scope._amount || !$scope._address) return;
var unitToSat = this.unitToSatoshi;
var currentSpendUnconfirmed = configWallet.spendUnconfirmed;
var outputs = [];
this.resetError();
if (isCordova && this.isWindowsPhoneApp)
$rootScope.shouldHideMenuBar = true;
var form = $scope.sendForm;
var comment = form.comment.$modelValue;
// ToDo: use a credential's (or fc's) function for this
if (comment && !client.credentials.sharedEncryptingKey) {
var msg = 'Could not add message to imported wallet without shared encrypting key';
$log.warn(msg);
return self.setSendError(gettext(msg));
}
if (form.amount.$modelValue * unitToSat > Number.MAX_SAFE_INTEGER) {
var msg = 'Amount too big';
$log.warn(msg);
return self.setSendError(gettext(msg));
};
$timeout(function() {
var paypro = self._paypro;
var address, amount;
address = form.address.$modelValue;
amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0));
outputs.push({
'toAddress': address,
'amount': amount,
'message': comment
});
var txp = {};
if (!lodash.isEmpty(self.sendMaxInfo)) {
txp.sendMax = true;
txp.inputs = self.sendMaxInfo.inputs;
txp.fee = self.sendMaxInfo.fee;
} else {
txp.amount = amount;
}
txp.toAddress = address;
txp.outputs = outputs;
txp.message = comment;
txp.payProUrl = paypro ? paypro.url : null;
txp.excludeUnconfirmedUtxos = configWallet.spendUnconfirmed ? false : true;
txp.feeLevel = walletSettings.feeLevel || 'normal';
ongoingProcess.set('creatingTx', true);
walletService.createTx(client, txp, function(err, createdTxp) {
ongoingProcess.set('creatingTx', false);
if (err) {
return self.setSendError(err);
}
if (!client.canSign() && !client.isPrivKeyExternal()) {
$log.info('No signing proposal: No private key');
ongoingProcess.set('sendingTx', true);
walletService.publishTx(client, createdTxp, function(err, publishedTxp) {
ongoingProcess.set('sendingTx', false);
if (err) {
return self.setSendError(err);
}
self.resetForm();
go.walletHome();
var type = txStatus.notify(createdTxp);
$scope.openStatusModal(type, createdTxp, function() {
return $scope.$emit('Local/TxProposalAction');
});
});
} else {
$rootScope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
if (accept) self.confirmTx(createdTxp);
else self.resetForm();
});
}
});
}, 100);
};
function fromFiat(val) {
return parseFloat((rateService.fromFiat(val, $scope.alternativeIsoCode) * satToUnit).toFixed(unitDecimals), 10);
};
function toFiat(val) {
return parseFloat((rateService.toFiat(val * unitToSatoshi, $scope.alternativeIsoCode)).toFixed(2), 10);
};
$scope.finish = function() {
var _amount = evaluate(format($scope.amount));
var amount = $scope.showAlternativeAmount ? fromFiat(_amount).toFixed(unitDecimals) : _amount.toFixed(unitDecimals);
$state.transitionTo('confirm', {
toAmount:walletService.formatAmount(amount * unitToSatoshi, true),
toAddress: $scope.toAddress,
toName: $scope.toName,
});
};
$scope.cancel = function() {
$state.transitionTo('tabs.send');
};
$scope.setWallets = function(network) {
$scope.wallets = profileService.getWallets({onlyComplete:true, network: network});
};
});

View file

@ -0,0 +1,72 @@
//
// self.startSearch = function() {
// self.isSearching = true;
// self.txHistorySearchResults = [];
// self.result = [];
// self.historyShowMore = false;
// self.nextTxHistory = self.historyShowMoreLimit;
// }
//
// self.cancelSearch = function() {
// self.isSearching = false;
// self.result = [];
// self.setCompactTxHistory();
// }
//
// self.updateSearchInput = function(search) {
// self.search = search;
// if (isCordova)
// window.plugins.toast.hide();
// self.throttleSearch();
// $ionicScrollDelegate.resize();
// }
//
// self.throttleSearch = lodash.throttle(function() {
//
// function filter(search) {
// self.result = [];
//
// function computeSearchableString(tx) {
// var addrbook = '';
// if (tx.addressTo && self.addressbook && self.addressbook[tx.addressTo]) addrbook = self.addressbook[tx.addressTo] || '';
// var searchableDate = computeSearchableDate(new Date(tx.time * 1000));
// var message = tx.message ? tx.message : '';
// var comment = tx.note ? tx.note.body : '';
// var addressTo = tx.addressTo ? tx.addressTo : '';
// return ((tx.amountStr + message + addressTo + addrbook + searchableDate + comment).toString()).toLowerCase();
// }
//
// function computeSearchableDate(date) {
// var day = ('0' + date.getDate()).slice(-2).toString();
// var month = ('0' + (date.getMonth() + 1)).slice(-2).toString();
// var year = date.getFullYear();
// return [month, day, year].join('/');
// };
//
// if (lodash.isEmpty(search)) {
// self.historyShowMore = false;
// return [];
// }
// self.result = lodash.filter(self.completeHistory, function(tx) {
// if (!tx.searcheableString) tx.searcheableString = computeSearchableString(tx);
// return lodash.includes(tx.searcheableString, search.toLowerCase());
// });
//
// if (self.result.length > self.historyShowLimit) self.historyShowMore = true;
// else self.historyShowMore = false;
//
// return self.result;
// };
//
// self.txHistorySearchResults = filter(self.search).slice(0, self.historyShowLimit);
// if (isCordova)
// window.plugins.toast.showShortBottom(gettextCatalog.getString('Matches: ' + self.result.length));
//
// $timeout(function() {
// $rootScope.$apply();
// });
//
// }, 1000);
//

View file

@ -1,21 +1,25 @@
'use strict';
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $ionicModal, $log, $timeout, addressbookService, profileService, configService, lodash) {
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $ionicModal, $log, $timeout, addressbookService, profileService, configService, lodash, $state, walletService) {
var originalList = [];
$scope.search = '';
$scope.init = function() {
var wallets = profileService.getWallets();
var wallets = profileService.getWallets({onlyComplete: true});
lodash.each(wallets, function(v) {
originalList.push({
color: v.color,
label: v.name,
isWallet: true,
getAddress: function(cb) {
console.log('[tab-send.js.20] get ADDRESS at wallet!!!', v.name); //TODO
walletService.getAddress(v, false, cb);
},
});
});
@ -26,7 +30,9 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
lodash.each(ab, function(v, k) {
contacts.push({
label: k,
address: v,
getAddress: function(cb) {
return cb(null,v);
},
});
});
@ -37,11 +43,11 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
$scope.findContact = function() {
if (!$scope.search || $scope.search.length<2){
if (!$scope.search || $scope.search.length < 2) {
$scope.list = originalList;
$timeout(function() {
$scope.$apply();
},10);
}, 10);
return;
}
@ -53,15 +59,14 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
$scope.list = result;
};
$scope.openInputAmountModal = function(recipient) {
$scope.recipientName = recipient.name || recipient.label;
$scope.recipientColor = recipient.color;
$ionicModal.fromTemplateUrl('views/modals/inputAmount.html', {
scope: $scope
}).then(function(modal) {
$scope.inputAmountModal = modal;
$scope.inputAmountModal.show();
$scope.goToAmount = function(item) {
item.getAddress(function(err,addr){
if (err|| !addr) {
$log.error(err);
return;
}
$log.debug('Got toAddress:' + addr + ' | ' + item.label)
return $state.transitionTo('amount', { toAddress: addr, toName: item.label})
});
};
});

View file

@ -14,7 +14,7 @@ if (window && window.navigator) {
//Setting up route
angular.module('copayApp').config(function(historicLogProvider, $provide, $logProvider, $stateProvider, $urlRouterProvider, $compileProvider) {
$urlRouterProvider.otherwise('/tabs');
$urlRouterProvider.otherwise('/tabs.home');
$logProvider.debugEnabled(true);
$provide.decorator('$log', ['$delegate', 'platformInfo',
@ -116,6 +116,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
})
.state('tabs', {
url: '/tabs',
cache: false,
needProfile: true,
abstract: true,
views: {
@ -126,6 +127,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
})
.state('tabs.home', {
url: '/home',
cache: false,
needProfile: true,
views: {
'tab-home': {
@ -135,6 +137,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
})
.state('tabs.receive', {
url: '/receive',
cache: false,
needProfile: true,
views: {
'tab-receive': {
@ -144,6 +147,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
})
.state('tabs.send', {
url: '/send',
cache: false,
needProfile: true,
views: {
'tab-send': {
@ -160,15 +164,36 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
},
}
})
.state('feedback', {
url: '/feedback',
.state('amount', {
cache: false,
url: '/amount',
needProfile: true,
views: {
'main': {
templateUrl: 'views/feedback.html',
templateUrl: 'views/amount.html',
},
}
},
params: {
toAddress: null,
toName: null,
},
})
.state('confirm', {
cache: false,
url: '/confirm',
needProfile: true,
views: {
'main': {
templateUrl: 'views/confirm.html',
},
},
params: {
toAddress: null,
toName: null,
toAmount: null,
},
})
.state('unsupported', {
url: '/unsupported',
needProfile: false,
@ -701,6 +726,29 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
navigator.splashscreen.hide();
}, 1000);
}
$log.info('Init profile...');
// Try to open local profile
profileService.loadAndBindProfile(function(err) {
if (err) {
if (err.message && err.message.match('NOPROFILE')) {
$log.debug('No profile... redirecting');
$state.transitionTo('disclaimer');
} else if (err.message && err.message.match('NONAGREEDDISCLAIMER')) {
$log.debug('Display disclaimer... redirecting');
$state.transitionTo('disclaimer');
} else {
throw new Error(err); // TODO
}
} else {
profileService.storeProfileIfDirty();
$log.debug('Profile loaded ... Starting UX.');
$state.transitionTo('tabs.home');
}
});
});
uxLanguage.init();
@ -724,34 +772,5 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
$log.debug(' toParams:' + JSON.stringify(toParams || {}));
$log.debug(' fromParams:' + JSON.stringify(fromParams || {}));
if (!profileService.profile && toState.needProfile) {
// Give us time to open / create the profile
event.preventDefault();
// Try to open local profile
profileService.loadAndBindProfile(function(err) {
if (err) {
if (err.message && err.message.match('NOPROFILE')) {
$log.debug('No profile... redirecting');
$state.transitionTo('disclaimer');
} else if (err.message && err.message.match('NONAGREEDDISCLAIMER')) {
$log.debug('Display disclaimer... redirecting');
$state.transitionTo('disclaimer');
} else {
throw new Error(err); // TODO
}
} else {
profileService.storeProfileIfDirty();
$log.debug('Profile loaded ... Starting UX.');
$state.transitionTo(toState.name || toState, toParams);
}
});
}
// else {
// if (profileService.focusedClient && !profileService.focusedClient.isComplete() && toState.walletShouldBeComplete) {
//
// $state.transitionTo('copayers');
// }
// }
});
});