Merge pull request #5015 from ajp8164/feat/multiple-cards

Implement multiple cards.
This commit is contained in:
Gustavo Maximiliano Cortez 2016-11-14 15:40:01 -03:00 committed by GitHub
commit 09bdcc3ce4
11 changed files with 241 additions and 74 deletions

View File

@ -145,6 +145,12 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
updateHistoryFromCache(function() {
self.update();
});
bitpayCardService.getBitpayDebitCards(function(err, cards) {
if (err) return;
$scope.card = lodash.find(cards, function(card) {
return card.eid == $scope.cardId;
});
});
}
});

View File

@ -3,16 +3,16 @@
angular.module('copayApp.controllers').controller('preferencesBitpayCardController',
function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService, gettextCatalog) {
$scope.remove = function() {
$scope.remove = function(card) {
var msg = gettextCatalog.getString('Are you sure you would like to remove your BitPay Card account from this device?');
popupService.showConfirm(null, msg, null, null, function(res) {
if (res) remove();
if (res) remove(card);
});
};
var remove = function() {
bitpayCardService.remove(function() {
$ionicHistory.removeBackView();
var remove = function(card) {
bitpayCardService.remove(card, function() {
$ionicHistory.clearHistory();
$timeout(function() {
$state.go('tabs.home');
}, 100);
@ -22,7 +22,7 @@ angular.module('copayApp.controllers').controller('preferencesBitpayCardControll
$scope.$on("$ionicView.beforeEnter", function(event, data) {
bitpayCardService.getBitpayDebitCards(function(err, data) {
if (err) return;
$scope.bitpayCards = data.cards;
$scope.bitpayCards = data;
});
});

View File

@ -246,7 +246,7 @@ angular.module('copayApp.controllers').controller('tabHomeController',
$scope.bitpayCards = null;
return;
}
$scope.bitpayCards = data.cards;
$scope.bitpayCards = data;
});
bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) {
if (err) return;

View File

@ -1001,41 +1001,50 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
});
}
$log.info('Init profile...');
// Try to open local profile
profileService.loadAndBindProfile(function(err) {
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$log.info('Verifying storage...');
storageService.verify(function(err) {
if (err) {
if (err.message && err.message.match('NOPROFILE')) {
$log.debug('No profile... redirecting');
$state.go('onboarding.welcome');
} else if (err.message && err.message.match('NONAGREEDDISCLAIMER')) {
if (lodash.isEmpty(profileService.getWallets())) {
$log.debug('No wallets and no disclaimer... redirecting');
$state.go('onboarding.welcome');
} else {
$log.debug('Display disclaimer... redirecting');
$state.go('onboarding.disclaimer', {
resume: true
});
}
} else {
throw new Error(err); // TODO
}
$log.error('Storage failed to verify: ' + err);
// TODO - what next?
} else {
profileService.storeProfileIfDirty();
$log.debug('Profile loaded ... Starting UX.');
scannerService.gentleInitialize();
$state.go('tabs.home');
$log.info('Storage OK');
}
// After everything have been loaded, initialize handler URL
$timeout(function() {
openURLService.init();
}, 1000);
$log.info('Init profile...');
// Try to open local profile
profileService.loadAndBindProfile(function(err) {
$ionicHistory.nextViewOptions({
disableAnimate: true
});
if (err) {
if (err.message && err.message.match('NOPROFILE')) {
$log.debug('No profile... redirecting');
$state.go('onboarding.welcome');
} else if (err.message && err.message.match('NONAGREEDDISCLAIMER')) {
if (lodash.isEmpty(profileService.getWallets())) {
$log.debug('No wallets and no disclaimer... redirecting');
$state.go('onboarding.welcome');
} else {
$log.debug('Display disclaimer... redirecting');
$state.go('onboarding.disclaimer', {
resume: true
});
}
} else {
throw new Error(err); // TODO
}
} else {
profileService.storeProfileIfDirty();
$log.debug('Profile loaded ... Starting UX.');
scannerService.gentleInitialize();
$state.go('tabs.home');
}
// After everything have been loaded, initialize handler URL
$timeout(function() {
openURLService.init();
}, 1000);
});
});
});

View File

@ -174,7 +174,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
if (err) return cb(err);
root.getBitpayDebitCards(function(err, data) {
if (err) return cb(err);
var card = lodash.find(data.cards, {id : cardId});
var card = lodash.find(data, {id : cardId});
if (!card) return cb(_setError('Not card found'));
// Get invoices
$http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) {
@ -211,7 +211,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
if (err) return cb(err);
root.getBitpayDebitCards(function(err, data) {
if (err) return cb(err);
var card = lodash.find(data.cards, {id : cardId});
var card = lodash.find(data, {id : cardId});
if (!card) return cb(_setError('Not card found'));
$http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) {
$log.info('BitPay TopUp: SUCCESS');
@ -288,13 +288,11 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http,
});
};
root.remove = function(cb) {
storageService.removeBitpayCardCredentials(BITPAY_CARD_NETWORK, function(err) {
storageService.removeBitpayDebitCards(BITPAY_CARD_NETWORK, function(err) {
storageService.removeBitpayDebitCardsHistory(BITPAY_CARD_NETWORK, function(err) {
$log.info('BitPay Debit Cards Removed: SUCCESS');
return cb();
});
root.remove = function(card, cb) {
storageService.removeBitpayDebitCard(BITPAY_CARD_NETWORK, card, function(err) {
storageService.removeBitpayDebitCardHistory(BITPAY_CARD_NETWORK, card, function(err) {
$log.info('BitPay Debit Card(s) Removed: SUCCESS');
return cb();
});
});
};

View File

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services')
.factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo) {
.factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, $timeout) {
var root = {};
@ -74,7 +74,90 @@ angular.module('copayApp.services')
});
};
////////////////////////////////////////////////////////////////////////////
//
// UPGRADING STORAGE
//
// 1. Write a function to upgrade the desired storage key(s). The function should have the protocol:
//
// _upgrade_x(key, network, cb), where:
//
// `x` is the name of the storage key
// `key` is the name of the storage key being upgraded
// `network` is one of 'livenet', 'testnet'
//
// 2. Add the storage key to `_upgraders` object using the name of the key as the `_upgrader` object key
// with the value being the name of the upgrade function (e.g., _upgrade_x). In order to avoid conflicts
// when a storage key is involved in multiple upgraders as well as predicte the order in which upgrades
// occur the `_upgrader` object key should be prefixed with '##_' (e.g., '01_') to create a unique and
// sortable name. This format is interpreted by the _upgrade() function.
//
// Upgraders are executed in numerical order per the '##_' object key prefix.
//
var _upgraders = {
'00_bitpayDebitCards' : _upgrade_bitpayDebitCards // 2016-11: Upgrade bitpayDebitCards-x to bitpayAccounts-x
};
function _upgrade_bitpayDebitCards(key, network, cb) {
key += '-' + network;
storage.get(key, function(err, data) {
if (err) return cb(err);
if (data != null) {
// Needs upgrade
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
root.setBitpayDebitCards(network, data, function(err) {
if (err) return cb(err);
storage.remove(key, function() {
cb(null, 'replaced with \'bitpayAccounts\'');
});
});
} else {
cb();
}
});
};
//
////////////////////////////////////////////////////////////////////////////
// IMPORTANT: This function is designed to block execution until it completes.
// Ideally storage should not be used until it has been verified.
root.verify = function(cb) {
_upgrade(function(err) {
cb(err);
});
};
function _handleUpgradeError(key, err) {
$log.error('Failed to upgrade storage for \'' + key + '\': ' + err);
};
function _handleUpgradeSuccess(key, msg) {
$log.info('Storage upgraded for \'' + key + '\': ' + msg);
};
function _upgrade(cb) {
var errorCount = 0;
var errorMessage = undefined;
var keys = Object.keys(_upgraders).sort();
var networks = ['livenet', 'testnet'];
keys.forEach(function(key) {
networks.forEach(function(network) {
var storagekey = key.split('_')[1];
_upgraders[key](storagekey, network, function(err, msg) {
if (err) {
_handleUpgradeError(storagekey, err);
errorCount++;
errorMessage = errorCount + ' storage upgrade failures';
}
if (msg) _handleUpgradeSuccess(storagekey, msg);
});
});
});
cb(errorMessage);
};
root.tryToMigrate = function(cb) {
if (!shouldUseFileStorage) return cb();
@ -349,20 +432,76 @@ angular.module('copayApp.services')
storage.get('bitpayDebitCardsHistory-' + network, cb);
};
root.removeBitpayDebitCardsHistory = function(network, cb) {
storage.remove('bitpayDebitCardsHistory-' + network, cb);
root.removeBitpayDebitCardHistory = function(network, card, cb) {
root.getBitpayDebitCardsHistory(network, function(err, data) {
if (err) return cb(err);
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
delete data[card.eid];
root.setBitpayDebitCardsHistory(network, JSON.stringify(data), cb);
});
};
root.setBitpayDebitCards = function(network, data, cb) {
storage.set('bitpayDebitCards-' + network, data, cb);
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (lodash.isEmpty(data) || !data.email) return cb('No card(s) to set');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
bitpayAccounts = JSON.parse(bitpayAccounts) || {};
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data;
storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb);
});
};
root.getBitpayDebitCards = function(network, cb) {
storage.get('bitpayDebitCards-' + network, cb);
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
bitpayAccounts = JSON.parse(bitpayAccounts) || {};
var cards = [];
Object.keys(bitpayAccounts).forEach(function(email) {
// For the UI, add the account email to the card object.
var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network].cards;
for (var i = 0; i < acctCards.length; i++) {
acctCards[i].email = email;
}
cards = cards.concat(acctCards);
});
cb(err, cards);
});
};
root.removeBitpayDebitCards = function(network, cb) {
storage.remove('bitpayDebitCards-' + network, cb);
root.removeBitpayDebitCard = function(network, card, cb) {
if (lodash.isString(card)) {
card = JSON.parse(card);
}
card = card || {};
if (lodash.isEmpty(card) || !card.eid) return cb('No card to remove');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) cb(err);
bitpayAccounts = JSON.parse(bitpayAccounts) || {};
Object.keys(bitpayAccounts).forEach(function(userId) {
var data = bitpayAccounts[userId]['bitpayDebitCards-' + network];
var newCards = lodash.reject(data.cards, {'eid': card.eid});
data.cards = newCards;
root.setBitpayDebitCards(network, data, function(err) {
if (err) cb(err);
// If there are no more cards in storage then re-enable the next step entry.
root.getBitpayDebitCards(network, function(err, cards){
if (err) cb(err);
if (cards.length == 0) {
root.removeNextStep('BitpayCard', cb);
} else {
cb();
}
});
});
});
});
};
root.setBitpayCardCredentials = function(network, data, cb) {

View File

@ -0,0 +1,15 @@
#bitpayCardPreferences {
.item {
.item-title {
display: block;
}
.item-subtitle {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: $light-gray;
font-size: 14px;
}
}
}

