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

261 lines
7.9 KiB
JavaScript
Raw Normal View History

2016-08-12 10:54:31 -03:00
'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, bitpayCardService, startupService) {
2016-09-21 16:04:25 -03:00
var wallet;
var listeners = [];
var notifications = [];
2016-08-31 18:43:58 -03:00
$scope.externalServices = {};
$scope.bitpayCardEnabled = true; // TODO
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-09-06 11:22:10 -03:00
$scope.$on("$ionicView.afterEnter", function() {
startupService.ready();
});
2016-10-11 12:59:02 -03:00
if (!$scope.homeTip) {
storageService.getHomeTipAccepted(function(error, value) {
2016-10-14 10:05:04 -03:00
$scope.homeTip = (value == 'accepted') ? false : true;
2016-10-11 12:59:02 -03:00
});
}
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) {
2016-09-21 11:38:38 -03:00
openTxModal(n);
} 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');
return popupService.showAlert(null, gettextCatalog.getString('Transaction not found'));
}
txpModalService.open(_txp);
});
2016-09-21 11:43:40 -03:00
}
2016-09-21 11:38:38 -03:00
}
};
var openTxModal = function(n) {
2016-09-21 16:04:25 -03:00
wallet = profileService.getWallet(n.walletId);
2016-09-20 16:21:05 -03:00
ongoingProcess.set('loadingTxInfo', true);
walletService.getTx(wallet, n.txid, function(err, tx) {
ongoingProcess.set('loadingTxInfo', false);
if (err) {
$log.error(err);
return popupService.showAlert(gettextCatalog.getString('Error'), err);
}
if (!tx) {
$log.warn('No tx found');
2016-09-21 12:38:49 -03:00
return popupService.showAlert(null, gettextCatalog.getString('Transaction not found'));
2016-09-20 16:21:05 -03:00
}
2016-09-22 12:08:15 -03:00
$scope.wallet = wallet;
$scope.btx = lodash.cloneDeep(tx);
$ionicModal.fromTemplateUrl('views/modals/tx-details.html', {
scope: $scope
}).then(function(modal) {
$scope.txDetailsModal = modal;
$scope.txDetailsModal.show();
});
2016-09-21 17:28:59 -03:00
walletService.getTxNote(wallet, n.txid, function(err, note) {
if (err) $log.debug(gettextCatalog.getString('Could not fetch transaction note'));
$scope.btx.note = note;
2016-09-20 16:21:05 -03:00
});
2016-09-20 12:02:45 -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();
}, 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-09-21 19:36:24 -03:00
$log.error(err);
2016-09-01 19:14:18 -03:00
} else {
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
if (!$scope.recentTransactionsEnabled) return;
2016-09-01 16:50:13 -03:00
$scope.fetchingNotifications = true;
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.fetchingNotifications = false;
$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-09-01 17:41:00 -03:00
2016-09-01 16:50:13 -03:00
})
};
2016-08-24 17:54:01 -03:00
var updateWallet = function(wallet) {
2016-08-23 12:41:41 -03:00
$log.debug('Updating wallet:' + wallet.name)
2016-08-22 22:10:46 -03:00
walletService.getStatus(wallet, {}, function(err, status) {
if (err) {
$log.error(err);
2016-08-22 22:10:46 -03:00
return;
2016-08-23 12:41:41 -03:00
}
2016-08-22 22:10:46 -03:00
wallet.status = status;
updateTxps();
if (!$scope.recentTransactionsEnabled) return;
2016-08-31 18:12:28 -03:00
2016-09-02 14:17:47 -03:00
$scope.fetchingNotifications = true;
2016-09-01 16:50:13 -03:00
profileService.getNotifications({
limit: 3
2016-09-01 19:14:18 -03:00
}, function(err, notifications) {
2016-09-02 14:17:47 -03:00
$scope.fetchingNotifications = false;
2016-09-01 16:50:13 -03:00
if (err) {
$log.error(err);
2016-09-01 16:50:13 -03:00
return;
}
2016-09-01 19:14:18 -03:00
$scope.notifications = notifications;
});
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
});
};
var nextStep = function() {
2016-08-26 11:11:14 -03:00
lodash.each(['AmazonGiftCards', 'BitpayCard', 'BuyAndSell'], function(service) {
storageService.getNextStep(service, function(err, value) {
$scope.externalServices[service] = value ? true : false;
$timeout(function() {
$ionicScrollDelegate.resize();
}, 10);
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() {
bitpayCardService.getCacheData(function(err, data) {
if (err ||  lodash.isEmpty(data)) return;
2016-09-28 21:09:41 -03:00
$scope.bitpayCard = data;
});
};
2016-10-11 12:59:02 -03:00
$scope.onRefresh = function() {
$scope.$broadcast('scroll.refreshComplete');
updateAllWallets();
};
2016-09-22 16:04:05 -03:00
$scope.$on("$ionicView.enter", function(event, data) {
2016-09-28 21:09:41 -03:00
$scope.bitpayCard = null;
nextStep();
updateAllWallets();
listeners = [
$rootScope.$on('bwsEvent', function(e, walletId, type, n) {
var wallet = profileService.getWallet(walletId);
updateWallet(wallet);
}),
$rootScope.$on('Local/TxAction', function(e, walletId) {
$log.debug('Got action for wallet ' + walletId);
var wallet = profileService.getWallet(walletId);
updateWallet(wallet);
})
];
configService.whenAvailable(function() {
var config = configService.getSync();
var isWindowsPhoneApp = platformInfo.isWP && platformInfo.isCordova;
2016-09-30 17:00:45 -03:00
$scope.glideraEnabled = config.glidera.enabled && !isWindowsPhoneApp;
$scope.coinbaseEnabled = config.coinbase.enabled && !isWindowsPhoneApp;
2016-09-27 15:57:39 -03:00
$scope.amazonEnabled = config.amazon.enabled;
$scope.bitpayCardEnabled = config.bitpayCard.enabled;
2016-09-30 17:00:45 -03:00
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;
2016-09-27 15:57:39 -03:00
$scope.recentTransactionsEnabled = config.recentTransactions.enabled;
2016-09-28 21:09:41 -03:00
if ($scope.bitpayCardEnabled) bitpayCardCache();
});
2016-09-21 17:12:25 -03:00
});
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
});
2016-08-12 10:54:31 -03:00
});