diff --git a/src/js/controllers/bitpayCard.js b/src/js/controllers/bitpayCard.js index 8dee60315..f92c3e461 100644 --- a/src/js/controllers/bitpayCard.js +++ b/src/js/controllers/bitpayCard.js @@ -6,13 +6,15 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi $scope.dateRange = 'last30Days'; $scope.network = bitpayCardService.getEnvironment(); - /* - bitpayCardService.getCacheData(function(err, data) { - if (err || lodash.isEmpty(data)) return; - self.bitpayCardTransactionHistory = data.transactions; - self.bitpayCardCurrentBalance = data.balance; - }); - */ + var getFromCache = function() { + bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) { + if (err || lodash.isEmpty(data)) return; + $scope.historyCached = true; + self.bitpayCardTransactionHistory = data.transactions; + self.bitpayCardCurrentBalance = data.balance; + $scope.$apply(); + }); + }; var setDateRange = function(preset) { var startDate, endDate; @@ -54,10 +56,17 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi self.bitpayCardTransactionHistory = history.transactionList; self.bitpayCardCurrentBalance = history.currentCardBalance; - var cacheData = { + var cacheHistory = { balance: self.bitpayCardCurrentBalance, 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'); popupService.showAlert(null, msg); } else { + getFromCache(); self.update(); } }); diff --git a/src/js/controllers/tab-home.js b/src/js/controllers/tab-home.js index 7e40a4e2c..996226957 100644 --- a/src/js/controllers/tab-home.js +++ b/src/js/controllers/tab-home.js @@ -206,12 +206,10 @@ angular.module('copayApp.controllers').controller('tabHomeController', if (err) return; $scope.bitpayCards = data.cards; }); - /* - bitpayCardService.getCacheData(function(err, data) { + bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) { if (err ||  lodash.isEmpty(data)) return; - $scope.bitpayCard = data; + $scope.cardsHistory = data; }); - */ }; $scope.onRefresh = function() { diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index e01441220..ca0c26b63 100644 --- a/src/js/services/bitpayCardService.js +++ b/src/js/services/bitpayCardService.js @@ -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) { _setCredentials(); storageService.removeBitpayDebitCards(credentials.NETWORK, function(err) { diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index bbbcea5a0..1cce7006a 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -325,16 +325,16 @@ angular.module('copayApp.services') storage.remove('coinbaseTxs-' + network, cb); }; - root.setBitpayCard = function(network, data, cb) { - storage.set('bitpayCard-' + network, data, cb); + root.setBitpayDebitCardsHistory = function(network, data, cb) { + storage.set('bitpayDebitCardsHistory-' + network, data, cb); }; - root.getBitpayCard = function(network, cb) { - storage.get('bitpayCard-' + network, cb); + root.getBitpayDebitCardsHistory = function(network, cb) { + storage.get('bitpayDebitCardsHistory-' + network, cb); }; - root.removeBitpayCard = function(network, cb) { - storage.remove('bitpayCard-' + network, cb); + root.removeBitpayDebitCardsHistory = function(network, cb) { + storage.remove('bitpayDebitCardsHistory-' + network, cb); }; root.setBitpayDebitCards = function(network, data, cb) { diff --git a/www/views/tab-home.html b/www/views/tab-home.html index f3bbc976a..32dfafe21 100644 --- a/www/views/tab-home.html +++ b/www/views/tab-home.html @@ -78,40 +78,41 @@ {{wallet.m}}-of-{{wallet.n}} -

- - Incomplete - - - {{wallet.status.availableBalanceStr}} - [Balance Hidden] - -   -

- - - - -
-
-

BitPay Visa® Card

-

{{card.lastFourDigits}}

- -
- - -
-
-

BitPay Visa® Card

-

Add your cards

- -
- + + +

+ + Incomplete + + + {{wallet.status.availableBalanceStr}} + [Balance Hidden] + +   +

+ + + + +
+
+

BitPay Visa® Card

+

{{!cardsHistory[card.id].balance ? '$' + cardsHistory[card.id].balance : 'Add funds to get started'|translate}}

+ +
+ + +
+
+

BitPay Visa® Card

+

Add your cards

+ +