View File

@ -13,6 +13,7 @@
@import "advancedSettings";
@import "bitpayCard";
@import "bitpayCardIntro";
@import "bitpayCardPreferences";
@import "address-book";
@import "wallet-backup-phrase";
@import "zero-state";

View File

@ -2,7 +2,7 @@
<ion-nav-bar class="bar-royal">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-title>BitPay Visa<sup>&reg;</sup> Card</ion-nav-title>
<ion-nav-title>BitPay Visa<sup>&reg;</sup> Card ({{card.lastFourDigits}})</ion-nav-title>
<ion-nav-buttons side="secondary">
<button class="button no-border" ui-sref="tabs.bitpayCard.preferences">
<i class="icon ion-ios-settings"></i>

View File

@ -1,4 +1,4 @@
<ion-view>
<ion-view id="bitpayCardPreferences">
<ion-nav-bar class="bar-royal">
<ion-nav-back-button>
</ion-nav-back-button>
@ -10,12 +10,14 @@
<div class="item item-divider" translate>
Cards
</div>
<div class="item" ng-repeat="card in bitpayCards">
xxxx-xxxx-xxxx-{{card.lastFourDigits}}
</div>
<div class="item item-divider"></div>
<div class="item assertive" ng-click="remove()">
Removes all data from this device
<div class="item item-icon-right" ng-click="remove(card)" ng-repeat="card in bitpayCards">
<span class="item-title">
xxxx-xxxx-xxxx-{{card.lastFourDigits}}
</span>
<span class="item-subtitle">
{{card.email}}
</span>
<i class="icon ion-trash-b assertive"></i>
</div>
</div>
</ion-content>

