Activate caching for bitpay card

This commit is contained in:
Gustavo Maximiliano Cortez 2016-10-07 13:51:55 -03:00
parent 9196e07f3a
commit 3f2f169db3
No known key found for this signature in database
GPG Key ID: 15EDAD8D9F2EB1AF
5 changed files with 104 additions and 52 deletions

View File

@ -6,13 +6,15 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.dateRange = 'last30Days'; $scope.dateRange = 'last30Days';
$scope.network = bitpayCardService.getEnvironment(); $scope.network = bitpayCardService.getEnvironment();
/* var getFromCache = function() {
bitpayCardService.getCacheData(function(err, data) { bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) {
if (err || lodash.isEmpty(data)) return; if (err || lodash.isEmpty(data)) return;
self.bitpayCardTransactionHistory = data.transactions; $scope.historyCached = true;
self.bitpayCardCurrentBalance = data.balance; self.bitpayCardTransactionHistory = data.transactions;
}); self.bitpayCardCurrentBalance = data.balance;
*/ $scope.$apply();
});
};
var setDateRange = function(preset) { var setDateRange = function(preset) {
var startDate, endDate; var startDate, endDate;
@ -54,10 +56,17 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
self.bitpayCardTransactionHistory = history.transactionList; self.bitpayCardTransactionHistory = history.transactionList;
self.bitpayCardCurrentBalance = history.currentCardBalance; self.bitpayCardCurrentBalance = history.currentCardBalance;
var cacheData = { var cacheHistory = {
balance: self.bitpayCardCurrentBalance, balance: self.bitpayCardCurrentBalance,
transactions: self.bitpayCardTransactionHistory transactions: self.bitpayCardTransactionHistory
}; };
bitpayCardService.setBitpayDebitCardsHistory($scope.cardId, cacheHistory, {}, function(err) {
if (err) $log.error(err);
$scope.historyCached = true;
});
$timeout(function() {
$scope.$apply();
});
}); });
}; };
@ -93,6 +102,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$state.go('tabs.home'); $state.go('tabs.home');
popupService.showAlert(null, msg); popupService.showAlert(null, msg);
} else { } else {
getFromCache();
self.update(); self.update();
} }
}); });

View File

