Currency list in settings

This commit is contained in:
Esteban Ordano 2014-08-27 17:15:05 -03:00
commit 3225553d78
6 changed files with 94 additions and 22 deletions

View file

@ -7,6 +7,8 @@ var defaultConfig = {
// DEFAULT unit: Bit // DEFAULT unit: Bit
unitName: 'bits', unitName: 'bits',
unitToSatoshi: 100, unitToSatoshi: 100,
alternativeName: 'US Dollar',
alternativeIsoCode: 'USD',
// wallet limits // wallet limits
limits: { limits: {

View file

@ -12,10 +12,9 @@ angular.module('copayApp.controllers').controller('SendController',
$scope.unitToBtc = config.unitToSatoshi / bitcore.util.COIN; $scope.unitToBtc = config.unitToSatoshi / bitcore.util.COIN;
$scope.minAmount = config.limits.minAmountSatoshi * satToUnit; $scope.minAmount = config.limits.minAmountSatoshi * satToUnit;
$scope.minAlternativeAmount = config.limits.minAmountSatoshi * satToAlternative; $scope.minAlternativeAmount = config.limits.minAmountSatoshi * satToAlternative;
// Mockup
$rootScope.alternativeName = 'Dollars'; this.alternativeName = config.alternativeName;
$rootScope.alternativeIsoCode = 'USD'; this.alternativeIsoCode = config.alternativeIsoCode;
config.unitDecimals = 2;
this.rateService = rateService; this.rateService = rateService;
$scope._amount = 0; $scope._amount = 0;
@ -40,7 +39,7 @@ angular.module('copayApp.controllers').controller('SendController',
this._alternative = newValue; this._alternative = newValue;
if (typeof(newValue) === 'number') { if (typeof(newValue) === 'number') {
this._amount = -(-( this._amount = -(-(
rateService.fromFiat(newValue, $rootScope.alternativeIsoCode) * satToUnit rateService.fromFiat(newValue, config.alternativeIsoCode) * satToUnit
).toFixed(config.unitDecimals)); ).toFixed(config.unitDecimals));
} }
}, },
@ -56,7 +55,7 @@ angular.module('copayApp.controllers').controller('SendController',
this._amount = newValue; this._amount = newValue;
if (newValue) { if (newValue) {
this._alternative = -(-( this._alternative = -(-(
rateService.toFiat(newValue * config.unitToSatoshi, $rootScope.alternativeIsoCode) rateService.toFiat(newValue * config.unitToSatoshi, config.alternativeIsoCode)
).toFixed(2)); ).toFixed(2));
} }
}, },

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $location, controllerUtils) { angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $location, controllerUtils, rateService) {
controllerUtils.redirIfLogged(); controllerUtils.redirIfLogged();
$scope.title = 'Settings'; $scope.title = 'Settings';
@ -14,27 +14,51 @@ angular.module('copayApp.controllers').controller('SettingsController', function
$scope.unitOpts = [{ $scope.unitOpts = [{
name: 'Satoshis (100,000,000 satoshis = 1BTC)', name: 'Satoshis (100,000,000 satoshis = 1BTC)',
shortName: 'SAT', shortName: 'SAT',
value: 1 value: 1,
decimals: 0
}, { }, {
name: 'bits (1,000,000 bits = 1BTC)', name: 'bits (1,000,000 bits = 1BTC)',
shortName: 'bits', shortName: 'bits',
value: 100 value: 100,
decimals: 2
}, { }, {
name: 'mBTC (1,000 mBTC = 1BTC)', name: 'mBTC (1,000 mBTC = 1BTC)',
shortName: 'mBTC', shortName: 'mBTC',
value: 100000 value: 100000,
decimals: 5
}, { }, {
name: 'BTC', name: 'BTC',
shortName: 'BTC', shortName: 'BTC',
value: 100000000 value: 100000000,
decimals: 8
}]; }];
$scope.selectedAlternative = {
name: 'US Dollar',
isoCode: 'USD'
};
$scope.alternativeOpts = rateService.alternatives;
rateService.whenAvailable(function() {
$scope.alternativeOpts = rateService.listAlternatives();
for (var ii in $scope.alternativeOpts) {
if (config.alternativeIsoCode === $scope.alternativeOpts[ii].isoCode) {
$scope.selectedAlternative = $scope.alternativeOpts[ii];
}
}
});
for (var ii in $scope.unitOpts) { for (var ii in $scope.unitOpts) {
if (config.unitName === $scope.unitOpts[ii].shortName) { if (config.unitName === $scope.unitOpts[ii].shortName) {
$scope.selectedUnit = $scope.unitOpts[ii]; $scope.selectedUnit = $scope.unitOpts[ii];
break; break;
} }
} }
for (var ii in $scope.alternativeOpts) {
if (config.alternativeIsoCode === $scope.alternativeOpts[ii].isoCode) {
$scope.selectedAlternative = $scope.alternativeOpts[ii];
}
}
$scope.changeNetwork = function() { $scope.changeNetwork = function() {
$scope.insightHost = $scope.networkName !== 'testnet' ? 'test-insight.bitpay.com' : 'insight.bitpay.com'; $scope.insightHost = $scope.networkName !== 'testnet' ? 'test-insight.bitpay.com' : 'insight.bitpay.com';
@ -68,7 +92,11 @@ angular.module('copayApp.controllers').controller('SettingsController', function
disableVideo: $scope.disableVideo, disableVideo: $scope.disableVideo,
unitName: $scope.selectedUnit.shortName, unitName: $scope.selectedUnit.shortName,
unitToSatoshi: $scope.selectedUnit.value, unitToSatoshi: $scope.selectedUnit.value,
version: copay.version, unitDecimals: $scope.selectedUnit.decimals,
alternativeName: $scope.selectedAlternative.name,
alternativeIsoCode: $scope.selectedAlternative.isoCode,
version: copay.version
})); }));
// Go home reloading the application // Go home reloading the application

