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

152 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-08-12 16:04:17 -03:00
'use strict';
2016-11-24 11:42:25 -03:00
angular.module('copayApp.controllers').controller('tabReceiveController', function($rootScope, $scope, $timeout, $log, $ionicModal, $state, $ionicHistory, $ionicPopover, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService, bwcError) {
2016-08-15 11:56:59 -03:00
2016-11-14 10:09:11 -03:00
var listeners = [];
2016-08-15 11:56:59 -03:00
$scope.isCordova = platformInfo.isCordova;
2016-09-21 17:12:25 -03:00
$scope.isNW = platformInfo.isNW;
2016-08-12 16:04:17 -03:00
2017-05-03 16:25:27 -03:00
$scope.requestSpecificAmount = function() {
$state.go('tabs.receive.amount', {
customAmount: true,
toAddress: $scope.addr
});
2016-08-12 16:04:17 -03:00
};
2017-05-03 17:08:18 -03:00
$scope.setAddress = function() {
2016-10-26 12:03:55 -03:00
if (!$scope.wallet || $scope.generatingAddress || !$scope.wallet.isComplete()) return;
2016-08-30 15:43:17 -03:00
$scope.addr = null;
2016-08-12 16:04:17 -03:00
$scope.generatingAddress = true;
2017-05-03 17:08:18 -03:00
walletService.getAddress($scope.wallet, false, function(err, addr) {
2016-09-29 19:52:26 -03:00
$scope.generatingAddress = false;
2017-01-16 16:00:09 -03:00
if (err) {
2017-01-16 16:00:09 -03:00
//Error is already formated
return popupService.showAlert(err);
}
2016-09-29 19:52:26 -03:00
$scope.addr = addr;
2016-10-17 09:55:20 -03:00
$timeout(function() {
2016-10-15 10:28:30 -03:00
$scope.$apply();
2016-10-17 09:55:20 -03:00
}, 10);
2016-09-23 12:07:56 -03:00
});
2016-09-29 19:52:26 -03:00
};
2016-09-26 11:26:10 -03:00
$scope.goCopayers = function() {
$ionicHistory.removeBackView();
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home');
$timeout(function() {
$state.transitionTo('tabs.copayers', {
walletId: $scope.wallet.credentials.walletId
});
}, 100);
};
2016-09-26 16:11:45 -03:00
$scope.openBackupNeededModal = function() {
2016-09-27 10:59:08 -03:00
$ionicModal.fromTemplateUrl('views/includes/backupNeededPopup.html', {
scope: $scope,
backdropClickToClose: false,
hardwareBackButtonClose: false
}).then(function(modal) {
$scope.BackupNeededModal = modal;
$scope.BackupNeededModal.show();
});
2016-09-26 13:05:15 -03:00
};
2016-09-26 16:11:45 -03:00
$scope.close = function() {
$scope.BackupNeededModal.hide();
$scope.BackupNeededModal.remove();
};
$scope.doBackup = function() {
$scope.close();
$scope.goToBackupFlow();
2016-09-26 13:05:15 -03:00
};
$scope.goToBackupFlow = function() {
2016-09-26 16:11:45 -03:00
$state.go('tabs.receive.backupWarning', {
from: 'tabs.receive',
2016-09-26 13:05:15 -03:00
walletId: $scope.wallet.credentials.walletId
});
2016-09-29 19:52:26 -03:00
};
2017-05-03 16:25:27 -03:00
$scope.updateCurrentWallet = function(wallet) {
walletService.getStatus(wallet, {}, function(err, status) {
2016-11-14 10:09:11 -03:00
if (err) {
2017-01-16 16:00:09 -03:00
return popupService.showAlert(bwcError.msg(err, gettextCatalog.getString('Could not update wallet')));
2016-11-14 10:09:11 -03:00
}
$timeout(function() {
2017-05-03 16:25:27 -03:00
$scope.wallet = profileService.getWallet(wallet.id);
2016-11-14 10:09:11 -03:00
$scope.wallet.status = status;
$scope.setAddress();
$scope.$apply();
}, 200);
});
};
2016-12-05 17:33:46 -05:00
$scope.shouldShowReceiveAddressFromHardware = function() {
var wallet = $scope.wallet;
if (wallet.isPrivKeyExternal() && wallet.credentials.hwInfo) {
return (wallet.credentials.hwInfo.name == walletService.externalSource.intelTEE.id);
} else {
return false;
}
};
$scope.showReceiveAddressFromHardware = function() {
var wallet = $scope.wallet;
if (wallet.isPrivKeyExternal() && wallet.credentials.hwInfo) {
2017-05-03 16:25:27 -03:00
walletService.showReceiveAddressFromHardware(wallet, $scope.addr, function() {});
2016-12-05 17:33:46 -05:00
}
};
2016-09-29 19:52:26 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.wallets = profileService.getWallets();
2017-05-03 16:25:27 -03:00
$scope.singleWallet = $scope.wallets.length == 1;
2016-11-14 10:09:11 -03:00
listeners = [
$rootScope.$on('bwsEvent', function(e, walletId, type, n) {
// Update current address
2017-05-03 16:25:27 -03:00
if ($scope.wallet && walletId == $scope.wallet.id) $scope.updateCurrentWallet($scope.wallet);
2016-11-14 10:09:11 -03:00
})
2017-05-03 16:25:27 -03:00
];
2016-11-14 10:09:11 -03:00
2017-05-03 16:25:27 -03:00
if (!$scope.wallets[0]) return;
if (!$scope.wallet) return $scope.init();
var w = lodash.find($scope.wallets, function(w) {
return w.id == $scope.wallet.id;
});
if (w) $scope.updateCurrentWallet($scope.wallet);
else $scope.init();
2016-11-14 10:09:11 -03:00
});
2017-05-03 16:25:27 -03:00
$scope.init = function() {
$scope.wallet = $scope.wallets[0];
$scope.updateCurrentWallet($scope.wallet);
};
2016-11-14 10:09:11 -03:00
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
2016-09-29 19:52:26 -03:00
});
2017-05-03 16:25:27 -03:00
$scope.onWalletSelect = function(wallet) {
$scope.updateCurrentWallet(wallet);
};
$scope.showWalletSelector = function() {
if ($scope.singleWallet) return;
$scope.walletSelectorTitle = gettextCatalog.getString('Address from');
$scope.showWallets = true;
};
$scope.copyToClipboard = function() {
if ($scope.isCordova) return 'bitcoin:' + $scope.addr;
else return $scope.addr;
}
2016-08-12 16:04:17 -03:00
});