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

187 lines
4.8 KiB
JavaScript
Raw Normal View History

2016-08-12 12:44:16 -03:00
'use strict';
2017-01-16 16:00:09 -03:00
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, bwcError, gettextCatalog) {
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;
2016-09-21 17:12:25 -03:00
var updateList = function() {
CONTACTS_SHOW_LIMIT = 10;
currentContactsPage = 0;
2016-08-18 19:25:30 -03:00
originalList = [];
2016-09-08 16:54:35 -03:00
var wallets = profileService.getWallets({
onlyComplete: true
});
$scope.hasWallets = lodash.isEmpty(wallets) ? false : true;
2016-09-27 12:26:58 -03:00
$scope.oneWallet = wallets.length == 1;
2016-08-15 18:11:36 -03:00
if (!$scope.oneWallet) {
lodash.each(wallets, function(v) {
originalList.push({
color: v.color,
name: v.name,
recipientType: 'wallet',
getAddress: function(cb) {
walletService.getAddress(v, false, cb);
},
});
2016-08-15 18:11:36 -03:00
});
}
2016-08-12 15:11:52 -03:00
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;
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',
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-08-12 15:11:52 -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);
$scope.list = lodash.clone(originalList);
2016-09-08 16:54:35 -03:00
$timeout(function() {
2016-09-27 12:26:58 -03:00
$ionicScrollDelegate.resize();
2016-09-08 16:54:35 -03:00
$scope.$apply();
}, 10);
2016-08-15 18:11:36 -03:00
});
2016-08-12 16:30:50 -03:00
};
2016-10-13 12:41:02 -03:00
$scope.openScanner = function() {
$state.go('tabs.scan');
};
2016-10-13 12:41:02 -03:00
2016-09-27 12:26:58 -03:00
$scope.showMore = function() {
currentContactsPage++;
updateList();
};
$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,
toColor: item.color
})
});
2016-08-12 12:44:16 -03:00
});
};
2016-08-18 19:25:30 -03:00
2017-01-16 16:58:42 -03:00
// THIS is ONLY to show the 'buy bitcoins' message
// does not has any other function.
2016-10-11 18:13:46 -03:00
var updateHasFunds = function() {
2016-10-15 10:03:45 -03:00
if ($rootScope.everHasFunds) {
$scope.hasFunds = true;
2016-10-15 09:41:35 -03:00
return;
2016-10-15 09:45:44 -03:00
}
2016-10-15 09:41:35 -03:00
2016-10-15 10:03:45 -03:00
$scope.hasFunds = false;
2016-10-11 18:13:46 -03:00
var wallets = profileService.getWallets({
onlyComplete: true,
});
if (!wallets || !wallets.length) {
2016-10-15 10:03:45 -03:00
return $timeout(function() {
2016-10-12 15:55:33 -03:00
$scope.$apply();
});
2016-10-11 18:13:46 -03:00
}
2016-12-12 15:22:44 -03:00
$scope.checkingBalance = true;
2017-01-16 16:58:42 -03:00
var index = 0;
2016-10-11 18:13:46 -03:00
lodash.each(wallets, function(w) {
walletService.getStatus(w, {}, function(err, status) {
2017-01-16 16:00:09 -03:00
2016-10-11 18:13:46 -03:00
++index;
2016-12-21 11:27:44 -03:00
if (err && !status) {
2016-10-11 18:13:46 -03:00
$log.error(err);
2017-01-16 16:58:42 -03:00
// error updating the wallet. Probably a network error, do not show
// the 'buy bitcoins' message.
2017-01-16 16:58:42 -03:00
$scope.hasFunds = true;
2016-12-21 11:27:44 -03:00
} else if (status.availableBalanceSat > 0) {
2016-10-15 10:03:45 -03:00
$scope.hasFunds = true;
$rootScope.everHasFunds = true;
2016-10-15 09:41:35 -03:00
}
2016-10-15 10:03:45 -03:00
if (index == wallets.length) {
if ($scope.hasFunds != true) {
$ionicScrollDelegate.freezeScroll(true);
}
2016-12-21 11:27:44 -03:00
$scope.checkingBalance = false;
2016-10-12 15:55:33 -03:00
$timeout(function() {
$scope.$apply();
});
2016-10-11 18:13:46 -03:00
}
});
});
};
2016-09-26 18:03:00 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.formData = {
search: null
};
2016-09-21 17:12:25 -03:00
updateList();
2016-10-11 18:13:46 -03:00
updateHasFunds();
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.glidera');
});
};
2016-08-12 12:44:16 -03:00
});