Wallet/src/js/controllers/preferencesAltCurrency.js

89 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('preferencesAltCurrencyController',
function($scope, $log, $timeout, $ionicHistory, configService, rateService, lodash, profileService, walletService, storageService) {
2015-03-06 12:00:10 -03:00
2016-12-14 11:44:55 -03:00
var next = 10;
var completeAlternativeList = [];
2016-12-14 11:44:55 -03:00
2016-12-13 16:48:22 -03:00
function init() {
2016-12-14 09:33:23 -03:00
var unusedCurrencyList = [{
isoCode: 'LTL'
}, {
isoCode: 'BTC'
}];
2016-12-14 09:48:36 -03:00
rateService.whenAvailable(function() {
2016-12-14 11:44:55 -03:00
$scope.listComplete = false;
2016-12-14 09:48:36 -03:00
var idx = lodash.indexBy(unusedCurrencyList, 'isoCode');
var idx2 = lodash.indexBy($scope.lastUsedAltCurrencyList, 'isoCode');
2016-12-15 15:49:02 -03:00
completeAlternativeList = lodash.reject(rateService.listAlternatives(true), function(c) {
2016-12-14 09:48:36 -03:00
return idx[c.isoCode] || idx2[c.isoCode];
});
2016-12-14 11:44:55 -03:00
$scope.altCurrencyList = completeAlternativeList.slice(0, 10);
2016-12-15 15:49:02 -03:00
2016-12-14 09:48:36 -03:00
$timeout(function() {
$scope.$apply();
});
2015-03-06 12:00:10 -03:00
});
2016-12-13 16:48:22 -03:00
}
2016-12-14 11:44:55 -03:00
$scope.loadMore = function() {
$timeout(function() {
$scope.altCurrencyList = completeAlternativeList.slice(0, next);
next += 10;
$scope.listComplete = $scope.altCurrencyList.length >= completeAlternativeList.length;
$scope.$broadcast('scroll.infiniteScrollComplete');
}, 100);
};
2016-12-13 16:48:22 -03:00
$scope.findCurrency = function(search) {
if (!search) init();
2016-12-14 11:44:55 -03:00
$scope.altCurrencyList = lodash.filter(completeAlternativeList, function(item) {
2016-12-13 16:48:22 -03:00
var val = item.name;
return lodash.includes(val.toLowerCase(), search.toLowerCase());
});
$timeout(function() {
$scope.$apply();
});
};
2015-03-06 12:00:10 -03:00
2016-06-10 09:27:14 -03:00
$scope.save = function(newAltCurrency) {
2015-03-06 12:00:10 -03:00
var opts = {
wallet: {
settings: {
alternativeName: newAltCurrency.name,
alternativeIsoCode: newAltCurrency.isoCode,
}
}
};
configService.set(opts, function(err) {
if (err) $log.warn(err);
2016-08-23 12:01:09 -03:00
2016-08-29 16:48:15 -03:00
$ionicHistory.goBack();
saveLastUsed(newAltCurrency);
2017-01-16 16:00:09 -03:00
walletService.updateRemotePreferences(profileService.getWallets());
2015-03-06 12:00:10 -03:00
});
};
function saveLastUsed(newAltCurrency) {
$scope.lastUsedAltCurrencyList.unshift(newAltCurrency);
$scope.lastUsedAltCurrencyList = lodash.uniq($scope.lastUsedAltCurrencyList, 'isoCode');
2016-12-14 09:48:36 -03:00
$scope.lastUsedAltCurrencyList = $scope.lastUsedAltCurrencyList.slice(0, 3);
storageService.setLastCurrencyUsed(JSON.stringify($scope.lastUsedAltCurrencyList), function() {});
};
2016-12-14 09:33:23 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
2016-12-14 09:48:36 -03:00
var config = configService.getSync();
$scope.currentCurrency = config.wallet.settings.alternativeIsoCode;
2016-12-15 15:49:02 -03:00
2016-12-14 09:48:36 -03:00
storageService.getLastCurrencyUsed(function(err, lastUsedAltCurrency) {
$scope.lastUsedAltCurrencyList = lastUsedAltCurrency ? JSON.parse(lastUsedAltCurrency) : [];
init();
});
2016-12-14 09:33:23 -03:00
});
2015-03-06 12:00:10 -03:00
});