Wallet/src/js/controllers/review.controller.js

144 lines
4.4 KiB
JavaScript
Raw Normal View History

'use strict';
angular
.module('copayApp.controllers')
.controller('reviewController', reviewController);
2018-08-02 09:48:12 +12:00
function reviewController(configService, gettextCatalog, profileService, $scope, txFormatService) {
var vm = this;
2018-08-01 14:28:26 +12:00
2018-08-02 09:48:12 +12:00
vm.origin = {
balanceAmount: '',
balanceCurrency: '',
color: '',
currency: '',
2018-08-02 10:07:25 +12:00
currencyColor: '',
2018-08-02 09:48:12 +12:00
name: '',
};
2018-08-01 17:17:34 +12:00
vm.primaryAmount = '';
2018-08-01 14:28:26 +12:00
vm.primaryCurrency = '';
vm.secondaryAmount = '';
vm.secondaryCurrency = '';
2018-08-01 17:17:34 +12:00
var coin = '';
2018-08-02 09:48:12 +12:00
var originWalletId = '';
var priceDisplayIsFiat = true;
2018-08-01 17:17:34 +12:00
var satoshis = null;
2018-08-02 09:48:12 +12:00
var toAddress = '';
var toWalletId = '';
2018-08-01 17:17:34 +12:00
2018-08-02 09:48:12 +12:00
2018-08-01 14:28:26 +12:00
$scope.$on("$ionicView.beforeEnter", onBeforeEnter);
function onBeforeEnter(event, data) {
2018-08-01 17:17:34 +12:00
coin = data.stateParams.coin;
2018-08-02 09:48:12 +12:00
originWalletId = data.stateParams.fromWalletId;
satoshis = parseInt(data.stateParams.amount, 10);
toAddress = data.stateParams.toAddress;
var originWallet = profileService.getWallet(originWalletId);
vm.origin.currency = originWallet.coin.toUpperCase();
vm.origin.color = originWallet.color;
vm.origin.name = originWallet.name;
2018-08-01 14:28:26 +12:00
2018-08-02 10:07:25 +12:00
configService.get(function onConfig(err, config) {
2018-08-01 14:28:26 +12:00
if (err) {
$log.err('Error getting config.', err);
} else {
console.log('Got config.');
2018-08-02 09:48:12 +12:00
//config = configCache;
2018-08-01 14:28:26 +12:00
// Use this later if have time
2018-08-02 10:07:25 +12:00
priceDisplayIsFiat = config.wallet.settings.priceDisplay === 'fiat';
vm.origin.currencyColor = originWallet.coin === 'btc' ? config.bitcoinWalletColor : config.bitcoinCashWalletColor;
2018-08-01 14:28:26 +12:00
}
2018-08-02 09:48:12 +12:00
updateSendAmounts();
getOriginWalletBalance(originWallet);
2018-08-01 14:28:26 +12:00
});
}
2018-08-02 09:48:12 +12:00
function getOriginWalletBalance(originWallet) {
console.log('origin wallet error:', originWallet.error);
var balanceCryptoAmount = '';
var balanceCryptoCurrencyCode = '';
var balanceFiatAmount = '';
var balanceFiatCurrency = ''
var originWalletStatus = null;
if (originWallet.status.isValid) {
originWalletStatus = originWallet.status;
} else if (originWallet.cachedStatus.isValid) {
originWalletStatus = originWallet.cachedStatus;
} else {
vm.origin.balanceAmount = '';
vm.origin.balanceCurrency = '';
return;
}
if (originWalletStatus) {
var cryptoBalanceParts = originWalletStatus.spendableBalanceStr.split(' ');
balanceCryptoAmount = cryptoBalanceParts[0];
balanceCryptoCurrencyCode = cryptoBalanceParts.length > 1 ? cryptoBalanceParts[1] : '';
if (originWalletStatus.alternativeBalanceAvailable) {
balanceFiatAmount = originWalletStatus.spendableBalanceAlternative;
balanceFiatCurrency = originWalletStatus.alternativeIsoCode;
}
}
if (priceDisplayIsFiat) {
vm.origin.balanceAmount = balanceFiatAmount ? balanceFiatAmount : balanceCryptoAmount;
vm.origin.balanceCurrency = balanceFiatAmount ? balanceFiatCurrency : balanceCryptoCurrencyCode;
} else {
vm.origin.balanceAmount = balanceCryptoAmount;
vm.origin.balanceCurrency = balanceCryptoCurrencyCode;
}
}
function updateSendAmounts() {
2018-08-01 17:17:34 +12:00
if (typeof satoshis !== 'number') {
return;
}
2018-08-02 09:48:12 +12:00
var cryptoAmount = '';
var cryptoCurrencyCode = '';
2018-08-01 17:17:34 +12:00
var amountStr = txFormatService.formatAmountStr(coin, satoshis);
2018-08-02 09:48:12 +12:00
if (amountStr) {
var amountParts = amountStr.split(' ');
cryptoAmount = amountParts[0];
cryptoCurrencyCode = amountParts.length > 1 ? amountParts[1] : '';
}
// Want to avoid flashing of amount strings so do all formatting after this has returned.
2018-08-01 17:17:34 +12:00
txFormatService.formatAlternativeStr(coin, satoshis, function(v) {
if (!v) {
2018-08-02 09:48:12 +12:00
vm.primaryAmount = cryptoAmount;
vm.primaryCurrency = cryptoCurrencyCode;
2018-08-01 17:17:34 +12:00
vm.secondaryAmount = '';
vm.secondaryCurrency = '';
return;
}
vm.secondaryAmount = vm.primaryAmount;
vm.secondaryCurrency = vm.primaryCurrency;
var fiatParts = v.split(' ');
2018-08-02 09:48:12 +12:00
var fiatAmount = fiatParts[0];
var fiatCurrency = fiatParts.length > 1 ? fiatParts[1] : '';
if (priceDisplayIsFiat) {
vm.primaryAmount = fiatAmount;
vm.primaryCurrency = fiatCurrency;
vm.secondaryAmount = cryptoAmount;
vm.secondaryCurrency = cryptoCurrencyCode;
} else {
vm.primaryAmount = cryptoAmount;
vm.primaryCurrency = cryptoCurrencyCode;
vm.secondaryAmount = fiatAmount;
vm.secondaryCurrency = fiatCurrency;
}
2018-08-01 17:17:34 +12:00
});
}
}