2016-08-12 12:44:16 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-11-02 15:45:15 +09:00
|
|
|
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, bwcError, gettextCatalog, scannerService, configService) {
|
2016-08-12 15:11:52 -03:00
|
|
|
|
2016-08-18 19:25:30 -03:00
|
|
|
var originalList;
|
2016-09-30 13:19:29 -03:00
|
|
|
var CONTACTS_SHOW_LIMIT;
|
|
|
|
|
var currentContactsPage;
|
2017-01-05 16:58:58 -03:00
|
|
|
$scope.isChromeApp = platformInfo.isChromeApp;
|
2016-10-11 16:36:01 -04:00
|
|
|
|
2016-08-12 16:55:14 -03:00
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
var hasWallets = function() {
|
|
|
|
|
$scope.wallets = profileService.getWallets({
|
2016-09-08 16:54:35 -03:00
|
|
|
onlyComplete: true
|
|
|
|
|
});
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.hasWallets = lodash.isEmpty($scope.wallets) ? false : true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// THIS is ONLY to show the 'buy bitcoins' message
|
|
|
|
|
// does not has any other function.
|
|
|
|
|
|
|
|
|
|
var updateHasFunds = function() {
|
|
|
|
|
|
|
|
|
|
if ($rootScope.everHasFunds) {
|
|
|
|
|
$scope.hasFunds = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.hasFunds = false;
|
|
|
|
|
var index = 0;
|
|
|
|
|
lodash.each($scope.wallets, function(w) {
|
|
|
|
|
walletService.getStatus(w, {}, function(err, status) {
|
|
|
|
|
|
|
|
|
|
++index;
|
|
|
|
|
if (err && !status) {
|
|
|
|
|
$log.error(err);
|
|
|
|
|
// error updating the wallet. Probably a network error, do not show
|
|
|
|
|
// the 'buy bitcoins' message.
|
|
|
|
|
|
|
|
|
|
$scope.hasFunds = true;
|
|
|
|
|
} else if (status.availableBalanceSat > 0) {
|
|
|
|
|
$scope.hasFunds = true;
|
|
|
|
|
$rootScope.everHasFunds = true;
|
|
|
|
|
}
|
2016-08-15 18:11:36 -03:00
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
if (index == $scope.wallets.length) {
|
|
|
|
|
$scope.checkingBalance = false;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var updateWalletsList = function() {
|
|
|
|
|
|
|
|
|
|
var networkResult = lodash.countBy($scope.wallets, 'network');
|
|
|
|
|
|
|
|
|
|
$scope.showTransferCard = $scope.hasWallets && (networkResult.livenet > 1 || networkResult.testnet > 1);
|
|
|
|
|
|
|
|
|
|
if ($scope.showTransferCard) {
|
|
|
|
|
var walletsToTransfer = $scope.wallets;
|
|
|
|
|
if (!(networkResult.livenet > 1)) {
|
|
|
|
|
walletsToTransfer = lodash.filter(walletsToTransfer, function(item) {
|
|
|
|
|
return item.network == 'testnet';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!(networkResult.testnet > 1)) {
|
|
|
|
|
walletsToTransfer = lodash.filter(walletsToTransfer, function(item) {
|
|
|
|
|
return item.network == 'livenet';
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-03-02 16:56:44 -03:00
|
|
|
var walletList = [];
|
2017-02-21 11:33:31 -05:00
|
|
|
lodash.each(walletsToTransfer, function(v) {
|
2017-03-02 16:56:44 -03:00
|
|
|
walletList.push({
|
2016-09-30 13:19:29 -03:00
|
|
|
color: v.color,
|
|
|
|
|
name: v.name,
|
2017-02-20 13:06:42 -05:00
|
|
|
recipientType: 'wallet',
|
2017-08-30 16:51:24 -03:00
|
|
|
coin: v.coin,
|
2017-09-20 11:53:30 -03:00
|
|
|
network: v.network,
|
2017-11-02 12:50:26 +09:00
|
|
|
balanceString: v.cachedBalance,
|
2016-09-30 13:19:29 -03:00
|
|
|
getAddress: function(cb) {
|
|
|
|
|
walletService.getAddress(v, false, cb);
|
|
|
|
|
},
|
|
|
|
|
});
|
2016-08-15 18:11:36 -03:00
|
|
|
});
|
2017-03-02 16:56:44 -03:00
|
|
|
originalList = originalList.concat(walletList);
|
2016-09-30 13:19:29 -03:00
|
|
|
}
|
2017-02-21 11:33:31 -05:00
|
|
|
}
|
2017-11-02 15:45:15 +09:00
|
|
|
|
2017-03-02 16:56:44 -03:00
|
|
|
var updateContactsList = function(cb) {
|
2017-11-02 14:55:24 +09:00
|
|
|
var config = configService.getSync();
|
2017-11-02 12:55:55 +09:00
|
|
|
var defaults = configService.getDefaults();
|
2016-08-15 18:11:36 -03:00
|
|
|
addressbookService.list(function(err, ab) {
|
|
|
|
|
if (err) $log.error(err);
|
2016-08-12 15:11:52 -03:00
|
|
|
|
2016-09-23 17:10:34 -03:00
|
|
|
$scope.hasContacts = lodash.isEmpty(ab) ? false : true;
|
2017-03-02 16:56:44 -03:00
|
|
|
if (!$scope.hasContacts) return cb();
|
2017-02-21 11:33:31 -05:00
|
|
|
|
2016-09-27 12:26:58 -03:00
|
|
|
var completeContacts = [];
|
2016-08-15 18:11:36 -03:00
|
|
|
lodash.each(ab, function(v, k) {
|
2016-09-27 12:26:58 -03:00
|
|
|
completeContacts.push({
|
2016-09-12 18:04:56 -03:00
|
|
|
name: lodash.isObject(v) ? v.name : v,
|
2016-08-18 19:25:30 -03:00
|
|
|
address: k,
|
2016-09-14 10:04:16 -03:00
|
|
|
email: lodash.isObject(v) ? v.email : null,
|
2017-02-20 13:06:42 -05:00
|
|
|
recipientType: 'contact',
|
2017-11-02 15:40:21 +09:00
|
|
|
coin: v.coin,
|
|
|
|
|
displayCoin: (v.coin == 'bch'
|
2017-11-02 14:55:24 +09:00
|
|
|
? (config.bitcoinCashAlias || defaults.bitcoinCashAlias)
|
2017-11-02 15:03:43 +09:00
|
|
|
: (config.bitcoinAlias || defaults.bitcoinAlias)).toUpperCase(),
|
2016-08-16 18:38:18 -03:00
|
|
|
getAddress: function(cb) {
|
2016-09-08 16:54:35 -03:00
|
|
|
return cb(null, k);
|
2016-08-16 18:38:18 -03:00
|
|
|
},
|
2016-08-15 18:11:36 -03:00
|
|
|
});
|
|
|
|
|
});
|
2016-09-27 12:26:58 -03:00
|
|
|
var contacts = completeContacts.slice(0, (currentContactsPage + 1) * CONTACTS_SHOW_LIMIT);
|
|
|
|
|
$scope.contactsShowMore = completeContacts.length > contacts.length;
|
2016-08-15 18:11:36 -03:00
|
|
|
originalList = originalList.concat(contacts);
|
2017-03-02 16:56:44 -03:00
|
|
|
return cb();
|
2016-08-15 18:11:36 -03:00
|
|
|
});
|
2016-08-12 16:30:50 -03:00
|
|
|
};
|
|
|
|
|
|
2017-03-02 16:56:44 -03:00
|
|
|
var updateList = function() {
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.list = lodash.clone(originalList);
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 10);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 12:41:02 -03:00
|
|
|
$scope.openScanner = function() {
|
2017-06-08 15:09:39 -03:00
|
|
|
var isWindowsPhoneApp = platformInfo.isCordova && platformInfo.isWP;
|
2017-06-07 14:45:40 -03:00
|
|
|
|
|
|
|
|
if (!isWindowsPhoneApp) {
|
|
|
|
|
$state.go('tabs.scan');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scannerService.useOldScanner(function(err, contents) {
|
|
|
|
|
if (err) {
|
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
incomingData.redir(contents);
|
|
|
|
|
});
|
2016-10-26 14:00:43 -04:00
|
|
|
};
|
2016-10-13 12:41:02 -03:00
|
|
|
|
2016-09-27 12:26:58 -03:00
|
|
|
$scope.showMore = function() {
|
|
|
|
|
currentContactsPage++;
|
2017-02-21 11:33:31 -05:00
|
|
|
updateWalletsList();
|
2016-09-27 12:26:58 -03:00
|
|
|
};
|
|
|
|
|
|
2017-06-08 17:43:06 -04:00
|
|
|
$scope.searchInFocus = function() {
|
|
|
|
|
$scope.searchFocus = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.searchBlurred = function() {
|
2017-06-16 12:45:11 -04:00
|
|
|
if ($scope.formData.search == null || $scope.formData.search.length == 0) {
|
2017-06-08 17:43:06 -04:00
|
|
|
$scope.searchFocus = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-25 17:40:20 -03:00
|
|
|
$scope.findContact = function(search) {
|
2016-08-15 18:11:36 -03:00
|
|
|
|
2016-08-25 11:18:10 -03:00
|
|
|
if (incomingData.redir(search)) {
|
|
|
|
|
return;
|
2016-08-24 19:31:07 -03:00
|
|
|
}
|
2016-08-24 18:06:17 -03:00
|
|
|
|
2016-08-18 19:25:30 -03:00
|
|
|
if (!search || search.length < 2) {
|
2016-08-15 18:11:36 -03:00
|
|
|
$scope.list = originalList;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
2016-10-07 11:27:53 -03:00
|
|
|
});
|
2016-08-15 18:11:36 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 17:40:20 -03:00
|
|
|
var result = lodash.filter(originalList, function(item) {
|
2016-09-12 18:04:56 -03:00
|
|
|
var val = item.name;
|
2016-08-18 19:25:30 -03:00
|
|
|
return lodash.includes(val.toLowerCase(), search.toLowerCase());
|
2016-08-12 16:30:50 -03:00
|
|
|
});
|
2016-08-12 16:55:14 -03:00
|
|
|
|
2016-08-12 16:30:50 -03:00
|
|
|
$scope.list = result;
|
2016-08-12 15:11:52 -03:00
|
|
|
};
|
|
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
$scope.goToAmount = function(item) {
|
2016-10-07 11:27:53 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
item.getAddress(function(err, addr) {
|
|
|
|
|
if (err || !addr) {
|
2017-01-16 16:58:42 -03:00
|
|
|
//Error is already formated
|
|
|
|
|
return popupService.showAlert(err);
|
2016-10-07 11:27:53 -03:00
|
|
|
}
|
2018-01-31 15:35:52 -04:00
|
|
|
|
|
|
|
|
if (item.recipientType && item.recipientType == 'contact') {
|
|
|
|
|
if (addr.indexOf('bch') == 0 || addr.indexOf('btc') == 0) {
|
|
|
|
|
addr = addr.substring(3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 11:27:53 -03:00
|
|
|
$log.debug('Got toAddress:' + addr + ' | ' + item.name);
|
|
|
|
|
return $state.transitionTo('tabs.send.amount', {
|
2017-02-20 13:06:42 -05:00
|
|
|
recipientType: item.recipientType,
|
2016-10-07 11:27:53 -03:00
|
|
|
toAddress: addr,
|
|
|
|
|
toName: item.name,
|
2016-10-13 17:11:19 -03:00
|
|
|
toEmail: item.email,
|
2017-08-30 16:51:24 -03:00
|
|
|
toColor: item.color,
|
|
|
|
|
coin: item.coin
|
2018-01-31 15:35:52 -04:00
|
|
|
});
|
2016-10-07 11:27:53 -03:00
|
|
|
});
|
2016-08-12 12:44:16 -03:00
|
|
|
});
|
|
|
|
|
};
|
2016-08-18 19:25:30 -03:00
|
|
|
|
2016-10-11 18:30:40 -03:00
|
|
|
// This could probably be enhanced refactoring the routes abstract states
|
|
|
|
|
$scope.createWallet = function() {
|
|
|
|
|
$state.go('tabs.home').then(function() {
|
|
|
|
|
$state.go('tabs.add.create-personal');
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.buyBitcoin = function() {
|
|
|
|
|
$state.go('tabs.home').then(function() {
|
2017-02-21 11:33:31 -05:00
|
|
|
$state.go('tabs.buyandsell');
|
2016-10-11 18:30:40 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
|
|
|
|
$scope.checkingBalance = true;
|
|
|
|
|
$scope.formData = {
|
|
|
|
|
search: null
|
|
|
|
|
};
|
|
|
|
|
originalList = [];
|
|
|
|
|
CONTACTS_SHOW_LIMIT = 10;
|
|
|
|
|
currentContactsPage = 0;
|
|
|
|
|
hasWallets();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.enter", function(event, data) {
|
|
|
|
|
if (!$scope.hasWallets) {
|
|
|
|
|
$scope.checkingBalance = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
updateHasFunds();
|
|
|
|
|
updateWalletsList();
|
2017-03-02 16:56:44 -03:00
|
|
|
updateContactsList(function() {
|
|
|
|
|
updateList();
|
|
|
|
|
});
|
2017-02-21 11:33:31 -05:00
|
|
|
});
|
2016-08-12 12:44:16 -03:00
|
|
|
});
|