Wallet/src/js/controllers/tab-home.js

276 lines
8.6 KiB
JavaScript
Raw Normal View History

2016-08-12 10:54:31 -03:00
'use strict';
angular.module('copayApp.controllers').controller('tabHomeController',
2016-10-26 12:56:33 -03:00
function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, gettextCatalog, lodash, popupService, ongoingProcess, externalLinkService, latestReleaseService, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, $window, bitpayCardService, startupService, addressbookService) {
2016-09-21 16:04:25 -03:00
var wallet;
var listeners = [];
var notifications = [];
2016-08-31 18:43:58 -03:00
$scope.externalServices = {};
2016-09-06 11:22:10 -03:00
$scope.openTxpModal = txpModalService.open;
$scope.version = $window.version;
$scope.name = $window.appConfig.nameCase;
$scope.homeTip = $stateParams.fromOnboarding;
2016-10-11 12:59:02 -03:00
$scope.isCordova = platformInfo.isCordova;
$scope.isAndroid = platformInfo.isAndroid;
2016-10-26 12:56:33 -03:00
$scope.isNW = platformInfo.isNW;
$scope.hideRateCard = {};
2016-09-06 11:22:10 -03:00
$scope.$on("$ionicView.afterEnter", function() {
startupService.ready();
});
2016-11-02 10:52:04 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
if (!$scope.homeTip) {
storageService.getHomeTipAccepted(function(error, value) {
$scope.homeTip = (value == 'accepted') ? false : true;
});
}
if ($scope.isNW) {
latestReleaseService.checkLatestRelease(function(err, newRelease) {
if (err) {
$log.warn(err);
return;
}
if (newRelease) $scope.newRelease = true;
});
}
2016-11-08 12:37:05 -03:00
storageService.getProfileCreationTime(function(error, time) {
var now = moment().unix() * 1000;
storageService.getRateCardFlag(function(error, value) {
$scope.hideRateCard.value = (value == 'true' || (time - now) > 0) ? true : false;
});
2016-11-02 15:30:14 -03:00
});
2016-11-02 10:52:04 -03:00
});
$scope.$on("$ionicView.enter", function(event, data) {
updateAllWallets();
addressbookService.list(function(err, ab) {
if (err) $log.error(err);
$scope.addressbook = ab || {};
2016-10-11 12:59:02 -03:00
});
2016-11-02 10:52:04 -03:00
listeners = [
$rootScope.$on('bwsEvent', function(e, walletId, type, n) {
var wallet = profileService.getWallet(walletId);
updateWallet(wallet);
if ($scope.recentTransactionsEnabled) getNotifications();
}),
$rootScope.$on('Local/TxAction', function(e, walletId) {
$log.debug('Got action for wallet ' + walletId);
var wallet = profileService.getWallet(walletId);
updateWallet(wallet);
if ($scope.recentTransactionsEnabled) getNotifications();
})
];
configService.whenAvailable(function() {
nextStep(function() {
var config = configService.getSync();
var isWindowsPhoneApp = platformInfo.isWP && platformInfo.isCordova;
$scope.glideraEnabled = config.glidera.enabled && !isWindowsPhoneApp;
$scope.coinbaseEnabled = config.coinbase.enabled && !isWindowsPhoneApp;
$scope.amazonEnabled = config.amazon.enabled;
$scope.bitpayCardEnabled = config.bitpayCard.enabled;
var buyAndSellEnabled = !$scope.externalServices.BuyAndSell && ($scope.glideraEnabled || $scope.coinbaseEnabled);
var amazonEnabled = !$scope.externalServices.AmazonGiftCards && $scope.amazonEnabled;
var bitpayCardEnabled = !$scope.externalServices.BitpayCard && $scope.bitpayCardEnabled;
$scope.nextStepEnabled = buyAndSellEnabled || amazonEnabled || bitpayCardEnabled;
$scope.recentTransactionsEnabled = config.recentTransactions.enabled;
if ($scope.recentTransactionsEnabled) getNotifications();
2016-10-26 12:56:33 -03:00
2016-11-02 10:52:04 -03:00
if ($scope.bitpayCardEnabled) bitpayCardCache();
$timeout(function() {
$ionicScrollDelegate.resize();
$scope.$apply();
}, 10);
});
2016-10-26 12:56:33 -03:00
});
2016-11-02 10:52:04 -03:00
});
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
});
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
});
2016-10-26 12:56:33 -03:00
$scope.openExternalLink = function(url, optIn, title, message, okText, cancelText) {
externalLinkService.open(url, optIn, title, message, okText, cancelText);
};
2016-09-21 11:38:38 -03:00
$scope.openNotificationModal = function(n) {
2016-09-21 16:04:25 -03:00
wallet = profileService.getWallet(n.walletId);
2016-09-21 12:26:44 -03:00
if (n.txid) {
$state.transitionTo('tabs.wallet.tx-details', {
txid: n.txid,
walletId: n.walletId
});
2016-09-21 11:38:38 -03:00
} else {
var txp = lodash.find($scope.txps, {
id: n.txpId
});
2016-09-21 16:04:25 -03:00
if (txp) {
txpModalService.open(txp);
} else {
ongoingProcess.set('loadingTxInfo', true);
walletService.getTxp(wallet, n.txpId, function(err, txp) {
var _txp = txp;
ongoingProcess.set('loadingTxInfo', false);
if (err) {
$log.warn('No txp found');
2016-10-26 17:10:21 -03:00
return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Transaction not found'));
2016-09-21 16:04:25 -03:00
}
txpModalService.open(_txp);
});
2016-09-21 11:43:40 -03:00
}
2016-09-21 11:38:38 -03:00
}
};
2016-09-06 11:22:10 -03:00
$scope.openWallet = function(wallet) {
if (!wallet.isComplete()) {
return $state.go('tabs.copayers', {
walletId: wallet.credentials.walletId
});
}
2016-09-28 11:08:08 -03:00
$state.go('tabs.wallet', {
2016-09-06 11:22:10 -03:00
walletId: wallet.credentials.walletId
});
};
2016-09-01 17:41:00 -03:00
var updateTxps = function() {
2016-09-02 14:33:18 -03:00
profileService.getTxps({
limit: 3
}, function(err, txps, n) {
2016-09-06 11:22:10 -03:00
if (err) $log.error(err);
2016-09-02 14:33:18 -03:00
$scope.txps = txps;
$scope.txpsN = n;
$timeout(function() {
$ionicScrollDelegate.resize();
$scope.$apply();
}, 100);
2016-09-02 14:33:18 -03:00
})
2016-08-22 17:43:31 -03:00
};
2016-08-12 10:54:31 -03:00
var updateAllWallets = function() {
2016-08-25 16:14:13 -03:00
$scope.wallets = profileService.getWallets();
if (lodash.isEmpty($scope.wallets)) return;
2016-08-31 18:12:28 -03:00
var i = $scope.wallets.length;
var j = 0;
var timeSpan = 60 * 60 * 24 * 7;
2016-08-31 18:12:28 -03:00
lodash.each($scope.wallets, function(wallet) {
walletService.getStatus(wallet, {}, function(err, status) {
2016-08-31 17:12:36 -03:00
if (err) {
2016-11-14 12:53:43 -03:00
if (err === 'WALLET_NOT_REGISTERED') wallet.error = gettextCatalog.getString('Wallet not registered');
else wallet.error = err;
2016-09-21 19:36:24 -03:00
$log.error(err);
2016-09-01 19:14:18 -03:00
} else {
2016-11-14 12:53:43 -03:00
wallet.error = null;
2016-09-01 19:14:18 -03:00
wallet.status = status;
}
2016-09-06 11:22:10 -03:00
if (++j == i) {
2016-09-02 14:33:18 -03:00
updateTxps();
2016-08-31 17:12:36 -03:00
}
2016-08-31 18:12:28 -03:00
});
});
};
2016-09-01 16:50:13 -03:00
var updateWallet = function(wallet) {
$log.debug('Updating wallet:' + wallet.name)
walletService.getStatus(wallet, {}, function(err, status) {
if (err) {
$log.error(err);
return;
}
wallet.status = status;
updateTxps();
});
};
var getNotifications = function() {
2016-09-01 19:14:18 -03:00
profileService.getNotifications({
2016-09-01 16:50:13 -03:00
limit: 3
2016-09-01 19:14:18 -03:00
}, function(err, n) {
2016-09-01 16:50:13 -03:00
if (err) {
$log.error(err);
2016-09-01 19:14:18 -03:00
return;
2016-09-01 16:50:13 -03:00
}
2016-09-01 19:14:18 -03:00
$scope.notifications = n;
2016-09-01 17:41:00 -03:00
$timeout(function() {
$ionicScrollDelegate.resize();
2016-09-01 17:41:00 -03:00
$scope.$apply();
}, 100);
2016-08-22 22:10:46 -03:00
});
};
2016-09-19 16:31:36 -03:00
$scope.hideHomeTip = function() {
2016-10-14 10:05:04 -03:00
storageService.setHomeTipAccepted('accepted', function() {
$scope.homeTip = false;
2016-10-11 17:22:37 -03:00
$timeout(function() {
$scope.$apply();
})
2016-09-19 16:31:36 -03:00
});
};
2016-10-27 11:45:38 -03:00
var nextStep = function(cb) {
2016-10-27 12:31:28 -03:00
var i = 0;
2016-10-27 11:45:38 -03:00
var services = ['AmazonGiftCards', 'BitpayCard', 'BuyAndSell'];
lodash.each(services, function(service) {
2016-08-26 11:11:14 -03:00
storageService.getNextStep(service, function(err, value) {
$scope.externalServices[service] = value ? true : false;
2016-10-27 12:31:28 -03:00
if (++i == services.length) return cb();
2016-08-26 11:11:14 -03:00
});
});
};
2016-08-22 22:10:46 -03:00
2016-09-26 16:44:47 -03:00
$scope.shouldHideNextSteps = function() {
$scope.hideNextSteps = !$scope.hideNextSteps;
$timeout(function() {
$ionicScrollDelegate.resize();
2016-09-29 19:24:33 -03:00
$scope.$apply();
}, 100);
2016-09-26 16:44:47 -03:00
};
2016-09-28 21:09:41 -03:00
var bitpayCardCache = function() {
2016-10-06 19:23:39 -03:00
bitpayCardService.getBitpayDebitCards(function(err, data) {
if (err) return;
if (lodash.isEmpty(data)) {
$scope.bitpayCards = null;
return;
}
2016-10-06 19:23:39 -03:00
$scope.bitpayCards = data.cards;
});
2016-10-07 13:51:55 -03:00
bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) {
if (err) return;
if (lodash.isEmpty(data)) {
$scope.cardsHistory = null;
return;
}
2016-10-07 13:51:55 -03:00
$scope.cardsHistory = data;
2016-09-28 21:09:41 -03:00
});
};
2016-10-11 12:59:02 -03:00
$scope.onRefresh = function() {
2016-10-14 10:25:52 -03:00
$timeout(function() {
$scope.$broadcast('scroll.refreshComplete');
}, 300);
2016-10-11 12:59:02 -03:00
updateAllWallets();
};
2016-08-12 10:54:31 -03:00
});