Wallet/src/js/controllers/walletDetails.js

302 lines
8.2 KiB
JavaScript
Raw Normal View History

2016-08-15 10:25:43 -03:00
'use strict';
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;
var currentTxHistoryPage = 0;
var listeners = [];
2016-08-23 17:31:50 -03:00
$scope.txps = [];
$scope.completeTxHistory = [];
$scope.openTxpModal = txpModalService.open;
2016-10-10 15:19:33 -03:00
$scope.isCordova = platformInfo.isCordova;
$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: [],
// 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();
};
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
walletService.getStatus($scope.wallet, {
2016-08-23 17:31:50 -03:00
force: !!force,
}, function(err, status) {
$scope.updatingStatus = false;
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() {
$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);
$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() {
walletService.recreate($scope.wallet, function(err) {
2016-09-08 12:13:37 -03:00
if (err) return;
$timeout(function() {
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
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;
$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() {
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();
$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
}
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) {
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);
};
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) {
var now = new Date();
2016-11-03 17:29:29 -04:00
var date = new Date(time * 1000);
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-03 17:29:29 -04:00
$scope.isUnconfirmed = function(tx) {
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() {
$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);
};
$scope.updateAll = function(force, cb)  {
updateStatus(force);
updateTxHistory(cb);
};
2016-08-18 10:37:08 -03:00
2016-08-15 10:25:43 -03:00
$scope.hideToggle = function() {
profileService.toggleHideBalanceFlag($scope.wallet.credentials.walletId, function(err) {
if (err) $log.error(err);
});
};
$scope.backup = function() {
//$state.go('tabs.preferences', {walletId: $scope.walletId});
//$state.transitionTo('tabs.preferences.backupWarning');
};
$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;
}
2016-11-09 13:37:14 -05:00
var contentMargin = amountHeight;
if(contentMargin > 180) {
contentMargin = 180;
}
2016-11-09 13:15:42 -05:00
var amountScale = amountHeight/180;
if(amountScale < 0.5) {
amountScale = 0.5;
}
if(amountScale > 1.1) {
amountScale = 1.1;
}
var s = amountScale;
$scope.altAmountOpacity = (amountHeight - 100)/80;
console.log('amountHeight', amountHeight);
$window.requestAnimationFrame(function() {
$scope.amountHeight = amountHeight + 'px';
2016-11-09 13:37:14 -05:00
$scope.contentMargin = contentMargin + 'px';
2016-11-09 13:15:42 -05:00
$scope.amountScale = 'scale3d(' + s + ',' + s + ',' + s+ ')';
console.log('$scope.amountScale', $scope.amountScale);
console.log('$scope.altAmountOpacity', $scope.altAmountOpacity);
$scope.$evalAsync(angular.noop);
});
};
2016-09-30 11:16:33 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.walletId = data.stateParams.walletId;
storageService.getBackupFlag($scope.walletId, function(err, flag) {
$scope.isBackedUp = flag ? true : false;
});
$scope.wallet = profileService.getWallet($scope.walletId);
$scope.requiresMultipleSignatures = $scope.wallet.credentials.m > 1;
addressbookService.list(function(err, ab) {
if (err) $log.error(err);
$scope.addressbook = ab || {};
});
$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();
}),
];
});
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
});
2016-08-15 10:25:43 -03:00
});