From ec56a7c044eaaee3c1ea2a2129d8dbe129bb1883 Mon Sep 17 00:00:00 2001 From: Javier Date: Mon, 8 Aug 2016 16:00:26 -0300 Subject: [PATCH] clean code --- public/views/modals/inputAmount.html | 19 +++---- src/js/controllers/modals/inputAmount.js | 64 +++++++++--------------- src/js/controllers/walletHome.js | 17 +++---- 3 files changed, 35 insertions(+), 65 deletions(-) diff --git a/public/views/modals/inputAmount.html b/public/views/modals/inputAmount.html index dc0086c0f..a43f55ab9 100644 --- a/public/views/modals/inputAmount.html +++ b/public/views/modals/inputAmount.html @@ -6,10 +6,13 @@

Enter amount

-
+
+
+ +
@@ -63,10 +66,10 @@

QR Code

  • - +
    + ng-click="shareAddress('bitcoin:' + addr + '?amount=' + specificAmount)"> Share address @@ -90,16 +93,6 @@
- -
-
- -
-
diff --git a/src/js/controllers/modals/inputAmount.js b/src/js/controllers/modals/inputAmount.js index 2d06a31cc..0058f34f8 100644 --- a/src/js/controllers/modals/inputAmount.js +++ b/src/js/controllers/modals/inputAmount.js @@ -5,6 +5,8 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct var satToUnit; var unitDecimals; var self = $scope.self; + var SMALL_FONT_SIZE_LIMIT = 13; + var LENGTH_EXPRESSION_LIMIT = 19; $scope.init = function() { var config = configService.getSync().wallet.settings; @@ -32,23 +34,20 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct if ($scope.amount && isExpression($scope.amount)) { var amount = evaluate(format($scope.amount)); - if ($scope.showAlternativeAmount) - $scope.globalResult = '= ' + $filter('formatFiatAmount')(amount); - else - $scope.globalResult = '= ' + profileService.formatAmount(amount * unitToSatoshi, true); + $scope.globalResult = '= ' + processResult(amount); } }; function checkFontSize() { - if ($scope.amount && $scope.amount.length >= 13) $scope.smallFont = true; + if ($scope.amount && $scope.amount.length >= SMALL_FONT_SIZE_LIMIT) $scope.smallFont = true; else $scope.smallFont = false; }; $scope.pushDigit = function(digit) { - checkFontSize(); - if ($scope.amount && $scope.amount.length >= 19) return; + if ($scope.amount && $scope.amount.length >= LENGTH_EXPRESSION_LIMIT) return; $scope.amount = ($scope.amount + digit).replace('..', '.'); + checkFontSize(); processAmount($scope.amount); }; @@ -67,25 +66,16 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct function isOperator(val) { var regex = /[\/\-\+\x\*]/; - var match = regex.exec(val); - if (match) return true; - return false; + return regex.test(val); }; function isExpression(val) { var regex = /^\.?\d+(\.?\d+)?([\/\-\+\*x]\d?\.?\d+)+$/; - var match = regex.exec(val); - if (match) return true; - return false; + return regex.test(val); }; $scope.removeDigit = function() { - if ($scope.amount.toString().length == 1) { - $scope.resetAmount(); - return; - } - $scope.amount = $scope.amount.slice(0, -1); processAmount($scope.amount); checkFontSize(); @@ -106,16 +96,19 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct var result = evaluate(formatedValue); if (lodash.isNumber(result)) { - if ($scope.showAlternativeAmount) - $scope.globalResult = isExpression(val) ? '= ' + $filter('formatFiatAmount')(result) : ''; - else - $scope.globalResult = isExpression(val) ? '= ' + profileService.formatAmount(result * unitToSatoshi, true) : ''; - + $scope.globalResult = isExpression(val) ? '= ' + processResult(result) : ''; $scope.amountResult = $filter('formatFiatAmount')(toFiat(result)); $scope.alternativeResult = profileService.formatAmount(fromFiat(result) * unitToSatoshi, true); } }; + function processResult(val) { + if ($scope.showAlternativeAmount) + return $filter('formatFiatAmount')(val); + else + return profileService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true); + }; + function fromFiat(val) { return parseFloat((rateService.fromFiat(val, $scope.alternativeIsoCode) * satToUnit).toFixed(unitDecimals), 10); }; @@ -131,7 +124,7 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct } catch (e) { return 0; } - if (result == 'Infinity' || lodash.isNaN(result)) return 0; + if (!lodash.isFinite(result)) return 0; return result; }; @@ -145,26 +138,15 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct }; $scope.finish = function() { - var amount; - var alternativeAmount; + var amount = evaluate(format($scope.amount)).toFixed(unitDecimals); + var alternativeAmount = fromFiat(amount).toFixed(unitDecimals); - if ($scope.showAlternativeAmount) { - amount = fromFiat($scope.globalResult); - alternativeAmount = profileService.formatAmount(evaluate(format($scope.amount)) * unitToSatoshi, true); - } else { - amount = profileService.formatAmount(evaluate(format($scope.amount)) * unitToSatoshi, true); - alternativeAmount = toFiat(amount); - } + if (amount % 1 == 0) amount = parseInt(amount); - if ($scope.address) { - var satToBtc = 1 / 100000000; - var amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); - if ($scope.unitName == 'bits') { - $scope.specificAmountBtc = (amountSat * satToBtc).toFixed(8); - } + if ($scope.addr) { + $scope.specificAmount = amount; + $scope.specificAlternativeAmount = $filter('formatFiatAmount')(toFiat(amount)); - $scope.specificAmount = profileService.formatAmount(amount * unitToSatoshi, true); - $scope.specificAlternativeAmount = $filter('formatFiatAmount')(alternativeAmount); $timeout(function() { $ionicScrollDelegate.resize(); }, 100); diff --git a/src/js/controllers/walletHome.js b/src/js/controllers/walletHome.js index 22f62ba29..1ef2263d1 100644 --- a/src/js/controllers/walletHome.js +++ b/src/js/controllers/walletHome.js @@ -348,14 +348,9 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi }; this.setAmount = function(amount, alternativeAmount, useAlternativeAmount) { + var amountResult = useAlternativeAmount ? alternativeAmount : amount; $scope.showAlternative = useAlternativeAmount; - var amountResult; - if (useAlternativeAmount) { - amountResult = parseFloat((rateService.fromFiat(alternativeAmount, self.alternativeIsoCode) * self.satToUnit).toFixed(self.unitDecimals), 10); - } else { - amountResult = amount; - } self.fromInputAmount = true; self.setForm(null, amountResult, null); }; @@ -543,7 +538,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi }); }; - $scope.openCustomAmountModal = function(addr) { + $scope.openCustomInputAmountModal = function(addr) { var fc = profileService.focusedClient; $scope.color = fc.backgroundColor; $scope.self = self; @@ -557,19 +552,19 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi }); }; - $scope.openAmountModal = function(address) { + $scope.openAmountModal = function(addr) { if (isCordova) - $scope.openInputAmountModal(address); + $scope.openInputAmountModal(addr); else - $scope.openCustomAmountModal(address); + $scope.openCustomInputAmountModal(addr); }; $scope.openInputAmountModal = function(addr) { var fc = profileService.focusedClient; $scope.color = fc.backgroundColor; $scope.showAlternativeAmount = $scope.showAlternative || null; - $scope.address = addr || null; $scope.self = self; + $scope.addr = addr; $ionicModal.fromTemplateUrl('views/modals/inputAmount.html', { scope: $scope