Enable cache for bitpay card

This commit is contained in:
Gustavo Maximiliano Cortez 2016-09-28 21:09:41 -03:00
parent da931600e2
commit 104209de5d
No known key found for this signature in database
GPG Key ID: 15EDAD8D9F2EB1AF
7 changed files with 90 additions and 44 deletions

View File

@ -3,9 +3,9 @@
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-title>BitPay Card</ion-nav-title>
<ion-nav-buttons side="secondary">
<button class="button back-button">
<i class="icon ion-ios-search-strong"></i>
<ion-nav-buttons side="secondary" ng-show="bitpayCard.bitpayCardAuthenticated">
<button class="button back-button" ui-sref="tabs.bitpayCard.preferences">
<i class="icon ion-ios-gear-outline"></i>
</button>
</ion-nav-buttons>
</ion-nav-bar>
@ -32,7 +32,7 @@
<form
ng-show="!bitpayCard.bitpayCardTwoFactorPending"
name="authenticateForm"
ng-submit="bitpayCard.authenticate()"
ng-submit="bitpayCard.authenticate(email, password)"
novalidate>
<div class="card list">
@ -68,7 +68,7 @@
<form
ng-show="bitpayCard.bitpayCardTwoFactorPending"
name="authenticate2FAForm"
ng-submit="bitpayCard.authenticate2FA()"
ng-submit="bitpayCard.authenticate2FA(twoFactorCode)"
novalidate>
<div class="list">
@ -98,24 +98,22 @@
<i class="icon ion-ios-plus-empty"></i> {{'Add Funds'|translate}}
</a>
</div>
<div ng-if="loadingHistory" class="m20t">
<div ng-if="loadingHistory" class="m10t">
<strong class="size-36">...</strong>
</div>
</div>
</div>
<div
class="oh pr m20t text-gray size-12 text-center"
class="m10t text-center padding"
ng-show="!bitpayCard.bitpayCardTransactionHistory[0] &&
!bitpayCard.bitpayCardInvoiceHistory[0] && !loadingHistory">
No transactions yet
!bitpayCard.bitpayCardInvoiceHistory[0] && (!loadingHistory || !bitpayCardCached)">
<i class="icon ion-ios-arrow-thin-up size-24"></i>
<h1>Get started</h1>
<h4>Your BitPay Card is ready. Add funds to your card to start using your card at stores and ATMs worldwide.</h4>
</div>
<div ng-show="loadingHistory" class="oh pr m20t text-gray text-center">
<i class="icon ion-android-sync"></i>
</div>
<div class="list" ng-show="!loadingHistory">
<div class="list" ng-if="bitpayCardCached">
<div class="item item-divider">
<select class="select-style" ng-model="dateRange" ng-change="bitpayCard.update(dateRange)">
<option value="last30Days">Recent Activity</option>

View File

@ -83,7 +83,8 @@
<div class="bg icon-bitpay-card"></div>
</i>
<h2>BitPay Card</h2>
<p translate>Add funds to get started</p>
<p ng-if="!bitpayCard" translate>Add funds to get started</p>
<span ng-if="bitpayCard">${{bitpayCard.balance}}</span>
<i class="icon nav-item-arrow-right"></i>
</a>
</div>

View File

@ -3,15 +3,14 @@
angular.module('copayApp.controllers').controller('bitpayCardController', function($scope, $timeout, $log, lodash, bitpayCardService, configService, profileService, walletService, ongoingProcess, pbkdf2Service, moment, popupService, gettextCatalog, bwcError) {
var self = this;
var wallet;
$scope.dateRange = 'last30Days';
$scope.network = bitpayCardService.getEnvironment();
$scope.$on('Wallet/Changed', function(event, w) {
if (lodash.isEmpty(w)) {
$log.debug('No wallet provided');
return;
}
wallet = w;
$log.debug('Wallet changed: ' + w.name);
bitpayCardService.getCacheData(function(err, data) {
if (err || lodash.isEmpty(data)) return;
$scope.bitpayCardCached = true;
self.bitpayCardTransactionHistory = data.transactions;
self.bitpayCardCurrentBalance = data.balance;
});
var processTransactions = function(invoices, history) {
@ -89,6 +88,14 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
self.bitpayCardTransactionHistory = processTransactions(invoices, history.transactionList);
self.bitpayCardCurrentBalance = history.currentCardBalance;
var cacheData = {
balance: self.bitpayCardCurrentBalance,
transactions: self.bitpayCardTransactionHistory
};
bitpayCardService.setCacheData(cacheData, function(err) {
if (err) $log.error(err);
});
});
});
}
@ -98,11 +105,11 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
});
};
this.authenticate = function() {
this.authenticate = function(email, password) {
var data = {
emailAddress : $scope.email,
hashedPassword : pbkdf2Service.pbkdf2Sync($scope.password, '..............', 200, 64).toString('hex')
emailAddress : email,
hashedPassword : pbkdf2Service.pbkdf2Sync(password, '..............', 200, 64).toString('hex')
};
// POST /authenticate
@ -123,10 +130,10 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
});
};
this.authenticate2FA = function() {
this.authenticate2FA = function(twoFactorCode) {
var data = {
twoFactorCode : $scope.twoFactorCode
twoFactorCode : twoFactorCode
};
self.authenticating = true;
@ -167,20 +174,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
};
$scope.$on("$ionicView.beforeEnter", function(event, data){
$scope.dateRange = 'last30Days';
$scope.network = bitpayCardService.getEnvironment();
$scope.wallets = profileService.getWallets({
network: $scope.network,
onlyComplete: true
});
self.update();
wallet = $scope.wallets[0];
if (wallet && wallet.credentials.n > 1)
self.isMultisigWallet = true;
});
});