View File

@ -92,28 +92,25 @@
</p>
<i class="icon bp-arrow-right"></i>
</a>
</div>
</div>
<div class="list card" ng-if="bitpayCardEnabled && bitpayCards[0] && externalServices.BitpayCard">
<div class="item item-icon-right item-heading">
<span translate>Cards</span>
<a ui-sref="tabs.bitpayCardIntro"><i class="icon ion-ios-plus-empty list-add-button"></i></a>
</div>
<div>
<a ng-repeat="card in bitpayCards"
ui-sref="tabs.bitpayCard({id:card.id})"
ng-if="bitpayCardEnabled && bitpayCards[0]"
class="item item-sub item-icon-left item-big-icon-left item-icon-right">
<i class="icon big-icon-svg">
<div class="bg icon-bitpay-card"></div>
</i>
<span>BitPay Visa&reg; Card</span>
<span>BitPay Visa&reg; Card ({{card.lastFourDigits}})</span>
<p>{{cardsHistory[card.id].balance ? '$' + cardsHistory[card.id].balance : 'Add funds to get started'|translate}}</p>
<i class="icon bp-arrow-right"></i>
</a>
<a ui-sref="tabs.bitpayCardIntro"
ng-if="bitpayCardEnabled && !bitpayCards[0] && externalServices.BitpayCard"
class="item item-sub item-icon-left item-big-icon-left item-icon-right">
<i class="icon big-icon-svg">
<div class="bg icon-bitpay-card"></div>
</i>
<span>BitPay Visa&reg; Card</span>
<p translate>Add your card</p>
<i class="icon bp-arrow-right"></i>
</a>
</div>
</div>