2016-08-15 10:25:43 -03:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2016-11-09 12:34:38 -05:00
|
|
|
|
angular.module('copayApp.controllers').controller('walletDetailsController', function($scope, $rootScope, $interval, $timeout, $filter, $log, $ionicModal, $ionicPopover, $state, $stateParams, profileService, lodash, configService, gettextCatalog, platformInfo, walletService, txpModalService, externalLinkService, popupService, addressbookService, storageService, $ionicHistory, $ionicScrollDelegate, $window) {
|
2016-09-08 12:13:37 -03:00
|
|
|
|
|
2016-08-18 10:37:08 -03:00
|
|
|
|
var HISTORY_SHOW_LIMIT = 10;
|
2016-09-29 19:04:52 -03:00
|
|
|
|
var currentTxHistoryPage = 0;
|
|
|
|
|
|
var listeners = [];
|
2016-08-23 17:31:50 -03:00
|
|
|
|
$scope.txps = [];
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.completeTxHistory = [];
|
|
|
|
|
|
$scope.openTxpModal = txpModalService.open;
|
2016-10-10 15:19:33 -03:00
|
|
|
|
$scope.isCordova = platformInfo.isCordova;
|
2016-10-14 10:24:20 -03:00
|
|
|
|
$scope.isAndroid = platformInfo.isAndroid;
|
2016-08-23 17:31:50 -03:00
|
|
|
|
|
2016-09-05 14:59:11 -03:00
|
|
|
|
$scope.openExternalLink = function(url, target) {
|
|
|
|
|
|
externalLinkService.open(url, target);
|
|
|
|
|
|
};
|
2016-08-23 17:31:50 -03:00
|
|
|
|
|
|
|
|
|
|
var setPendingTxps = function(txps) {
|
2016-09-20 10:02:28 -03:00
|
|
|
|
|
|
|
|
|
|
/* Uncomment to test multiple outputs */
|
|
|
|
|
|
|
|
|
|
|
|
// var txp = {
|
|
|
|
|
|
// message: 'test multi-output',
|
|
|
|
|
|
// fee: 1000,
|
|
|
|
|
|
// createdOn: new Date() / 1000,
|
|
|
|
|
|
// outputs: [],
|
2016-09-29 19:04:52 -03:00
|
|
|
|
// wallet: $scope.wallet
|
2016-09-20 10:02:28 -03:00
|
|
|
|
// };
|
|
|
|
|
|
//
|
|
|
|
|
|
// function addOutput(n) {
|
|
|
|
|
|
// txp.outputs.push({
|
|
|
|
|
|
// amount: 600,
|
|
|
|
|
|
// toAddress: '2N8bhEwbKtMvR2jqMRcTCQqzHP6zXGToXcK',
|
|
|
|
|
|
// message: 'output #' + (Number(n) + 1)
|
|
|
|
|
|
// });
|
|
|
|
|
|
// };
|
|
|
|
|
|
// lodash.times(15, addOutput);
|
|
|
|
|
|
// txps.push(txp);
|
|
|
|
|
|
|
2016-08-23 17:31:50 -03:00
|
|
|
|
if (!txps) {
|
|
|
|
|
|
$scope.txps = [];
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
$scope.txps = lodash.sortBy(txps, 'createdOn').reverse();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
|
var updateStatus = function(force) {
|
2016-08-23 17:31:50 -03:00
|
|
|
|
$scope.updatingStatus = true;
|
|
|
|
|
|
$scope.updateStatusError = false;
|
2016-09-08 12:13:37 -03:00
|
|
|
|
$scope.walletNotRegistered = false;
|
2016-08-23 17:31:50 -03:00
|
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
|
walletService.getStatus($scope.wallet, {
|
2016-08-23 17:31:50 -03:00
|
|
|
|
force: !!force,
|
|
|
|
|
|
}, function(err, status) {
|
|
|
|
|
|
$scope.updatingStatus = false;
|
2016-11-04 14:56:20 -04:00
|
|
|
|
console.log('status', status);
|
2016-08-23 17:31:50 -03:00
|
|
|
|
if (err) {
|
2016-09-08 12:13:37 -03:00
|
|
|
|
if (err === 'WALLET_NOT_REGISTERED') {
|
|
|
|
|
|
$scope.walletNotRegistered = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$scope.updateStatusError = true;
|
|
|
|
|
|
}
|
2016-08-23 17:31:50 -03:00
|
|
|
|
$scope.status = null;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setPendingTxps(status.pendingTxps);
|
|
|
|
|
|
|
|
|
|
|
|
$scope.status = status;
|
2016-08-24 17:54:01 -03:00
|
|
|
|
$timeout(function() {
|
2016-08-23 17:45:23 -03:00
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
}, 1);
|
2016-08-24 17:54:01 -03:00
|
|
|
|
|
2016-08-23 17:31:50 -03:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-15 10:25:43 -03:00
|
|
|
|
$scope.openSearchModal = function() {
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.color = $scope.wallet.color;
|
2016-08-15 10:25:43 -03:00
|
|
|
|
|
|
|
|
|
|
$ionicModal.fromTemplateUrl('views/modals/search.html', {
|
|
|
|
|
|
scope: $scope,
|
|
|
|
|
|
focusFirstInput: true
|
|
|
|
|
|
}).then(function(modal) {
|
|
|
|
|
|
$scope.searchModal = modal;
|
|
|
|
|
|
$scope.searchModal.show();
|
|
|
|
|
|
});
|
2016-09-02 14:29:34 -03:00
|
|
|
|
|
|
|
|
|
|
$scope.close = function() {
|
|
|
|
|
|
$scope.searchModal.hide();
|
2016-10-21 19:13:14 -04:00
|
|
|
|
};
|
2016-08-15 10:25:43 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-18 17:56:04 -03:00
|
|
|
|
$scope.openTxModal = function(btx) {
|
2016-08-15 10:25:43 -03:00
|
|
|
|
$scope.btx = lodash.cloneDeep(btx);
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.walletId = $scope.wallet.id;
|
2016-10-22 17:43:05 -03:00
|
|
|
|
$state.transitionTo('tabs.wallet.tx-details', {
|
|
|
|
|
|
txid: $scope.btx.txid,
|
|
|
|
|
|
walletId: $scope.walletId
|
|
|
|
|
|
});
|
2016-08-15 10:25:43 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-18 10:37:08 -03:00
|
|
|
|
$scope.recreate = function() {
|
2016-09-29 19:04:52 -03:00
|
|
|
|
walletService.recreate($scope.wallet, function(err) {
|
2016-09-08 12:13:37 -03:00
|
|
|
|
if (err) return;
|
|
|
|
|
|
$timeout(function() {
|
2016-09-29 19:04:52 -03:00
|
|
|
|
walletService.startScan($scope.wallet, function() {
|
2016-09-08 12:13:37 -03:00
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2016-08-18 10:37:08 -03:00
|
|
|
|
};
|
2016-08-17 18:48:30 -03:00
|
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
|
var updateTxHistory = function(cb) {
|
2016-09-07 15:35:14 -03:00
|
|
|
|
if (!cb) cb = function() {};
|
2016-08-18 10:37:08 -03:00
|
|
|
|
if ($scope.updatingTxHistory) return;
|
|
|
|
|
|
|
|
|
|
|
|
$scope.updatingTxHistory = true;
|
|
|
|
|
|
$scope.updateTxHistoryError = false;
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.updatingTxHistoryProgress = 0;
|
2016-08-18 10:37:08 -03:00
|
|
|
|
|
2016-09-30 15:58:56 -03:00
|
|
|
|
var progressFn = function(txs, newTxs) {
|
|
|
|
|
|
$scope.updatingTxHistoryProgress = newTxs;
|
2016-08-22 18:20:01 -03:00
|
|
|
|
$scope.completeTxHistory = txs;
|
2016-08-18 10:37:08 -03:00
|
|
|
|
$scope.showHistory();
|
2016-09-30 15:58:56 -03:00
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
});
|
2016-08-18 10:37:08 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
2016-09-29 19:04:52 -03:00
|
|
|
|
walletService.getTxHistory($scope.wallet, {
|
2016-08-18 10:37:08 -03:00
|
|
|
|
progressFn: progressFn,
|
|
|
|
|
|
}, function(err, txHistory) {
|
|
|
|
|
|
$scope.updatingTxHistory = false;
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
$scope.txHistory = null;
|
|
|
|
|
|
$scope.updateTxHistoryError = true;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-08-22 18:20:01 -03:00
|
|
|
|
$scope.completeTxHistory = txHistory;
|
2016-08-18 10:37:08 -03:00
|
|
|
|
$scope.showHistory();
|
2016-09-30 12:27:31 -03:00
|
|
|
|
$scope.$apply();
|
2016-08-24 17:54:01 -03:00
|
|
|
|
return cb();
|
2016-08-18 10:37:08 -03:00
|
|
|
|
});
|
2016-08-15 16:07:30 -03:00
|
|
|
|
});
|
2016-08-15 10:25:43 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-18 10:37:08 -03:00
|
|
|
|
$scope.showHistory = function() {
|
2016-08-22 18:20:01 -03:00
|
|
|
|
if ($scope.completeTxHistory) {
|
|
|
|
|
|
$scope.txHistory = $scope.completeTxHistory.slice(0, (currentTxHistoryPage + 1) * HISTORY_SHOW_LIMIT);
|
|
|
|
|
|
$scope.txHistoryShowMore = $scope.completeTxHistory.length > $scope.txHistory.length;
|
2016-08-18 10:37:08 -03:00
|
|
|
|
}
|
2016-11-01 17:47:07 -04:00
|
|
|
|
console.log('$scope.completeTxHistory', $scope.completeTxHistory);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$scope.getDate = function(txCreated) {
|
2016-11-02 17:01:32 -04:00
|
|
|
|
var date = new Date(txCreated * 1000);
|
|
|
|
|
|
return date;
|
2016-08-18 10:37:08 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-11-04 13:29:19 -04:00
|
|
|
|
$scope.isFirstInGroup = function(index) {
|
2016-11-02 16:35:05 -04:00
|
|
|
|
if(index === 0) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
var curTx = $scope.txHistory[index];
|
|
|
|
|
|
var prevTx = $scope.txHistory[index - 1];
|
|
|
|
|
|
return !createdDuringSameMonth(curTx, prevTx);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-11-04 13:29:19 -04:00
|
|
|
|
$scope.isLastInGroup = function(index) {
|
|
|
|
|
|
if(index === $scope.txHistory.length - 1) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $scope.isFirstInGroup(index + 1);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-11-02 16:35:05 -04:00
|
|
|
|
function createdDuringSameMonth(tx1, tx2) {
|
|
|
|
|
|
var date1 = new Date(tx1.time * 1000);
|
|
|
|
|
|
var date2 = new Date(tx2.time * 1000);
|
2016-11-02 17:01:32 -04:00
|
|
|
|
return getMonthYear(date1) === getMonthYear(date2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 17:29:29 -04:00
|
|
|
|
$scope.createdWithinPastDay = function(time) {
|
2016-11-03 15:50:22 -04:00
|
|
|
|
var now = new Date();
|
2016-11-03 17:29:29 -04:00
|
|
|
|
var date = new Date(time * 1000);
|
2016-11-03 15:50:22 -04:00
|
|
|
|
return (now.getTime() - date.getTime()) < (1000 * 60 * 60 * 24);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-11-02 17:01:32 -04:00
|
|
|
|
$scope.isDateInCurrentMonth = function(date) {
|
|
|
|
|
|
var now = new Date();
|
|
|
|
|
|
return getMonthYear(now) === getMonthYear(date);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getMonthYear(date) {
|
|
|
|
|
|
return date.getMonth() + date.getFullYear();
|
2016-11-02 16:35:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 17:29:29 -04:00
|
|
|
|
$scope.isUnconfirmed = function(tx) {
|
2016-11-07 15:32:28 -05:00
|
|
|
|
return !tx.confirmations || tx.confirmations === 0;
|
2016-11-03 17:29:29 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-18 10:37:08 -03:00
|
|
|
|
$scope.showMore = function() {
|
2016-10-03 11:52:48 -03:00
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
currentTxHistoryPage++;
|
|
|
|
|
|
$scope.showHistory();
|
|
|
|
|
|
$scope.$broadcast('scroll.infiniteScrollComplete');
|
|
|
|
|
|
}, 100);
|
2016-08-18 10:37:08 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-10 15:19:33 -03:00
|
|
|
|
$scope.onRefresh = function() {
|
2016-10-14 10:25:52 -03:00
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$broadcast('scroll.refreshComplete');
|
|
|
|
|
|
}, 300);
|
2016-10-10 15:19:33 -03:00
|
|
|
|
$scope.updateAll(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.updateAll = function(force, cb) {
|
|
|
|
|
|
updateStatus(force);
|
|
|
|
|
|
updateTxHistory(cb);
|
2016-09-22 10:34:00 -03:00
|
|
|
|
};
|
2016-08-18 10:37:08 -03:00
|
|
|
|
|
2016-08-15 10:25:43 -03:00
|
|
|
|
$scope.hideToggle = function() {
|
2016-09-29 19:04:52 -03:00
|
|
|
|
profileService.toggleHideBalanceFlag($scope.wallet.credentials.walletId, function(err) {
|
2016-08-31 12:03:44 -03:00
|
|
|
|
if (err) $log.error(err);
|
|
|
|
|
|
});
|
2016-09-22 10:34:00 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-11-07 17:22:51 -05:00
|
|
|
|
$scope.backup = function() {
|
|
|
|
|
|
//$state.go('tabs.preferences', {walletId: $scope.walletId});
|
|
|
|
|
|
//$state.transitionTo('tabs.preferences.backupWarning');
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-11-09 12:34:38 -05:00
|
|
|
|
$scope.amountHeight = '180px';
|
|
|
|
|
|
|
|
|
|
|
|
$scope.getScrollPosition = function(){
|
|
|
|
|
|
console.log($ionicScrollDelegate.getScrollPosition().top);
|
|
|
|
|
|
var pos = $ionicScrollDelegate.getScrollPosition().top;
|
|
|
|
|
|
var amountHeight = 180 - pos;
|
|
|
|
|
|
if(amountHeight < 80) {
|
|
|
|
|
|
amountHeight = 80;
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('amountHeight', amountHeight);
|
|
|
|
|
|
$window.requestAnimationFrame(function() {
|
|
|
|
|
|
$scope.amountHeight = amountHeight + 'px';
|
|
|
|
|
|
$scope.$evalAsync(angular.noop);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-30 11:16:33 -03:00
|
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
2016-09-22 10:34:00 -03:00
|
|
|
|
|
2016-11-07 17:22:51 -05:00
|
|
|
|
$scope.walletId = data.stateParams.walletId;
|
|
|
|
|
|
storageService.getBackupFlag($scope.walletId, function(err, flag) {
|
|
|
|
|
|
$scope.isBackedUp = flag ? true : false;
|
|
|
|
|
|
});
|
|
|
|
|
|
$scope.wallet = profileService.getWallet($scope.walletId);
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.requiresMultipleSignatures = $scope.wallet.credentials.m > 1;
|
|
|
|
|
|
|
2016-10-21 11:58:33 -03:00
|
|
|
|
addressbookService.list(function(err, ab) {
|
|
|
|
|
|
if (err) $log.error(err);
|
|
|
|
|
|
$scope.addressbook = ab || {};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.updateAll();
|
|
|
|
|
|
|
|
|
|
|
|
listeners = [
|
|
|
|
|
|
$rootScope.$on('bwsEvent', function(e, walletId) {
|
|
|
|
|
|
if (walletId == $scope.wallet.id)
|
|
|
|
|
|
updateStatus();
|
|
|
|
|
|
}),
|
|
|
|
|
|
$rootScope.$on('Local/TxAction', function(e, walletId) {
|
|
|
|
|
|
if (walletId == $scope.wallet.id)
|
|
|
|
|
|
updateStatus();
|
|
|
|
|
|
}),
|
|
|
|
|
|
];
|
|
|
|
|
|
});
|
2016-09-22 10:34:00 -03:00
|
|
|
|
|
2016-09-29 19:04:52 -03:00
|
|
|
|
$scope.$on("$ionicView.leave", function(event, data) {
|
|
|
|
|
|
lodash.each(listeners, function(x) {
|
|
|
|
|
|
x();
|
2016-09-22 10:34:00 -03:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2016-08-15 10:25:43 -03:00
|
|
|
|
});
|