@ -206,12 +206,10 @@ angular.module('copayApp.controllers').controller('tabHomeController',
if (err) return; if (err) return;
$scope.bitpayCards = data.cards; $scope.bitpayCards = data.cards;
}); });
/* bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) {
bitpayCardService.getCacheData(function(err, data) {
if (err ||  lodash.isEmpty(data)) return; if (err ||  lodash.isEmpty(data)) return;
$scope.bitpayCard = data; $scope.cardsHistory = data;
}); });
*/
}; };
$scope.onRefresh = function() { $scope.onRefresh = function() {

View File

@ -278,6 +278,49 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
}); });
}; };
root.getBitpayDebitCardsHistory = function(cardId, cb) {
_setCredentials();
storageService.getBitpayDebitCardsHistory(credentials.NETWORK, function(err, data) {
if (err) return cb(err);
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (cardId) data = data[cardId];
return cb(null, data);
});
};
root.setBitpayDebitCardsHistory = function(cardId, data, opts, cb) {
_setCredentials();
storageService.getBitpayDebitCardsHistory(credentials.NETWORK, function(err, oldData) {
if (lodash.isString(oldData)) {
oldData = JSON.parse(oldData);
}
if (lodash.isString(data)) {
data = JSON.parse(data);
}
var inv = oldData || {};
inv[cardId] = data;
if (opts && opts.remove) {
delete(inv[cardId]);
}
inv = JSON.stringify(inv);
storageService.setBitpayDebitCardsHistory(credentials.NETWORK, inv, function(err) {
return cb(err);
});
});
};
root.removeBitpayDebitCardsHistory = function(cb) {
_setCredentials();
storageService.removeBitpayDebitCardsHistory(credentials.NETWORK, function(err) {
if (err) return cb(err);
return cb();
});
};
root.logout = function(cb) { root.logout = function(cb) {
_setCredentials(); _setCredentials();
storageService.removeBitpayDebitCards(credentials.NETWORK, function(err) { storageService.removeBitpayDebitCards(credentials.NETWORK, function(err) {

View File

@ -325,16 +325,16 @@ angular.module('copayApp.services')
storage.remove('coinbaseTxs-' + network, cb); storage.remove('coinbaseTxs-' + network, cb);
}; };
root.setBitpayCard = function(network, data, cb) { root.setBitpayDebitCardsHistory = function(network, data, cb) {
storage.set('bitpayCard-' + network, data, cb); storage.set('bitpayDebitCardsHistory-' + network, data, cb);
}; };
root.getBitpayCard = function(network, cb) { root.getBitpayDebitCardsHistory = function(network, cb) {
storage.get('bitpayCard-' + network, cb); storage.get('bitpayDebitCardsHistory-' + network, cb);
}; };
root.removeBitpayCard = function(network, cb) { root.removeBitpayDebitCardsHistory = function(network, cb) {
storage.remove('bitpayCard-' + network, cb); storage.remove('bitpayDebitCardsHistory-' + network, cb);
}; };
root.setBitpayDebitCards = function(network, data, cb) { root.setBitpayDebitCards = function(network, data, cb) {

View File

@ -78,40 +78,41 @@
{{wallet.m}}-of-{{wallet.n}} {{wallet.m}}-of-{{wallet.n}}
</span> </span>
</span> </span>
<p> </span>
<span ng-if="!wallet.isComplete()" class="assertive" translate>
Incomplete <p>
</span> <span ng-if="!wallet.isComplete()" class="assertive" translate>
<span ng-if="wallet.isComplete()"> Incomplete
<span ng-if="!wallet.balanceHidden">{{wallet.status.availableBalanceStr}}</span> </span>
<span ng-if="wallet.balanceHidden" translate>[Balance Hidden]</span> <span ng-if="wallet.isComplete()">
</span> <span ng-if="!wallet.balanceHidden">{{wallet.status.availableBalanceStr}}</span>
&nbsp; <span ng-if="wallet.balanceHidden" translate>[Balance Hidden]</span>
</p> </span>
<i class="icon bp-arrow-right"></i> &nbsp;
</a> </p>
<a ng-repeat="card in bitpayCards" <i class="icon bp-arrow-right"></i>
ui-sref="tabs.bitpayCard({id:card.id})" </a>
ng-if="bitpayCardEnabled && bitpayCards[0]" <a ng-repeat="card in bitpayCards"
class="item item-icon-left item-big-icon-left item-icon-right"> ui-sref="tabs.bitpayCard({id:card.id})"
<i class="icon big-icon-svg"> ng-if="bitpayCardEnabled && bitpayCards[0]"
<div class="bg icon-bitpay-card"></div> class="item item-icon-left item-big-icon-left item-icon-right">
</i> <i class="icon big-icon-svg">
<h2>BitPay Visa&reg; Card</h2> <div class="bg icon-bitpay-card"></div>
<p>{{card.lastFourDigits}}</p> </i>
<i class="icon bp-arrow-right"></i> <h2>BitPay Visa&reg; Card</h2>
</a> <p>{{!cardsHistory[card.id].balance ? '$' + cardsHistory[card.id].balance : 'Add funds to get started'|translate}}</p>
<a ui-sref="tabs.bitpayCardIntro" <i class="icon bp-arrow-right"></i>
ng-if="bitpayCardEnabled && !bitpayCards[0]" </a>
class="item item-icon-left item-big-icon-left item-icon-right"> <a ui-sref="tabs.bitpayCardIntro"
<i class="icon big-icon-svg"> ng-if="bitpayCardEnabled && !bitpayCards[0]"
<div class="bg icon-bitpay-card"></div> class="item item-icon-left item-big-icon-left item-icon-right">
</i> <i class="icon big-icon-svg">
<h2>BitPay Visa&reg; Card</h2> <div class="bg icon-bitpay-card"></div>
<p translate>Add your cards</p> </i>
<i class="icon bp-arrow-right"></i> <h2>BitPay Visa&reg; Card</h2>
</a> <p translate>Add your cards</p>
</div> <i class="icon bp-arrow-right"></i>
</a>
</div> </div>
<div class="list card" ng-if="wallets[0] && externalServices.BuyAndSell && (glideraEnabled || coinbaseEnabled)"> <div class="list card" ng-if="wallets[0] && externalServices.BuyAndSell && (glideraEnabled || coinbaseEnabled)">