refactor bitpay card

This commit is contained in:
Matias Alejo Garcia 2017-01-27 17:54:41 -03:00
commit 19b3d9bb15
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
11 changed files with 242 additions and 261 deletions

View file

@ -10,7 +10,11 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.network = bitpayService.getEnvironment().network;
var updateHistoryFromCache = function(cb) {
bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) {
// TODO no cache for now
$log.warn ('TODO: cache');
return cb();
bitpayCardService.getHistory($scope.cardId, function(err, data) {
if (err ||  lodash.isEmpty(data)) return cb();
$scope.historyCached = true;
self.bitpayCardTransactionHistory = data.transactions;
@ -86,15 +90,18 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
self.bitpayCardCurrentBalance = history.currentCardBalance;
if ($scope.dateRange.value == 'last30Days') {
$log.debug('BitPay Card: store cache history');
var cacheHistory = {
balance: history.currentCardBalance,
transactions: history.txs
};
bitpayCardService.setBitpayDebitCardsHistory($scope.cardId, cacheHistory, {}, function(err) {
if (err) $log.error(err);
$scope.historyCached = true;
});
// TODO CACHE
//
// $log.debug('BitPay Card: store cache history');
// var cacheHistory = {
// balance: history.currentCardBalance,
// transactions: history.txs
// };
// bitpayCardService.setHistory($scope.cardId, cacheHistory, {}, function(err) {
// if (err) $log.error(err);
// $scope.historyCached = true;
// });
}
$timeout(function() {
$scope.$apply();
@ -147,7 +154,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
updateHistoryFromCache(function() {
self.update();
});
bitpayCardService.getBitpayDebitCards(function(err, cards) {
bitpayCardService.getCards(function(err, cards) {
if (err) return;
$scope.card = lodash.find(cards, function(card) {
return card.eid == $scope.cardId;

View file

@ -1,5 +1,5 @@
'use strict';
angular.module('copayApp.controllers').controller('bitpayCardIntroController', function($scope, $log, $state, $ionicHistory, storageService, externalLinkService, bitpayCardService, gettextCatalog, popupService, appIdentityService, bitpayService) {
angular.module('copayApp.controllers').controller('bitpayCardIntroController', function($scope, $log, $state, $ionicHistory, storageService, externalLinkService, bitpayCardService, gettextCatalog, popupService, appIdentityService, bitpayService, lodash) {
$scope.$on("$ionicView.beforeEnter", function(event, data) {
if (data.stateParams && data.stateParams.secret) {
@ -18,27 +18,23 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
return;
}
if (paired) {
bitpayCardService.fetchBitpayDebitCards(apiContext, function(err, data) {
bitpayCardService.sync(apiContext, function(err, cards) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error fetching Debit Cards'), err);
popupService.showAlert(gettextCatalog.getString('Error updating Debit Cards'), err);
return;
}
// Set flag for nextStep
storageService.setNextStep('BitpayCard', 'true', function(err) {});
// Save data
bitpayCardService.setBitpayDebitCards(data, function(err) {
if (err) return;
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home').then(function() {
if (data.cards[0]) {
$state.transitionTo('tabs.bitpayCard', {
id: data.cards[0].id
});
}
});
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home').then(function() {
if (cards[0]) {
$state.transitionTo('tabs.bitpayCard', {
id: cards[0].id
});
}
});
});
}

View file

@ -1,19 +1,21 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesBitpayCardController',
function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService, gettextCatalog) {
function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService, gettextCatalog, $log) {
$scope.remove = function(card) {
var msg = gettextCatalog.getString('Are you sure you would like to remove your BitPay Card ({{lastFourDigits}}) from this device?', {
lastFourDigits: card.lastFourDigits
});
popupService.showConfirm(null, msg, null, null, function(res) {
if (res) remove(card);
$log.info('Removing bitpay card:' + card.eid)
if (res)
remove(card.eid);
});
};
var remove = function(card) {
bitpayCardService.remove(card, function(err) {
var remove = function(cardEid) {
bitpayCardService.remove(cardEid, function(err) {
if (err) {
return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not remove card'));
}
@ -25,8 +27,9 @@ angular.module('copayApp.controllers').controller('preferencesBitpayCardControll
};
$scope.$on("$ionicView.beforeEnter", function(event, data) {
bitpayCardService.getBitpayDebitCards(function(err, data) {
bitpayCardService.getCards(function(err, data) {
if (err) return;
$scope.bitpayCards = data;
});
});

View file

@ -285,7 +285,7 @@ angular.module('copayApp.controllers').controller('tabHomeController',
};
var bitpayCardCache = function() {
bitpayCardService.getBitpayDebitCards(function(err, data) {
bitpayCardService.getCards(function(err, data) {
if (err) return;
if (lodash.isEmpty(data)) {
$scope.bitpayCards = null;
@ -296,14 +296,16 @@ angular.module('copayApp.controllers').controller('tabHomeController',
$scope.$digest();
}, 100);
});
bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) {
if (err) return;
if (lodash.isEmpty(data)) {
$scope.cardsHistory = null;
return;
}
$scope.cardsHistory = data;
});
// TODO
// bitpayCardService.getCardsHistoryCache(function(err, data) {
// if (err) return;
// if (lodash.isEmpty(data)) {
// $scope.cardsHistory = null;
// return;
// }
// $scope.cardsHistory = data;
// });
};
$scope.onRefresh = function() {

View file

@ -29,7 +29,7 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct
$scope.coinbaseEnabled = config.coinbaseV2 && !isWindowsPhoneApp;
if ($scope.bitpayCardEnabled) {
bitpayCardService.getBitpayDebitCards(function(err, cards) {
bitpayCardService.getCards(function(err, cards) {
if (err) $log.error(err);
$scope.bitpayCards = cards && cards.length > 0;
});