Skip to content
Open
137 changes: 88 additions & 49 deletions src/js/controllers/tab-send.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

angular.module('copayApp.controllers').controller('tabSendController', function tabSendController(
bitcoinUriService
bitcoinUriService
, externalLinkService
, $scope
, $log
Expand All @@ -21,28 +21,26 @@ angular.module('copayApp.controllers').controller('tabSendController', function
, clipboardService
, incomingDataService
, moonPayService
, ionicToast
, opencapService
) {

var clipboardHasAddress = false;
var clipboardHasContent = false;
var originalList;
var isBuyBitcoinAllowed = false;
$scope.displayBalanceAsFiat = true;
$scope.walletSelectorTitleForce = true;

moonPayService.getCountryByIpAddress().then(function onGetCountryByIpAddress(user) {
isBuyBitcoinAllowed = user.isAllowed;
});

$scope.addContact = function() {
$state.go('tabs.send.addressbook');
$scope.addContact = function () {
$state.go('tabs.send.addressbook');
};

$scope.pasteClipboard = function() {
$scope.pasteClipboard = function () {
if ($scope.clipboardHasAddress || $scope.clipboardHasContent) {
clipboardService.readFromClipboard(function(text) {
$scope.$apply(function() {
$scope.formData.search = text;
$scope.findContact($scope.formData.search);
clipboardService.readFromClipboard(function (text) {
$scope.$apply(function () {
$scope.formData.search = text;
$scope.findContact($scope.formData.search);
});
});
} else {
Expand All @@ -53,12 +51,12 @@ angular.module('copayApp.controllers').controller('tabSendController', function
}
};

$scope.$on("$ionicView.enter", function(event, data) {
$scope.$on("$ionicView.enter", function (event, data) {

var stateParams = sendFlowService.state.getClone();
$scope.fromWallet = profileService.getWallet(stateParams.fromWalletId);

clipboardService.readFromClipboard(function(text) {
clipboardService.readFromClipboard(function (text) {
if (text.length > 200) {
text = text.substring(0, 200);
}
Expand All @@ -82,33 +80,78 @@ angular.module('copayApp.controllers').controller('tabSendController', function
return;
}
updateHasFunds();
updateContactsList(function() {
updateContactsList(function () {
updateList();
});
});

$scope.findContact = function(search) {
$scope.closePopover = function () {
$scope.popover.hide();
};

$scope.$on('$destroy', function () {
$scope.popover.remove();
});

$scope.confirmOpenapAddress = function (address, coin) {
$scope.popover.remove();
var params = sendFlowService.state.getClone();
params.data = address;
params.coin = coin;
sendFlowService.start(params, function onError() {
return
});
}

$scope.resolveOpencapAlias = function (alias) {
opencapService.getAddress(alias)
.then(function (result) {
if (typeof $scope.fromWallet !== 'undefined') {
if ($scope.fromWallet.coin === 'bch') {
result.addresses = { bch: result.addresses.bch };
}
if ($scope.fromWallet.coin === 'btc') {
result.addresses = { btc: result.addresses.btc };
}
}

$scope.opencapAddresses = result.addresses;
$scope.opencapDnssec = result.dnssec;
$ionicPopover.fromTemplateUrl('templates/popoverOpencapSend.html', { scope: $scope })
.then(function onThen(popover) {
$scope.popover = popover;
popover.show(angular.element(document.querySelector('#search-input')))
});
})
.catch(function (status) {
// do nothing because they may have been typing
});
}

$scope.findContact = function (search) {
if (!search || search.length < 1) {
$scope.list = originalList;
$timeout(function() {
$timeout(function () {
$scope.$apply();
});
return;
}

$scope.resolveOpencapAlias(search);

var params = sendFlowService.state.getClone();
params.data = search;
sendFlowService.start(params, function onError() {
var result = lodash.filter(originalList, function(item) {
var result = lodash.filter(originalList, function (item) {
var val = item.name;
return lodash.startsWith(val.toLowerCase(), search.toLowerCase());
});

$scope.list = result;
});
};

var hasWallets = function() {
var hasWallets = function () {
$scope.walletsWithFunds = profileService.getWallets({
onlyComplete: true,
hasFunds: true
Expand All @@ -127,11 +170,11 @@ angular.module('copayApp.controllers').controller('tabSendController', function
$scope.hasWallets = lodash.isEmpty($scope.wallets) ? false : true;
};

var updateHasFunds = function() {
var updateHasFunds = function () {
$scope.hasFunds = false;
var index = 0;
lodash.each($scope.wallets, function(w) {
walletService.getStatus(w, {}, function(err, status) {
lodash.each($scope.wallets, function (w) {
walletService.getStatus(w, {}, function (err, status) {

++index;
if (err && !status) {
Expand All @@ -146,54 +189,54 @@ angular.module('copayApp.controllers').controller('tabSendController', function

if (index === $scope.wallets.length) {
$scope.checkingBalance = false;
$timeout(function() {
$timeout(function () {
$scope.$apply();
});
}
});
});
};

var updateContactsList = function(cb) {
var updateContactsList = function (cb) {
var config = configService.getSync();
var defaults = configService.getDefaults();
addressbookService.list(function(err, ab) {
addressbookService.list(function (err, ab) {
if (err) $log.error(err);

$scope.hasContacts = lodash.isEmpty(ab) ? false : true;
if (!$scope.hasContacts) return cb();

var completeContacts = [];
lodash.each(ab, function(v, k) {
lodash.each(ab, function (v, k) {
completeContacts.push({
name: lodash.isObject(v) ? v.name : v,
address: k,
email: lodash.isObject(v) ? v.email : null,
recipientType: 'contact',
coin: v.coin,
displayCoin: (v.coin == 'bch'
? (config.bitcoinCashAlias || defaults.bitcoinCashAlias)
: (config.bitcoinAlias || defaults.bitcoinAlias)).toUpperCase()
displayCoin: (v.coin == 'bch'
? (config.bitcoinCashAlias || defaults.bitcoinCashAlias)
: (config.bitcoinAlias || defaults.bitcoinAlias)).toUpperCase()
});
});
originalList = completeContacts;
return cb();
});
};

var updateList = function() {
var updateList = function () {
$scope.list = lodash.clone(originalList);
$timeout(function() {
$timeout(function () {
$ionicScrollDelegate.resize();
$scope.$apply();
}, 10);
};

$scope.searchInFocus = function() {
$scope.searchInFocus = function () {
$scope.searchFocus = true;
};

$scope.searchBlurred = function() {
$scope.searchBlurred = function () {
if ($scope.formData.search == null || $scope.formData.search.length === 0) {
$scope.searchFocus = false;
}
Expand All @@ -210,38 +253,34 @@ angular.module('copayApp.controllers').controller('tabSendController', function
}

$log.debug('Got toAddress:' + toAddress + ' | ' + item.name);

var stateParams = sendFlowService.state.getClone();
stateParams.toAddress = toAddress;
stateParams.coin = item.coin;
sendFlowService.start(stateParams);
});
};

$scope.startWalletToWalletTransfer = function() {
$scope.startWalletToWalletTransfer = function () {
console.log('startWalletToWalletTransfer()');
var params = sendFlowService.state.getClone();
params.isWalletTransfer = true;
sendFlowService.start(params);
}

// This could probably be enhanced refactoring the routes abstract states
$scope.createWallet = function() {
$state.go('tabs.home').then(function() {
$scope.createWallet = function () {
$state.go('tabs.home').then(function () {
$state.go('tabs.add.create-personal');
});
};

$scope.buyBitcoin = function() {
if (isBuyBitcoinAllowed) {
moonPayService.start();
} else {
var os = platformInfo.isAndroid ? 'android' : platformInfo.isIOS ? 'ios' : 'desktop';
externalLinkService.open('https://purchase.bitcoin.com/?utm_source=WalletApp&utm_medium=' + os);
}
$scope.buyBitcoin = function () {
var os = platformInfo.isAndroid ? 'android' : platformInfo.isIOS ? 'ios' : 'desktop';
externalLinkService.open('https://purchase.bitcoin.com/?utm_source=WalletApp&utm_medium=' + os);
};

$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.$on("$ionicView.beforeEnter", function (event, data) {
console.log(data);
console.log('tab-send onBeforeEnter sendflow ', sendFlowService.state);
$scope.isIOS = platformInfo.isIOS && platformInfo.isCordova;
Expand All @@ -254,7 +293,7 @@ angular.module('copayApp.controllers').controller('tabSendController', function
originalList = [];
hasWallets();

configService.whenAvailable(function(_config) {
configService.whenAvailable(function (_config) {
$scope.displayBalanceAsFiat = _config.wallet.settings.priceDisplay === 'fiat';
});

Expand All @@ -263,4 +302,4 @@ angular.module('copayApp.controllers').controller('tabSendController', function
}

});
});
});
Loading