2016-08-12 10:54:31 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('tabHomeController',
|
2017-11-13 15:07:25 +09:00
|
|
|
function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, $window, gettextCatalog, lodash, popupService, ongoingProcess, externalLinkService, latestReleaseService, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, appConfigService, startupService, addressbookService, feedbackService, bwcError, nextStepsService, buyAndSellService, homeIntegrationsService, bitpayCardService, pushNotificationsService, timeService, bitcoincomService, pricechartService, firebaseEventsService) {
|
2016-09-21 16:04:25 -03:00
|
|
|
var wallet;
|
2016-09-29 19:04:52 -03:00
|
|
|
var listeners = [];
|
2016-09-30 12:27:31 -03:00
|
|
|
var notifications = [];
|
2016-08-31 18:43:58 -03:00
|
|
|
$scope.externalServices = {};
|
2016-09-06 11:22:10 -03:00
|
|
|
$scope.openTxpModal = txpModalService.open;
|
2016-12-27 17:05:02 -03:00
|
|
|
$scope.version = $window.version;
|
2016-12-27 15:19:53 -03:00
|
|
|
$scope.name = appConfigService.nameCase;
|
2016-09-19 12:06:46 -03:00
|
|
|
$scope.homeTip = $stateParams.fromOnboarding;
|
2016-10-11 12:59:02 -03:00
|
|
|
$scope.isCordova = platformInfo.isCordova;
|
2016-10-14 10:24:20 -03:00
|
|
|
$scope.isAndroid = platformInfo.isAndroid;
|
2017-06-08 15:21:38 -03:00
|
|
|
$scope.isWindowsPhoneApp = platformInfo.isCordova && platformInfo.isWP;
|
2016-10-26 12:56:33 -03:00
|
|
|
$scope.isNW = platformInfo.isNW;
|
2016-11-11 17:04:53 -03:00
|
|
|
$scope.showRateCard = {};
|
2016-09-06 11:22:10 -03:00
|
|
|
|
2016-10-11 22:46:07 -04: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;
|
|
|
|
|
}
|
2017-01-25 09:40:31 -03:00
|
|
|
if (newRelease) {
|
|
|
|
|
$scope.newRelease = true;
|
2017-01-26 09:26:14 -03:00
|
|
|
$scope.updateText = gettextCatalog.getString('There is a new version of {{appName}} available', {
|
|
|
|
|
appName: $scope.name
|
|
|
|
|
});
|
2017-01-25 09:40:31 -03:00
|
|
|
}
|
2016-11-02 10:52:04 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 17:04:53 -03:00
|
|
|
storageService.getFeedbackInfo(function(error, info) {
|
2017-06-08 15:21:38 -03:00
|
|
|
|
|
|
|
|
if ($scope.isWindowsPhoneApp) {
|
|
|
|
|
$scope.showRateCard.value = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-11 17:04:53 -03:00
|
|
|
if (!info) {
|
|
|
|
|
initFeedBackInfo();
|
|
|
|
|
} else {
|
|
|
|
|
var feedbackInfo = JSON.parse(info);
|
2016-11-11 17:05:06 -03:00
|
|
|
//Check if current version is greater than saved version
|
2016-12-27 17:05:02 -03:00
|
|
|
var currentVersion = $scope.version;
|
2016-11-11 17:04:53 -03:00
|
|
|
var savedVersion = feedbackInfo.version;
|
|
|
|
|
var isVersionUpdated = feedbackService.isVersionUpdated(currentVersion, savedVersion);
|
|
|
|
|
if (!isVersionUpdated) {
|
|
|
|
|
initFeedBackInfo();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var now = moment().unix();
|
2016-12-16 11:28:57 -03:00
|
|
|
var timeExceeded = (now - feedbackInfo.time) >= 24 * 7 * 60 * 60;
|
2016-11-11 17:04:53 -03:00
|
|
|
$scope.showRateCard.value = timeExceeded && !feedbackInfo.sent;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-02 15:30:14 -03:00
|
|
|
});
|
|
|
|
|
|
2016-11-11 17:04:53 -03:00
|
|
|
function initFeedBackInfo() {
|
|
|
|
|
var feedbackInfo = {};
|
|
|
|
|
feedbackInfo.time = moment().unix();
|
2016-12-27 17:05:02 -03:00
|
|
|
feedbackInfo.version = $scope.version;
|
2016-11-11 17:04:53 -03:00
|
|
|
feedbackInfo.sent = false;
|
|
|
|
|
storageService.setFeedbackInfo(JSON.stringify(feedbackInfo), function() {
|
|
|
|
|
$scope.showRateCard.value = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-11-02 10:52:04 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.enter", function(event, data) {
|
2017-02-23 11:13:14 -03:00
|
|
|
updateAllWallets();
|
2016-11-02 10:52:04 -03:00
|
|
|
|
|
|
|
|
addressbookService.list(function(err, ab) {
|
|
|
|
|
if (err) $log.error(err);
|
|
|
|
|
$scope.addressbook = ab || {};
|
2016-10-11 12:59:02 -03:00
|
|
|
});
|
2016-10-10 13:06:29 -04: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();
|
2017-01-30 18:04:17 -03:00
|
|
|
|
2016-11-02 10:52:04 -03:00
|
|
|
}),
|
|
|
|
|
$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();
|
|
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 18:04:17 -03:00
|
|
|
$scope.buyAndSellItems = buyAndSellService.getLinked();
|
2017-01-30 19:38:52 -03:00
|
|
|
$scope.homeIntegrations = homeIntegrationsService.get();
|
2016-11-02 10:52:04 -03:00
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
bitpayCardService.get({}, function(err, cards) {
|
2017-01-31 09:30:27 -03:00
|
|
|
$scope.bitpayCardItems = cards;
|
|
|
|
|
});
|
2016-11-02 10:52:04 -03:00
|
|
|
|
2017-02-03 18:03:29 -03:00
|
|
|
configService.whenAvailable(function(config) {
|
2017-01-30 18:04:17 -03:00
|
|
|
$scope.recentTransactionsEnabled = config.recentTransactions.enabled;
|
|
|
|
|
if ($scope.recentTransactionsEnabled) getNotifications();
|
2016-11-02 10:52:04 -03:00
|
|
|
|
2017-01-31 17:45:19 -03:00
|
|
|
if (config.hideNextSteps.enabled) {
|
|
|
|
|
$scope.nextStepsItems = null;
|
|
|
|
|
} else {
|
2017-01-31 16:32:45 -03:00
|
|
|
$scope.nextStepsItems = nextStepsService.get();
|
|
|
|
|
}
|
2016-10-26 12:56:33 -03:00
|
|
|
|
2017-02-03 18:03:29 -03:00
|
|
|
pushNotificationsService.init();
|
2017-11-13 15:07:25 +09:00
|
|
|
firebaseEventsService.init();
|
2017-02-03 18:03:29 -03:00
|
|
|
|
2017-01-30 18:04:17 -03:00
|
|
|
$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();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-12-14 15:18:52 -03:00
|
|
|
$scope.createdWithinPastDay = function(time) {
|
2017-06-08 10:38:58 -04:00
|
|
|
return timeService.withinPastDay(time);
|
2016-12-14 15:18:52 -03:00
|
|
|
};
|
|
|
|
|
|
2016-12-12 17:29:11 -03:00
|
|
|
$scope.openExternalLink = function() {
|
|
|
|
|
var url = 'https://github.com/bitpay/copay/releases/latest';
|
|
|
|
|
var optIn = true;
|
|
|
|
|
var title = gettextCatalog.getString('Update Available');
|
|
|
|
|
var message = gettextCatalog.getString('An update to this app is available. For your security, please update to the latest version.');
|
|
|
|
|
var okText = gettextCatalog.getString('View Update');
|
|
|
|
|
var cancelText = gettextCatalog.getString('Go Back');
|
2016-10-26 12:56:33 -03:00
|
|
|
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) {
|
2016-11-10 13:47:57 -03:00
|
|
|
$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
|
|
|
|
2016-09-29 19:04:52 -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() {
|
2016-09-28 15:45:21 -03:00
|
|
|
$ionicScrollDelegate.resize();
|
2016-11-10 13:47:57 -03:00
|
|
|
$scope.$apply();
|
2016-12-14 15:37:15 -03:00
|
|
|
}, 10);
|
2016-09-02 14:33:18 -03:00
|
|
|
})
|
2016-08-22 17:43:31 -03:00
|
|
|
};
|
2016-08-12 10:54:31 -03:00
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
var updateAllWallets = function() {
|
2017-10-05 15:18:30 -03:00
|
|
|
var wallets = [];
|
|
|
|
|
$scope.walletsBtc = profileService.getWallets({coin: 'btc'});
|
|
|
|
|
$scope.walletsBch = profileService.getWallets({coin: 'bch'});
|
2016-08-24 19:00:50 -03:00
|
|
|
|
2017-10-05 15:18:30 -03:00
|
|
|
lodash.each($scope.walletsBtc, function(wBtc) {
|
|
|
|
|
wallets.push(wBtc);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
lodash.each($scope.walletsBch, function(wBch) {
|
|
|
|
|
wallets.push(wBch);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (lodash.isEmpty(wallets)) return;
|
|
|
|
|
|
|
|
|
|
var i = wallets.length;
|
2016-08-31 18:12:28 -03:00
|
|
|
var j = 0;
|
2016-08-24 19:00:50 -03:00
|
|
|
|
2017-10-05 15:18:30 -03:00
|
|
|
lodash.each(wallets, function(wallet) {
|
2016-08-31 18:12:28 -03:00
|
|
|
walletService.getStatus(wallet, {}, function(err, status) {
|
2016-08-31 17:12:36 -03:00
|
|
|
if (err) {
|
2017-01-16 16:00:09 -03:00
|
|
|
|
2017-01-25 09:40:31 -03:00
|
|
|
wallet.error = (err === 'WALLET_NOT_REGISTERED') ? gettextCatalog.getString('Wallet not registered') : bwcError.msg(err);
|
2017-01-16 16:00:09 -03:00
|
|
|
|
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;
|
2017-01-31 14:24:13 -03:00
|
|
|
|
|
|
|
|
// TODO service refactor? not in profile service
|
|
|
|
|
profileService.setLastKnownBalance(wallet.id, wallet.status.totalBalanceStr, function() {});
|
2016-09-01 19:14:18 -03:00
|
|
|
}
|
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-11-10 13:47:57 -03:00
|
|
|
};
|
2016-09-01 16:50:13 -03:00
|
|
|
|
2016-11-10 13:47:57 -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
|
2017-01-18 11:00:51 -03:00
|
|
|
}, function(err, notifications, total) {
|
2016-09-01 16:50:13 -03:00
|
|
|
if (err) {
|
2016-09-29 19:04:52 -03:00
|
|
|
$log.error(err);
|
2016-09-01 19:14:18 -03:00
|
|
|
return;
|
2016-09-01 16:50:13 -03:00
|
|
|
}
|
2017-06-18 17:45:46 +09:00
|
|
|
|
|
|
|
|
var txIdList = [];
|
|
|
|
|
|
|
|
|
|
var notificationsBeforeCheck = notifications.length;
|
2017-11-13 15:07:25 +09:00
|
|
|
|
2017-06-18 17:45:46 +09:00
|
|
|
for (var i=0; i<notifications.length; i++) {
|
|
|
|
|
var txId = notifications[i].txid;
|
|
|
|
|
if (txIdList.includes(txId)) {
|
|
|
|
|
notifications.splice(i, 1);
|
|
|
|
|
i = i - 1;
|
|
|
|
|
} else {
|
|
|
|
|
txIdList.push(txId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var notificationsAfterCheck = notifications.length;
|
|
|
|
|
var removedNotifications = notificationsBeforeCheck - notificationsAfterCheck;
|
|
|
|
|
|
|
|
|
|
if (notificationsBeforeCheck != notificationsAfterCheck) {
|
|
|
|
|
console.log("Found a redundant notification. Removed " + removedNotifications);
|
|
|
|
|
total = total - removedNotifications;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 11:00:51 -03:00
|
|
|
$scope.notifications = notifications;
|
|
|
|
|
$scope.notificationsN = total;
|
2016-09-01 17:41:00 -03:00
|
|
|
$timeout(function() {
|
2016-09-28 15:45:21 -03:00
|
|
|
$ionicScrollDelegate.resize();
|
2016-09-01 17:41:00 -03:00
|
|
|
$scope.$apply();
|
2016-12-14 15:37:15 -03:00
|
|
|
}, 10);
|
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() {
|
2016-10-10 13:06:29 -04:00
|
|
|
$scope.homeTip = false;
|
2016-10-11 17:22:37 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
})
|
2016-09-19 16:31:36 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
});
|