View File

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesBitpayCardController',
function($scope, $state, $timeout, bitpayCardService, popupService) {
function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService) {
$scope.logout = function() {
var title = 'Are you sure you would like to log out of your Bitpay Card account?';
@ -12,8 +12,9 @@ angular.module('copayApp.controllers').controller('preferencesBitpayCardControll
var logout = function() {
bitpayCardService.logout(function() {
$ionicHistory.removeBackView();
$timeout(function() {
$state.go('bitpayCard.main');
$state.go('tabs.home');
}, 100);
});
};

View File

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('tabHomeController',
function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, gettextCatalog, lodash, popupService, ongoingProcess, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, $window) {
function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, gettextCatalog, lodash, popupService, ongoingProcess, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, $window, bitpayCardService) {
var wallet;
$scope.externalServices = {};
$scope.bitpayCardEnabled = true; // TODO
@ -206,7 +206,15 @@ angular.module('copayApp.controllers').controller('tabHomeController',
});
});
var bitpayCardCache = function() {
bitpayCardService.getCacheData(function(err, data) {
if (err || lodash.isEmpty(data)) return;
$scope.bitpayCard = data;
});
};
$scope.$on("$ionicView.enter", function(event, data) {
$scope.bitpayCard = null;
configService.whenAvailable(function() {
var config = configService.getSync();
var isWindowsPhoneApp = platformInfo.isWP && platformInfo.isCordova;
@ -217,6 +225,8 @@ angular.module('copayApp.controllers').controller('tabHomeController',
$scope.bitpayCardEnabled = config.bitpayCard.enabled;
$scope.nextStepEnabled = $scope.glideraEnabled || $scope.coinbaseEnabled || $scope.amazonEnabled || $scope.bitpayCardEnabled;
$scope.recentTransactionsEnabled = config.recentTransactions.enabled;
if ($scope.bitpayCardEnabled) bitpayCardCache();
});
$scope.nextStep();
$scope.updateAllWallets();

View File

@ -187,8 +187,38 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
});
};
root.getCacheData = function(cb) {
_setCredentials();
storageService.getBitpayCardCache(credentials.NETWORK, function(err, data) {
if (err) return cb(err);
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
return cb(null, data);
});
};
root.setCacheData = function(data, cb) {
_setCredentials();
data = JSON.stringify(data);
storageService.setBitpayCardCache(credentials.NETWORK, data, function(err) {
if (err) return cb(err);
return cb();
});
};
root.removeCacheData = function(cb) {
_setCredentials();
storageService.removeBitpayCardCache(credentials.NETWORK, function(err) {
if (err) return cb(err);
return cb();
});
};
root.logout = function(cb) {
_setCredentials();
root.removeCacheData(function() {});
storageService.removeBitpayCard(credentials.NETWORK, function(err) {
$http(_getBitPay('/visa-api/logout')).then(function(data) {
$log.info('BitPay Logout: SUCCESS');

View File

@ -337,6 +337,18 @@ angular.module('copayApp.services')
storage.remove('bitpayCard-' + network, cb);
};
root.setBitpayCardCache = function(network, data, cb) {
storage.set('bitpayCardCache-' + network, data, cb);
};
root.getBitpayCardCache = function(network, cb) {
storage.get('bitpayCardCache-' + network, cb);
};
root.removeBitpayCardCache = function(network, cb) {
storage.remove('bitpayCardCache-' + network, cb);
};
root.removeAllWalletData = function(walletId, cb) {
root.clearLastAddress(walletId, function(err) {
if (err) return cb(err);