Wallet/src/js/controllers/index.js
Gustavo Maximiliano Cortez 49cd19bd1b Better look and feel
2015-09-05 14:32:29 -03:00

1094 lines
30 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, utilService, $state, glideraService) {
var self = this;
self.isCordova = isCordova;
self.onGoingProcess = {};
self.limitHistory = 5;
function strip(number) {
return (parseFloat(number.toPrecision(12)));
};
self.goHome = function() {
go.walletHome();
};
self.menu = [{
'title': gettext('Home'),
'icon': 'icon-home',
'link': 'walletHome'
}, {
'title': gettext('Receive'),
'icon': 'icon-receive2',
'link': 'receive'
}, {
'title': gettext('Send'),
'icon': 'icon-paperplane',
'link': 'send'
}, {
'title': gettext('History'),
'icon': 'icon-history',
'link': 'history'
}];
self.addonViews = addonManager.addonViews();
self.menu = self.menu.concat(addonManager.addonMenuItems());
self.menuItemSize = self.menu.length > 5 ? 2 : 3;
self.txTemplateUrl = addonManager.txTemplateUrl() || 'views/includes/transaction.html';
self.tab = 'walletHome';
self.availableLanguages = [{
name: 'English',
isoCode: 'en',
}, {
name: 'Français',
isoCode: 'fr',
}, {
name: 'Italiano',
isoCode: 'it',
}, {
name: 'Deutsch',
isoCode: 'de',
}, {
name: 'Español',
isoCode: 'es',
}, {
name: 'Português',
isoCode: 'pt',
}, {
name: 'Ελληνικά',
isoCode: 'el',
}, {
name: '日本語',
isoCode: 'ja',
}, {
name: 'Pусский',
isoCode: 'ru',
}];
self.feeOpts = feeService.feeOpts;
self.setOngoingProcess = function(processName, isOn) {
$log.debug('onGoingProcess', processName, isOn);
self[processName] = isOn;
self.onGoingProcess[processName] = isOn;
var name;
self.anyOnGoingProcess = lodash.any(self.onGoingProcess, function(isOn, processName) {
if (isOn)
name = name || processName;
return isOn;
});
// The first one
self.onGoingProcessName = name;
$timeout(function() {
$rootScope.$apply();
});
};
self.setFocusedWallet = function() {
var fc = profileService.focusedClient;
if (!fc) return;
// Clean status
self.totalBalanceSat = null;
self.lockedBalanceSat = null;
self.availableBalanceSat = null;
self.pendingAmount = null;
self.spendUnconfirmed = null;
self.totalBalanceStr = null;
self.availableBalanceStr = null;
self.lockedBalanceStr = null;
self.alternativeBalanceAvailable = false;
self.totalBalanceAlternative = null;
self.currentFeeLevel = null;
self.notAuthorized = false;
self.txHistory = [];
self.txHistoryPaging = false;
self.pendingTxProposalsCountForUs = null;
self.setSpendUnconfirmed();
self.glideraToken = null;
self.glideraTxs = null;
self.glideraEmail = null;
$timeout(function() {
self.hasProfile = true;
self.noFocusedWallet = false;
self.onGoingProcess = {};
// Credentials Shortcuts
self.m = fc.credentials.m;
self.n = fc.credentials.n;
self.network = fc.credentials.network;
self.copayerId = fc.credentials.copayerId;
self.copayerName = fc.credentials.copayerName;
self.requiresMultipleSignatures = fc.credentials.m > 1;
self.isShared = fc.credentials.n > 1;
self.walletName = fc.credentials.walletName;
self.walletId = fc.credentials.walletId;
self.isComplete = fc.isComplete();
self.canSign = fc.canSign();
self.txps = [];
self.copayers = [];
self.updateColor();
self.updateAlias();
self.initGlidera();
storageService.getBackupFlag(self.walletId, function(err, val) {
self.needsBackup = self.network == 'testnet' ? false : !val;
self.openWallet();
});
});
};
self.setTab = function(tab, reset, tries) {
tries = tries || 0;
// check if the whole menu item passed
if (typeof tab == 'object') {
if (tab.open) {
if (tab.link) {
self.tab = tab.link;
}
tab.open();
return;
} else {
return self.setTab(tab.link);
}
}
if (self.tab === tab && !reset)
return;
if (!document.getElementById('menu-' + tab) && ++tries < 5) {
return $timeout(function() {
self.setTab(tab, reset, tries);
}, 300);
}
if (!self.tab || !$state.is('walletHome'))
self.tab = 'walletHome';
go.path('walletHome', function() {
if (document.getElementById(self.tab)) {
document.getElementById(self.tab).className = 'tab-out tab-view ' + self.tab;
var old = document.getElementById('menu-' + self.tab);
if (old) {
old.className = '';
}
}
if (document.getElementById(tab)) {
document.getElementById(tab).className = 'tab-in tab-view ' + tab;
var newe = document.getElementById('menu-' + tab);
if (newe) {
newe.className = 'active';
}
}
self.tab = tab;
$rootScope.$emit('Local/TabChanged', tab);
});
};
self._updateRemotePreferencesFor = function(clients, prefs, cb) {
var client = clients.shift();
if (!client)
return cb();
$log.debug('Saving remote preferences', client.credentials.walletName, prefs);
client.savePreferences(prefs, function(err) {
// we ignore errors here
if (err) $log.warn(err);
self._updateRemotePreferencesFor(clients, prefs, cb);
});
};
self.updateRemotePreferences = function(opts, cb) {
var prefs = opts.preferences || {};
var fc = profileService.focusedClient;
// Update this JIC.
var config = configService.getSync().wallet.settings;
//prefs.email (may come from arguments)
prefs.language = self.defaultLanguageIsoCode;
prefs.unit = config.unitCode;
var clients = [];
if (opts.saveAll) {
clients = lodash.values(profileService.walletClients);
} else {
clients = [fc];
};
self._updateRemotePreferencesFor(clients, prefs, function(err) {
if (err) return cb(err);
if (!fc) return cb();
fc.getPreferences(function(err, preferences) {
if (err) {
return cb(err);
}
self.preferences = preferences;
return cb();
});
});
};
var _walletStatusHash = function(walletStatus) {
var bal;
if (walletStatus) {
bal = walletStatus.balance.totalAmount;
} else {
bal = self.totalBalanceSat;
}
return bal;
};
self.updateAll = function(opts, initStatusHash, tries) {
tries = tries || 0;
opts = opts || {};
if (opts.untilItChanges && lodash.isUndefined(initStatusHash)) {
initStatusHash = _walletStatusHash();
$log.debug('Updating status until it changes. initStatusHash:' + initStatusHash)
}
var get = function(cb) {
if (opts.walletStatus)
return cb(null, opts.walletStatus);
else {
self.updateError = false;
return fc.getStatus(function(err, ret) {
if (err) {
self.updateError = bwsError.msg(err, gettext('Could not update Wallet'));
} else {
if (!opts.quiet)
self.setOngoingProcess('scanning', ret.wallet.scanning);
}
return cb(err, ret);
});
}
};
var fc = profileService.focusedClient;
if (!fc) return;
$timeout(function() {
if (!opts.quiet)
self.setOngoingProcess('updatingStatus', true);
$log.debug('Updating Status:', fc, tries);
get(function(err, walletStatus) {
var currentStatusHash = _walletStatusHash(walletStatus);
$log.debug('Status update. hash:' + currentStatusHash + ' Try:' + tries);
if (!err && opts.untilItChanges && initStatusHash == currentStatusHash && tries < 7) {
return $timeout(function() {
$log.debug('Retrying update... Try:' + tries)
return self.updateAll({
walletStatus: null,
untilItChanges: true,
triggerTxUpdate: opts.triggerTxUpdate,
}, initStatusHash, ++tries);
}, 1400 * tries);
}
if (!opts.quiet)
self.setOngoingProcess('updatingStatus', false);
if (err) {
self.handleError(err);
return;
}
$log.debug('Wallet Status:', walletStatus);
self.setPendingTxps(walletStatus.pendingTxps);
self.setFeesOpts();
// Status Shortcuts
self.walletName = walletStatus.wallet.name;
self.walletSecret = walletStatus.wallet.secret;
self.walletStatus = walletStatus.wallet.status;
self.walletScanStatus = walletStatus.wallet.scanStatus;
self.copayers = walletStatus.wallet.copayers;
self.preferences = walletStatus.preferences;
self.setBalance(walletStatus.balance);
self.otherWallets = lodash.filter(profileService.getWallets(self.network), function(w) {
return w.id != self.walletId;
});;
// Notify external addons or plugins
$rootScope.$emit('Local/BalanceUpdated', walletStatus.balance);
$rootScope.$apply();
if (opts.triggerTxUpdate) {
$timeout(function() {
self.updateTxHistory();
}, 1);
}
});
});
};
self.setSpendUnconfirmed = function() {
self.spendUnconfirmed = configService.getSync().wallet.spendUnconfirmed;
};
self.setSendMax = function() {
// Set Send max
if (self.currentFeeLevel && self.totalBytesToSendMax) {
feeService.getCurrentFeeValue(self.currentFeeLevel, function(err, feePerKb) {
// KB to send max
if (self.totalBytesToSendMax) {
var feeToSendMaxSat = parseInt(((self.totalBytesToSendMax * feePerKb) / 1000.).toFixed(0));
self.feeRateToSendMax = feePerKb;
self.availableMaxBalance = strip((self.availableBalanceSat - feeToSendMaxSat) * self.satToUnit);
self.feeToSendMaxStr = profileService.formatAmount(feeToSendMaxSat) + ' ' + self.unitName;
} else {
self.feeToSendMaxStr = null;
self.feeRateToSendMax = null;
}
});
}
};
self.setCurrentFeeLevel = function(level) {
self.currentFeeLevel = level || configService.getSync().wallet.settings.feeLevel || 'normal';
self.setSendMax();
};
self.setFeesOpts = function() {
var fc = profileService.focusedClient;
if (!fc) return;
$timeout(function() {
feeService.getFeeLevels(function(levels) {
self.feeLevels = levels;
$rootScope.$apply();
});
});
};
self.updateBalance = function() {
var fc = profileService.focusedClient;
$timeout(function() {
self.setOngoingProcess('updatingBalance', true);
$log.debug('Updating Balance');
fc.getBalance(function(err, balance) {
self.setOngoingProcess('updatingBalance', false);
if (err) {
self.handleError(err);
return;
}
$log.debug('Wallet Balance:', balance);
self.setBalance(balance);
});
});
};
self.updatePendingTxps = function() {
var fc = profileService.focusedClient;
$timeout(function() {
self.setOngoingProcess('updatingPendingTxps', true);
$log.debug('Updating PendingTxps');
fc.getTxProposals({}, function(err, txps) {
self.setOngoingProcess('updatingPendingTxps', false);
if (err) {
self.handleError(err);
} else {
$log.debug('Wallet PendingTxps:', txps);
self.setPendingTxps(txps);
}
$rootScope.$apply();
});
});
};
self.updateTxHistory = function(skip) {
var fc = profileService.focusedClient;
if (!fc.isComplete()) return;
if (!skip) {
self.txHistory = [];
}
self.skipHistory = skip || 0;
$log.debug('Updating Transaction History');
self.txHistoryError = false;
self.updatingTxHistory = true;
self.txHistoryPaging = false;
$timeout(function() {
fc.getTxHistory({
skip: self.skipHistory,
limit: self.limitHistory + 1
}, function(err, txs) {
self.updatingTxHistory = false;
if (err) {
$log.debug('TxHistory ERROR:', err);
// We do not should errors here, since history is usually
// fetched AFTER others requests (if skip=0)
if (skip)
self.handleError(err);
self.txHistoryError = true;
} else {
$log.debug('Wallet Transaction History:', txs);
self.skipHistory = self.skipHistory + self.limitHistory;
self.setTxHistory(txs);
}
$rootScope.$apply();
});
});
};
// This handles errors from BWS/index with are nomally
// trigger from async events (like updates)
self.handleError = function(err) {
$log.warn('Client ERROR:', err);
if (err.code === 'NOT_AUTHORIZED') {
self.notAuthorized = true;
go.walletHome();
} else if (err.code === 'NOT_FOUND') {
self.showErrorPopup(gettext('Could not access Wallet Service: Not found'));
} else {
var msg = ""
$scope.$emit('Local/ClientError', (err.error ? err.error : err));
var msg = bwsError.msg(err, gettext('Error at Wallet Service'));
self.showErrorPopup(msg);
}
};
self.openWallet = function() {
var fc = profileService.focusedClient;
$timeout(function() {
$rootScope.$apply();
self.setOngoingProcess('openingWallet', true);
self.updateError = false;
fc.openWallet(function(err, walletStatus) {
self.setOngoingProcess('openingWallet', false);
if (err) {
self.updateError = true;
self.handleError(err);
return;
}
$log.debug('Wallet Opened');
self.updateAll(lodash.isObject(walletStatus) ? {
walletStatus: walletStatus
} : null);
$rootScope.$apply();
});
});
};
self.setPendingTxps = function(txps) {
self.pendingTxProposalsCountForUs = 0;
lodash.each(txps, function(tx) {
tx = utilService.processTx(tx);
var action = lodash.find(tx.actions, {
copayerId: self.copayerId
});
if (!action && tx.status == 'pending') {
tx.pendingForUs = true;
}
if (action && action.type == 'accept') {
tx.statusForUs = 'accepted';
} else if (action && action.type == 'reject') {
tx.statusForUs = 'rejected';
} else {
tx.statusForUs = 'pending';
}
if (!tx.deleteLockTime)
tx.canBeRemoved = true;
if (tx.creatorId != self.copayerId) {
self.pendingTxProposalsCountForUs = self.pendingTxProposalsCountForUs + 1;
}
addonManager.formatPendingTxp(tx);
});
self.txps = txps;
};
self.setTxHistory = function(txs) {
var config = configService.getSync().wallet.settings;
var now = Math.floor(Date.now() / 1000);
var c = 0;
self.txHistoryPaging = txs[self.limitHistory] ? true : false;
lodash.each(txs, function(tx) {
tx = utilService.processTx(tx);
// no future transactions...
if (tx.time > now)
tx.time = now;
if (c < self.limitHistory) {
self.txHistory.push(tx);
c++;
}
});
};
self.updateAlias = function() {
var config = configService.getSync();
config.aliasFor = config.aliasFor || {};
self.alias = config.aliasFor[self.walletId];
var fc = profileService.focusedClient;
fc.alias = self.alias;
};
self.updateColor = function() {
var config = configService.getSync();
config.colorFor = config.colorFor || {};
self.backgroundColor = config.colorFor[self.walletId] || '#4A90E2';
var fc = profileService.focusedClient;
fc.backgroundColor = self.backgroundColor;
};
self.setBalance = function(balance) {
if (!balance) return;
var config = configService.getSync().wallet.settings;
var COIN = 1e8;
// Address with Balance
self.balanceByAddress = balance.byAddress;
// SAT
if (self.spendUnconfirmed) {
self.totalBalanceSat = balance.totalAmount;
self.lockedBalanceSat = balance.lockedAmount;
self.availableBalanceSat = balance.availableAmount;
self.pendingAmount = null;
} else {
self.totalBalanceSat = balance.totalConfirmedAmount;
self.lockedBalanceSat = balance.lockedConfirmedAmount;
self.availableBalanceSat = balance.availableConfirmedAmount;
self.pendingAmount = balance.totalAmount - balance.totalConfirmedAmount;
}
// Selected unit
self.unitToSatoshi = config.unitToSatoshi;
self.satToUnit = 1 / self.unitToSatoshi;
self.unitName = config.unitName;
//STR
self.totalBalanceStr = profileService.formatAmount(self.totalBalanceSat) + ' ' + self.unitName;
self.lockedBalanceStr = profileService.formatAmount(self.lockedBalanceSat) + ' ' + self.unitName;
self.availableBalanceStr = profileService.formatAmount(self.availableBalanceSat) + ' ' + self.unitName;
if (self.pendingAmount) {
self.pendingAmountStr = profileService.formatAmount(self.pendingAmount) + ' ' + self.unitName;
} else {
self.pendingAmountStr = null;
}
self.alternativeName = config.alternativeName;
self.alternativeIsoCode = config.alternativeIsoCode;
// Other
self.totalBytesToSendMax = balance.totalBytesToSendMax;
self.setCurrentFeeLevel();
// Check address
addressService.isUsed(self.walletId, balance.byAddress, function(err, used) {
if (used) {
$log.debug('Address used. Creating new');
$rootScope.$emit('Local/NeedNewAddress');
}
});
rateService.whenAvailable(function() {
var totalBalanceAlternative = rateService.toFiat(self.totalBalanceSat, self.alternativeIsoCode);
var lockedBalanceAlternative = rateService.toFiat(self.lockedBalanceSat, self.alternativeIsoCode);
var alternativeConversionRate = rateService.toFiat(100000000, self.alternativeIsoCode);
self.totalBalanceAlternative = $filter('noFractionNumber')(totalBalanceAlternative, 2);
self.lockedBalanceAlternative = $filter('noFractionNumber')(lockedBalanceAlternative, 2);
self.alternativeConversionRate = $filter('noFractionNumber')(alternativeConversionRate, 2);
self.alternativeBalanceAvailable = true;
self.updatingBalance = false;
self.isRateAvailable = true;
$rootScope.$apply();
});
if (!rateService.isAvailable()) {
$rootScope.$apply();
}
};
this.csvHistory = function() {
function saveFile(name, data) {
var chooser = document.querySelector(name);
chooser.addEventListener("change", function(evt) {
var fs = require('fs');
fs.writeFile(this.value, data, function(err) {
if (err) {
$log.debug(err);
}
});
}, false);
chooser.click();
}
function formatDate(date) {
var dateObj = new Date(date);
if (!dateObj) {
$log.debug('Error formating a date');
return 'DateError'
}
if (!dateObj.toJSON()) {
return '';
}
return dateObj.toJSON();
}
function formatString(str) {
if (!str) return '';
if (str.indexOf('"') !== -1) {
//replace all
str = str.replace(new RegExp('"', 'g'), '\'');
}
//escaping commas
str = '\"' + str + '\"';
return str;
}
function getHistory(skip, cb) {
skip = skip || 0;
fc.getTxHistory({
skip: skip,
limit: 100
}, function(err, txs) {
if (err) return cb(err);
if (txs && txs.length > 0) {
allTxs.push(txs);
return getHistory(skip + 100, cb);
} else {
return cb(null, lodash.flatten(allTxs));
}
});
};
if (isCordova) {
$log.info('Not available on mobile');
return;
}
var isNode = nodeWebkit.isDefined();
var fc = profileService.focusedClient;
if (!fc.isComplete()) return;
var self = this;
var allTxs = [];
$log.debug('Generating CSV from History');
self.setOngoingProcess('generatingCSV', true);
$timeout(function() {
getHistory(null, function(err, txs) {
self.setOngoingProcess('generatingCSV', false);
if (err) {
self.handleError(err);
} else {
$log.debug('Wallet Transaction History:', txs);
self.satToUnit = 1 / self.unitToSatoshi;
var data = txs;
var satToBtc = 1 / 100000000;
var filename = 'Copay-' + (self.alias || self.walletName) + '.csv';
var csvContent = '';
if (!isNode) csvContent = 'data:text/csv;charset=utf-8,';
csvContent += 'Date,Destination,Note,Amount,Currency,Spot Value,Total Value,Tax Type,Category\n';
var _amount, _note;
var dataString;
data.forEach(function(it, index) {
var amount = it.amount;
if (it.action == 'moved')
amount = 0;
_amount = (it.action == 'sent' ? '-' : '') + (amount * satToBtc).toFixed(8);
_note = formatString((it.message ? it.message : '') + ' TxId: ' + it.txid + ' Fee:' + (it.fees * satToBtc).toFixed(8));
if (it.action == 'moved')
_note += ' Moved:' + (it.amount * satToBtc).toFixed(8)
dataString = formatDate(it.time * 1000) + ',' + formatString(it.addressTo) + ',' + _note + ',' + _amount + ',BTC,,,,';
csvContent += dataString + "\n";
if (it.fees && (it.action == 'moved' || it.action == 'sent')) {
var _fee = (it.fees * satToBtc).toFixed(8)
csvContent += formatDate(it.time * 1000) + ',Bitcoin Network Fees,, -' + _fee + ',BTC,,,,' + "\n";
}
});
if (isNode) {
saveFile('#export_file', csvContent);
} else {
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", filename);
link.click();
}
}
$rootScope.$apply();
});
});
};
self.showErrorPopup = function (msg, cb) {
$log.warn('Showing err popup:' + msg);
self.showAlert = {
msg: msg,
close: function(err) {
self.showAlert = null;
if (cb) return cb(err);
},
};
$timeout(function() {
$rootScope.$apply();
});
};
self.recreate = function(cb) {
var fc = profileService.focusedClient;
self.setOngoingProcess('recreating', true);
fc.recreateWallet(function(err) {
self.notAuthorized = false;
self.setOngoingProcess('recreating', false);
if (err) {
self.handleError(err);
$rootScope.$apply();
return;
}
profileService.setWalletClients();
$timeout(function() {
$rootScope.$emit('Local/WalletImported', self.walletId);
}, 100);
});
};
self.openMenu = function() {
go.swipe(true);
};
self.closeMenu = function() {
go.swipe();
};
self.retryScan = function() {
var self = this;
self.startScan(self.walletId);
}
self.startScan = function(walletId) {
var c = profileService.walletClients[walletId];
if (self.walletId == walletId)
self.setOngoingProcess('scanning', true);
c.startScan({
includeCopayerBranches: true,
}, function(err) {
if (err && self.walletId == walletId) {
self.setOngoingProcess('scanning', false);
self.handleError(err);
$rootScope.$apply();
}
});
};
self.setUxLanguage = function() {
var userLang = configService.getSync().wallet.settings.defaultLanguage;
if (!userLang) {
// Auto-detect browser language
var androidLang;
if (navigator && navigator.userAgent && (androidLang = navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) {
userLang = androidLang[1];
} else {
// works for iOS and Android 4.x
userLang = navigator.userLanguage || navigator.language;
}
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
}
if (userLang != gettextCatalog.getCurrentLanguage()) {
$log.debug('Setting default language: ' + userLang);
gettextCatalog.setCurrentLanguage(userLang);
amMoment.changeLocale(userLang);
}
self.defaultLanguageIsoCode = userLang;
self.defaultLanguageName = lodash.result(lodash.find(self.availableLanguages, {
'isoCode': self.defaultLanguageIsoCode
}), 'name');
};
self.initGlidera = function() {
if (self.isShared) return;
storageService.getGlideraToken(self.network, function(err, val) {
if (err) return;
else {
self.glideraToken = val;
glideraService.getTransactions(val, function(error, txs) {
self.glideraTxs = txs;
});
glideraService.getEmail(val, function(error, data) {
self.glideraEmail = data.email;
});
}
});
};
// UX event handlers
$rootScope.$on('Local/ColorUpdated', function(event) {
self.updateColor();
$timeout(function() {
$rootScope.$apply();
});
});
$rootScope.$on('Local/AliasUpdated', function(event) {
self.updateAlias();
$timeout(function() {
$rootScope.$apply();
});
});
$rootScope.$on('Local/SpendUnconfirmedUpdated', function(event) {
self.setSpendUnconfirmed();
self.updateAll();
});
$rootScope.$on('Local/FeeLevelUpdated', function(event, level) {
self.setCurrentFeeLevel(level);
});
$rootScope.$on('Local/ProfileBound', function() {
storageService.getRemotePrefsStoredFlag(function(err, val) {
if (err || val) return;
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved')
storageService.setRemotePrefsStoredFlag(function() {});
});
});
});
$rootScope.$on('Local/NewFocusedWallet', function() {
self.setUxLanguage();
});
$rootScope.$on('Local/LanguageSettingUpdated', function() {
self.setUxLanguage();
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved')
});
});
$rootScope.$on('Local/GlideraTokenUpdated', function() {
self.initGlidera();
});
$rootScope.$on('Local/UnitSettingUpdated', function(event) {
self.updateAll();
self.updateTxHistory();
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved')
});
});
$rootScope.$on('Local/EmailSettingUpdated', function(event, email, cb) {
self.updateRemotePreferences({
preferences: {
email: email || null
},
}, cb);
});
$rootScope.$on('Local/BWSUpdated', function(event) {
profileService.applyConfig();
storageService.setCleanAndScanAddresses(function() {});
});
$rootScope.$on('Local/WalletCompleted', function(event) {
self.setFocusedWallet();
go.walletHome();
});
self.debouncedUpdate = lodash.throttle(function() {
self.updateAll({
quiet: true
});
self.updateTxHistory();
}, 4000, {
leading: false,
trailing: true
});
self.debouncedUpdateHistory = lodash.throttle(function() {
self.updateTxHistory();
}, 60000);
$rootScope.$on('Local/Resume', function(event) {
$log.debug('### Resume event');
self.debouncedUpdate();
});
$rootScope.$on('Local/BackupDone', function(event) {
self.needsBackup = false;
storageService.setBackupFlag(self.walletId, function(err) {
if (err) root.showErrorPopup(err);
});
});
$rootScope.$on('Local/DeviceError', function(event, err) {
root.showErrorPopup(err);
});
$rootScope.$on('Local/WalletImported', function(event, walletId) {
self.needsBackup = false;
storageService.setBackupFlag(walletId, function() {
addressService.expireAddress(walletId, function(err) {
self.startScan(walletId);
});
});
});
$rootScope.$on('NewIncomingTx', function() {
self.updateAll({
walletStatus: null,
untilItChanges: true,
triggerTxUpdate: true,
});
});
$rootScope.$on('NewBlock', function() {
if (self.pendingAmount) {
self.updateAll();
}
if (self.network == 'testnet') {
self.debouncedUpdateHistory();
} else {
self.updateTxHistory();
}
});
$rootScope.$on('NewOutgoingTx', function() {
self.updateAll({
walletStatus: null,
untilItChanges: true,
triggerTxUpdate: true,
});
});
lodash.each(['NewTxProposal', 'TxProposalFinallyRejected', 'TxProposalRemoved',
'Local/NewTxProposal', 'Local/TxProposalAction', 'ScanFinished'
], function(eventName) {
$rootScope.$on(eventName, function(event, untilItChanges) {
self.updateAll({
walletStatus: null,
untilItChanges: untilItChanges,
triggerTxUpdate: true,
});
});
});
lodash.each(['TxProposalRejectedBy', 'TxProposalAcceptedBy'], function(eventName) {
$rootScope.$on(eventName, function() {
var f = function() {
if (self.updatingStatus) {
return $timeout(f, 200);
};
self.updatePendingTxps();
};
f();
});
});
$rootScope.$on('Local/NoWallets', function(event) {
$timeout(function() {
self.hasProfile = true;
self.noFocusedWallet = true;
self.isComplete = null;
self.walletName = null;
go.path('import');
});
});
$rootScope.$on('Local/NewFocusedWallet', function() {
self.setFocusedWallet();
self.updateTxHistory();
go.walletHome();
storageService.getCleanAndScanAddresses(function(err, val) {
if (val) {
$log.debug('Clear last address cache and Scan');
lodash.each(lodash.keys(profileService.walletClients), function(walletId) {
addressService.expireAddress(walletId, function(err) {
self.startScan(walletId);
});
});
storageService.removeCleanAndScanAddresses(function() {});
}
});
});
$rootScope.$on('Local/SetTab', function(event, tab, reset) {
self.setTab(tab, reset);
});
$rootScope.$on('Local/ShowAlert', function(event, msg, cb) {
self.showErrorPopup(msg,cb);
});
$rootScope.$on('Local/NeedsPassword', function(event, isSetup, cb) {
self.askPassword = {
isSetup: isSetup,
callback: function(err, pass) {
self.askPassword = null;
return cb(err, pass);
},
};
});
lodash.each(['NewCopayer', 'CopayerUpdated'], function(eventName) {
$rootScope.$on(eventName, function() {
// Re try to open wallet (will triggers)
self.setFocusedWallet();
});
});
});