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

242 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-08-12 12:44:16 -03:00
'use strict';
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, bwcError, gettextCatalog, scannerService, bitcoreCash) {
2016-08-12 15:11:52 -03:00
2016-08-18 19:25:30 -03:00
var originalList;
var CONTACTS_SHOW_LIMIT;
var currentContactsPage;
$scope.isChromeApp = platformInfo.isChromeApp;
var hasWallets = function() {
$scope.wallets = profileService.getWallets({
2016-09-08 16:54:35 -03:00
onlyComplete: true
});
$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
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 = [];
lodash.each(walletsToTransfer, function(v) {
2017-03-02 16:56:44 -03:00
walletList.push({
color: v.color,
name: v.name,
recipientType: 'wallet',
2017-08-30 16:51:24 -03:00
coin: v.coin,
2017-09-20 11:53:30 -03:00
network: v.network,
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-08-12 15:11:52 -03:00
var getCoin = function(address) {
var cashAddress = bitcoreCash.Address.isValid(address, 'livenet');
if (cashAddress) {
return 'bch';
}
return 'btc';
};
2017-03-02 16:56:44 -03:00
var updateContactsList = function(cb) {
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
$scope.hasContacts = lodash.isEmpty(ab) ? false : true;
2017-03-02 16:56:44 -03:00
if (!$scope.hasContacts) return cb();
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,
recipientType: 'contact',
coin: getCoin(k),
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() {
$scope.list = lodash.clone(originalList);
$timeout(function() {
$ionicScrollDelegate.resize();
$scope.$apply();
}, 10);
};
2016-10-13 12:41:02 -03:00
$scope.openScanner = function() {
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-13 12:41:02 -03:00
2016-09-27 12:26:58 -03:00
$scope.showMore = function() {
currentContactsPage++;
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() {
if ($scope.formData.search == null || $scope.formData.search.length == 0) {
2017-06-08 17:43:06 -04:00
$scope.searchFocus = false;
}
};
$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-08-15 18:11:36 -03:00
return;
}
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: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) {
$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);
}
$log.debug('Got toAddress:' + addr + ' | ' + item.name);
return $state.transitionTo('tabs.send.amount', {
recipientType: item.recipientType,
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
})
});
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() {
$state.go('tabs.buyandsell');
2016-10-11 18:30:40 -03: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();
});
});
2016-08-12 12:44:16 -03:00
});