cash support in amount.js

This commit is contained in:
Matias Alejo Garcia 2017-08-29 10:52:26 -03:00 committed by Gustavo Maximiliano Cortez
commit c21afeca30
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
6 changed files with 170 additions and 36 deletions

View file

@ -14,6 +14,10 @@ angular.module('copayApp.controllers').controller('advancedSettingsController',
$scope.hideNextSteps = { $scope.hideNextSteps = {
value: config.hideNextSteps.enabled value: config.hideNextSteps.enabled
}; };
$scope.cashSupport = {
value: config.cashSupport.enabled
};
}; };
$scope.spendUnconfirmedChange = function() { $scope.spendUnconfirmedChange = function() {
@ -27,6 +31,19 @@ angular.module('copayApp.controllers').controller('advancedSettingsController',
}); });
}; };
$scope.cashSupportChange = function() {
var opts = {
cashSupport: {
enabled: $scope.cashSupport.value
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
};
$scope.nextStepsChange = function() { $scope.nextStepsChange = function() {
var opts = { var opts = {
hideNextSteps: { hideNextSteps: {

View file

@ -9,6 +9,12 @@ angular.module('copayApp.controllers').controller('amountController', function($
var SMALL_FONT_SIZE_LIMIT = 10; var SMALL_FONT_SIZE_LIMIT = 10;
var LENGTH_EXPRESSION_LIMIT = 19; var LENGTH_EXPRESSION_LIMIT = 19;
var isNW = platformInfo.isNW; var isNW = platformInfo.isNW;
var unitIndex = 0;
var altUnitIndex = 0;
var availableUnits = [];
var fiatCode;
$scope.isChromeApp = platformInfo.isChromeApp; $scope.isChromeApp = platformInfo.isChromeApp;
$scope.$on('$ionicView.leave', function() { $scope.$on('$ionicView.leave', function() {
@ -16,13 +22,71 @@ angular.module('copayApp.controllers').controller('amountController', function($
}); });
$scope.$on("$ionicView.beforeEnter", function(event, data) { $scope.$on("$ionicView.beforeEnter", function(event, data) {
var config = configService.getSync().wallet.settings;
function setAvailableUnits() {
// TODO: Depends on the available wallets
// also, depends on forceCurrency & forceCoin
// Take this from somewhere elase
availableUnits = [{
name: 'Bitcoin',
id: 'btc',
shortName: 'BTC',
}];
var anyCashWallet = true; // TODO!!
if (anyCashWallet) {
availableUnits.push({
name: 'Bitcoin Cash',
id: 'bch',
shortName: 'BCH',
});
};
var fiat;
if (data.stateParams.currency) {
fiat = data.stateParams.currency;
} else {
fiat = config.fiat || 'USD';
}
availableUnits.push({
name: fiat, // TODO
id: fiat,
shortName: fiat,
isFiat: true,
});
unitIndex = 0;
altUnitIndex = availableUnits.length - 1;
fiatCode = fiat;
};
// Go to... // Go to...
_id = data.stateParams.id; // Optional (BitPay Card ID or Wallet ID) _id = data.stateParams.id; // Optional (BitPay Card ID or Wallet ID)
$scope.nextStep = data.stateParams.nextStep; $scope.nextStep = data.stateParams.nextStep;
// TODO
$scope.currency = data.stateParams.currency; $scope.currency = data.stateParams.currency;
$scope.forceCurrency = data.stateParams.forceCurrency; $scope.forceCurrency = data.stateParams.forceCurrency;
$scope.forceCoin = data.stateParams.forceCoin; $scope.forceCoin = data.stateParams.forceCoin;
$scope.coin = data.stateParams.coin || 'btc';
// TODO
// if (data.stateParams.coin) {
// unitIndex = lodash.indexOf(data.stateParams.coin.toUpperCase());
// if (unitIndex < 0) {
// $log.warn('Could not find desired coin:' + data.stateParams.coin)
// unitIndex = 0;
// }
// }
setAvailableUnits();
updateUnitUI();
$scope.showMenu = $ionicHistory.backView() && ($ionicHistory.backView().stateName == 'tabs.send' || $scope.showMenu = $ionicHistory.backView() && ($ionicHistory.backView().stateName == 'tabs.send' ||
$ionicHistory.backView().stateName == 'tabs.bitpayCard'); $ionicHistory.backView().stateName == 'tabs.bitpayCard');
@ -30,7 +94,6 @@ angular.module('copayApp.controllers').controller('amountController', function($
$scope.toAddress = data.stateParams.toAddress; $scope.toAddress = data.stateParams.toAddress;
$scope.toName = data.stateParams.toName; $scope.toName = data.stateParams.toName;
$scope.toEmail = data.stateParams.toEmail; $scope.toEmail = data.stateParams.toEmail;
$scope.showAlternativeAmount = !!$scope.nextStep;
$scope.toColor = data.stateParams.toColor; $scope.toColor = data.stateParams.toColor;
$scope.showSendMax = false; $scope.showSendMax = false;
@ -63,13 +126,6 @@ angular.module('copayApp.controllers').controller('amountController', function($
$scope.$apply(); $scope.$apply();
}); });
}); });
var config = configService.getSync().wallet.settings;
if (data.stateParams.currency) {
$scope.alternativeIsoCode = data.stateParams.currency;
} else {
$scope.alternativeIsoCode = config.alternativeIsoCode || 'USD';
}
$scope.specificAmount = $scope.specificAlternativeAmount = ''; $scope.specificAmount = $scope.specificAlternativeAmount = '';
$scope.isCordova = platformInfo.isCordova; $scope.isCordova = platformInfo.isCordova;
unitToSatoshi = config.unitToSatoshi; unitToSatoshi = config.unitToSatoshi;
@ -115,6 +171,8 @@ angular.module('copayApp.controllers').controller('amountController', function($
$scope.finish(); $scope.finish();
}; };
// TODO
$scope.toggleAlternative = function() { $scope.toggleAlternative = function() {
if ($scope.forceCurrency) return; if ($scope.forceCurrency) return;
$scope.showAlternativeAmount = !$scope.showAlternativeAmount; $scope.showAlternativeAmount = !$scope.showAlternativeAmount;
@ -125,10 +183,52 @@ angular.module('copayApp.controllers').controller('amountController', function($
} }
}; };
$scope.toggleCoin = function() { function updateUnitUI() {
if ($scope.forceCurrency || $scope.forceCoin) return;
$scope.coin = $scope.coin == 'btc' ? 'bch' : 'btc'; $scope.unit = availableUnits[unitIndex].shortName;
$scope.alternativeUnit = availableUnits[altUnitIndex].shortName;
processAmount();
$log.debug('Update unit coin @amount unit:' + $scope.unit + " alternativeUnit:" + $scope.alternativeUnit); //TODO
};
$scope.changeUnit = function() {
// TODO
// if ($scope.forceCurrency || $scope.forceCoin) return;
unitIndex++;
if (unitIndex >= availableUnits.length) unitIndex = 0;
if (availableUnits[unitIndex].isFiat) {
// Always return to BTC... TODO?
altUnitIndex = 0;
} else {
altUnitIndex = lodash.findIndex(availableUnits, {
isFiat: true
});
}
updateUnitUI();
};
$scope.changeAlternativeUnit = function() {
console.log('[amount.js.215:changeAlternativeUnit:]'); //TODO
// Do nothing is fiat is not main unit
if (!availableUnits[unitIndex].isFiat) return;
var nextCoin = lodash.findIndex(availableUnits, function(x) {
if (x.isFiat) return false;
if (x.id == availableUnits[altUnitIndex].id) return false;
return true;
});
if (nextCoin >= 0) {
altUnitIndex = nextCoin;
updateUnitUI();
}
}; };
function checkFontSize() { function checkFontSize() {
@ -139,7 +239,7 @@ angular.module('copayApp.controllers').controller('amountController', function($
$scope.pushDigit = function(digit) { $scope.pushDigit = function(digit) {
if ($scope.amount && $scope.amount.length >= LENGTH_EXPRESSION_LIMIT) return; if ($scope.amount && $scope.amount.length >= LENGTH_EXPRESSION_LIMIT) return;
if ($scope.amount.indexOf('.') > -1 && digit == '.') return; if ($scope.amount.indexOf('.') > -1 && digit == '.') return;
if ($scope.showAlternativeAmount && $scope.amount.indexOf('.') > -1 && $scope.amount[$scope.amount.indexOf('.') + 2]) return; if (availableUnits[unitIndex].isFiat && $scope.amount.indexOf('.') > -1 && $scope.amount[$scope.amount.indexOf('.') + 2]) return;
$scope.amount = ($scope.amount + digit).replace('..', '.'); $scope.amount = ($scope.amount + digit).replace('..', '.');
checkFontSize(); checkFontSize();
@ -176,7 +276,7 @@ angular.module('copayApp.controllers').controller('amountController', function($
}; };
$scope.resetAmount = function() { $scope.resetAmount = function() {
$scope.amount = $scope.alternativeResult = $scope.amountResult = $scope.globalResult = ''; $scope.amount = $scope.alternativeAmount = $scope.globalResult = '';
$scope.allowSend = false; $scope.allowSend = false;
checkFontSize(); checkFontSize();
}; };
@ -187,24 +287,28 @@ angular.module('copayApp.controllers').controller('amountController', function($
$scope.allowSend = lodash.isNumber(result) && +result > 0; $scope.allowSend = lodash.isNumber(result) && +result > 0;
if (lodash.isNumber(result)) { if (lodash.isNumber(result)) {
$scope.globalResult = isExpression($scope.amount) ? '= ' + processResult(result) : ''; $scope.globalResult = isExpression($scope.amount) ? '= ' + processResult(result) : '';
$scope.amountResult = $filter('formatFiatAmount')(toFiat(result));
$scope.alternativeResult = txFormatService.formatAmount(fromFiat(result) * unitToSatoshi, true); if (availableUnits[unitIndex].isFiat) {
$scope.alternativeAmount = txFormatService.formatAmount(fromFiat(result) * unitToSatoshi, true);
} else {
$scope.alternativeAmount = $filter('formatFiatAmount')(toFiat(result));
}
} }
}; };
function processResult(val) { function processResult(val) {
if ($scope.showAlternativeAmount) if (availableUnits[unitIndex].isFiat)
return $filter('formatFiatAmount')(val); return $filter('formatFiatAmount')(val);
else else
return txFormatService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true); return txFormatService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true);
}; };
function fromFiat(val) { function fromFiat(val) {
return parseFloat((rateService.fromFiat(val, $scope.alternativeIsoCode, $scope.coin) * satToUnit).toFixed(unitDecimals)); return parseFloat((rateService.fromFiat(val, fiatCode, availableUnits[altUnitIndex].id) * satToUnit).toFixed(unitDecimals));
}; };
function toFiat(val) { function toFiat(val) {
return parseFloat((rateService.toFiat(val * unitToSatoshi, $scope.alternativeIsoCode, $scope.coin)).toFixed(2)); return parseFloat((rateService.toFiat(val * unitToSatoshi, fiatCode, availableUnits[unitIndex].id)).toFixed(2));
}; };
function evaluate(val) { function evaluate(val) {
@ -219,6 +323,8 @@ angular.module('copayApp.controllers').controller('amountController', function($
}; };
function format(val) { function format(val) {
if (!val) return;
var result = val.toString(); var result = val.toString();
if (isOperator(lodash.last(val))) if (isOperator(lodash.last(val)))
@ -234,8 +340,9 @@ angular.module('copayApp.controllers').controller('amountController', function($
$state.transitionTo($scope.nextStep, { $state.transitionTo($scope.nextStep, {
id: _id, id: _id,
amount: $scope.useSendMax ? null : _amount, amount: $scope.useSendMax ? null : _amount,
currency: $scope.showAlternativeAmount ? $scope.alternativeIsoCode : ($scope.coin).toUpperCase(), // TODO
coin: $scope.coin, currency: $scope.showAlternativeAmount ? fiatCode : (coin).toUpperCase(),
coin: coin,
useSendMax: $scope.useSendMax useSendMax: $scope.useSendMax
}); });
} else { } else {
@ -247,7 +354,7 @@ angular.module('copayApp.controllers').controller('amountController', function($
toName: $scope.toName, toName: $scope.toName,
toEmail: $scope.toEmail, toEmail: $scope.toEmail,
toColor: $scope.toColor, toColor: $scope.toColor,
coin: $scope.coin, coin: coin,
useSendMax: $scope.useSendMax useSendMax: $scope.useSendMax
}); });
} }

View file

@ -61,7 +61,10 @@ angular.module('copayApp.services').factory('configService', function(storageSer
bannedUntil: null, bannedUntil: null,
}, },
// External services cashSupport: {
enabled: true,
},
recentTransactions: { recentTransactions: {
enabled: true, enabled: true,
}, },
@ -133,6 +136,11 @@ angular.module('copayApp.services').factory('configService', function(storageSer
configCache.hideNextSteps = defaultConfig.hideNextSteps; configCache.hideNextSteps = defaultConfig.hideNextSteps;
} }
if (!configCache.cashSupport) {
configCache.cashSupport = defaultConfig.cashSupport;
}
if (!configCache.recentTransactions) { if (!configCache.recentTransactions) {
configCache.recentTransactions = defaultConfig.recentTransactions; configCache.recentTransactions = defaultConfig.recentTransactions;
} }

View file

@ -200,7 +200,7 @@
&__result { &__result {
color: $v-light-gray; color: $v-light-gray;
font-size: .9em; font-size: .9em;
margin-bottom: -.9em; //margin-bottom: -.9em; TODO matias
line-height: 1; line-height: 1;
@media(max-height: 480px) { @media(max-height: 480px) {
margin-bottom: 0; margin-bottom: 0;

View file

@ -7,6 +7,16 @@
<ion-content> <ion-content>
<div class="settings-list list"> <div class="settings-list list">
<ion-toggle class="has-comment" ng-model="cashSupport.value" toggle-class="toggle-balanced" ng-change="cashSupportChange()">
<span class="toggle-label" translate>Support Bitcoin Cash</span>
</ion-toggle>
<div class="comment" translate>
Enable Bitcoin Cash wallet creation and operation within the App. Learn more about Bitcoin Cash at <a href="https://www.bitcoincash.org/">its main website</a>
</div>
<ion-toggle class="has-comment" ng-model="spendUnconfirmed.value" toggle-class="toggle-balanced" ng-change="spendUnconfirmedChange()"> <ion-toggle class="has-comment" ng-model="spendUnconfirmed.value" toggle-class="toggle-balanced" ng-change="spendUnconfirmedChange()">
<span class="toggle-label" translate>Use Unconfirmed Funds</span> <span class="toggle-label" translate>Use Unconfirmed Funds</span>
</ion-toggle> </ion-toggle>

View file

@ -48,19 +48,11 @@
<div class="amount"> <div class="amount">
<div class="amount__editable" ng-class="{'amount__editable--minimize': smallFont, 'amount__editable--standard': !smallFont, 'amount__editable--placeholder': !amount}"> <div class="amount__editable" ng-class="{'amount__editable--minimize': smallFont, 'amount__editable--standard': !smallFont, 'amount__editable--placeholder': !amount}">
<span class="amount__number">{{amount || "0.00" }}</span> <span class="amount__number">{{amount || "0.00" }}</span>
<a class="amount__currency-toggle" ng-show="showAlternativeAmount">{{alternativeIsoCode}}</a> <a class="amount__currency-toggle" ng-click="changeUnit()">{{unit}}</a>
<a class="amount__currency-toggle" ng-click="toggleCoin()" ng-show="!showAlternativeAmount">{{coin}}</a>
</div> </div>
<div class="icon-toggle" ng-if="!forceCurrency" ng-click="toggleAlternative()"> <div ng-class="{'amount__results--minimize': smallFont, 'amount__results--standard': !smallFont, 'amount__results--placeholder': amountResult}" ng-click="changeAlternativeUnit()">
<i class="icon ion-arrow-swap"></i> <div class="amount__result" ng-show="globalResult">{{globalResult}} {{unit}}</div>
</div> <div class="amount__result-equiv">&asymp; {{alternativeAmount || '0.00'}} {{alternativeUnit}}</div>
<div ng-class="{'amount__results--minimize': smallFont, 'amount__results--standard': !smallFont, 'amount__results--placeholder': !amountResult}" ng-show="!showAlternativeAmount">
<div class="amount__result" ng-show="globalResult">{{globalResult}} {{coin}}</div>
<div class="amount__result-equiv">&asymp; {{amountResult || '0.00'}} {{alternativeIsoCode}}</div>
</div>
<div ng-class="{'amount__results--minimize': smallFont, 'amount__results--standard': !smallFont, 'amount__results--placeholder': !amountResult}" ng-show="showAlternativeAmount">
<div class="amount__result" ng-show="globalResult">{{globalResult}} {{alternativeIsoCode}}</div>
<div class="amount__result-equiv">{{alternativeResult || '0.00'}} {{coin}}</div>
</div> </div>
</div> </div>
</div> </div>