View file

@ -1,28 +1,51 @@
'use strict'; 'use strict';
var RateService = function($http) { var request = require('request');
var RateService = function() {
this.isAvailable = false; this.isAvailable = false;
this.SAT_TO_BTC = 1 / 1e8; this.SAT_TO_BTC = 1 / 1e8;
this.queued = [];
this.alternatives = [];
var that = this; var that = this;
var backoff = 5; var backoff = 5;
var retrieve = function() { var retrieve = function() {
$http({method: 'GET', url: 'https://bitpay.com/api/rates'}). request.get({
success(function(data, status, headers, config) { url:'https://bitpay.com/api/rates',
json: true
}, function(err, response, listOfCurrencies) {
if (err) {
backoff *= 1.5;
setTimeout(retrieve, backoff * 1000);
return;
}
var rates = {}; var rates = {};
data.forEach(function(element) { listOfCurrencies.forEach(function(element) {
rates[element.code] = element.rate; rates[element.code] = element.rate;
that.alternatives.push({
name: element.name,
isoCode: element.code,
rate: element.rate
});
}); });
that.isAvailable = true; that.isAvailable = true;
that.rates = rates; that.rates = rates;
}). that.queued.forEach(function(callback) {
error(function(data, status, headers, config) { setTimeout(callback, 0);
backoff *= 1.5; });
setTimeout(retrieve, backoff * 1000);
}); });
}; };
retrieve(); retrieve();
}; };
RateService.prototype.whenAvailable = function(callback) {
if (this.isAvailable) {
setTimeout(callback, 0);
} else {
this.queued.push(callback);
}
};
RateService.prototype.toFiat = function(satoshis, code) { RateService.prototype.toFiat = function(satoshis, code) {
if (!this.isAvailable) { if (!this.isAvailable) {
return 0; return 0;
@ -37,4 +60,19 @@ RateService.prototype.fromFiat = function(amount, code) {
return amount / this.rates[code] / this.SAT_TO_BTC; return amount / this.rates[code] / this.SAT_TO_BTC;
}; };
RateService.prototype.listAlternatives = function() {
if (!this.isAvailable) {
return [];
}
var alts = [];
this.alternatives.forEach(function(element) {
alts.push({
name: element.name,
isoCode: element.isoCode
});
});
return alts;
};
angular.module('copayApp.services').service('rateService', RateService); angular.module('copayApp.services').service('rateService', RateService);

View file

@ -85,7 +85,7 @@
</div> </div>
<div class="large-6 medium-6 columns"> <div class="large-6 medium-6 columns">
<div class="row collapse"> <div class="row collapse">
<label for="alternative">Amount in {{ $root.alternativeName }} </label> <label for="alternative">Amount in {{ ctrl.alternativeName }} </label>
<div class="small-9 columns"> <div class="small-9 columns">
<input type="number" id="alternative_amount" <input type="number" id="alternative_amount"
ng-disabled="loading || !ctrl.rateService.isAvailable " ng-disabled="loading || !ctrl.rateService.isAvailable "
@ -95,7 +95,7 @@
> >
</div> </div>
<div class="small-3 columns"> <div class="small-3 columns">
<span class="postfix">{{$root.alternativeIsoCode}}</span> <span class="postfix">{{ctrl.alternativeIsoCode}}</span>
</div> </div>
</div> </div>
</div> </div>

View file

@ -26,6 +26,11 @@
<select class="form-control" ng-model="selectedUnit" ng-options="o.name for o in unitOpts" required> <select class="form-control" ng-model="selectedUnit" ng-options="o.name for o in unitOpts" required>
</select> </select>
</fieldset> </fieldset>
<fieldset>
<legend>Alternative Currency</legend>
<select class="form-control" ng-model="selectedAlternative" ng-options="alternative.name for alternative in alternativeOpts" required>
</select>
</fieldset>
<fieldset> <fieldset>
<legend>Videoconferencing</legend> <legend>Videoconferencing</legend>
<input id="disableVideo-opt" type="checkbox" ng-model="disableVideo" class="form-control"> <input id="disableVideo-opt" type="checkbox" ng-model="disableVideo" class="form-control">