copay/src/js/controllers/index.js

790 lines
22 KiB
JavaScript
Raw Normal View History

2015-03-06 07:00:10 -08:00
'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, gettextCatalog, gettext, amMoment) {
2015-04-26 07:41:25 -07:00
2015-03-06 07:00:10 -08:00
var self = this;
self.isCordova = isCordova;
self.onGoingProcess = {};
2015-04-23 11:19:30 -07:00
self.limitHistory = 5;
2015-03-06 07:00:10 -08:00
function strip(number) {
return (parseFloat(number.toPrecision(12)));
};
2015-05-07 14:35:54 -07:00
self.goHome = function() {
go.walletHome();
};
2015-04-21 22:48:00 -07:00
self.menu = [{
'title': gettext('Home'),
2015-04-21 22:48:00 -07:00
'icon': 'icon-home',
'link': 'walletHome'
2015-04-21 22:48:00 -07:00
}, {
'title': gettext('Receive'),
2015-04-29 08:16:28 -07:00
'icon': 'icon-receive2',
2015-04-21 22:48:00 -07:00
'link': 'receive'
}, {
'title': gettext('Send'),
2015-04-21 22:48:00 -07:00
'icon': 'icon-paperplane',
'link': 'send'
}, {
'title': gettext('History'),
2015-04-21 22:48:00 -07:00
'icon': 'icon-history',
'link': 'history'
2015-04-23 11:19:30 -07:00
}];
2015-04-21 22:48:00 -07:00
2015-04-23 08:27:43 -07:00
self.tab = 'walletHome';
2015-04-22 11:19:08 -07:00
self.availableLanguages = [{
name: gettext('English'),
2015-04-22 11:19:08 -07:00
isoCode: 'en',
}, {
name: gettext('Spanish'),
2015-04-22 11:19:08 -07:00
isoCode: 'es',
}, {
name: gettext('French'),
2015-04-22 11:19:08 -07:00
isoCode: 'fr',
}, {
name: gettext('Japanese'),
2015-04-22 11:19:08 -07:00
isoCode: 'ja',
}];
2015-03-06 07:00:10 -08:00
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;
2015-04-14 08:51:49 -07:00
$timeout(function() {
$rootScope.$apply();
});
2015-03-06 07:00:10 -08:00
};
self.setFocusedWallet = function() {
var fc = profileService.focusedClient;
if (!fc) return;
2015-04-15 09:39:48 -07:00
// Clean status
self.lockedBalance = null;
self.totalBalanceStr = null;
self.alternativeBalanceAvailable = false;
self.totalBalanceAlternative = null;
self.notAuthorized = false;
self.txHistory = [];
self.txHistoryPaging = false;
self.pendingTxProposalsCountForUs = null;
2015-04-15 09:39:48 -07:00
2015-03-06 07:00:10 -08:00
$timeout(function() {
self.hasProfile = true;
self.noFocusedWallet = false;
self.onGoingProcess = {};
2015-04-29 08:16:28 -07:00
// Credentials Shortcuts
2015-03-06 07:00:10 -08:00
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.txps = [];
self.copayers = [];
2015-04-28 16:13:28 -07:00
self.updateColor();
2015-05-14 06:39:22 -07:00
self.updateAlias();
2015-03-06 07:00:10 -08:00
storageService.getBackupFlag(self.walletId, function(err, val) {
2015-04-26 16:30:09 -07:00
self.needsBackup = self.network == 'testnet' ? false : !val;
2015-03-06 07:00:10 -08:00
self.openWallet();
});
});
};
2015-04-28 16:13:28 -07:00
self.setTab = function(tab, reset, tries) {
tries = tries || 0;
if (self.tab === tab && !reset)
return;
2015-05-04 08:23:43 -07:00
if (!document.getElementById('menu-' + tab) && ++tries < 5) {
2015-04-28 16:13:28 -07:00
return $timeout(function() {
2015-05-04 08:23:43 -07:00
self.setTab(tab, reset, tries);
2015-04-28 16:13:28 -07:00
}, 300);
}
if (!self.tab)
self.tab = 'walletHome';
if (document.getElementById(self.tab)) {
2015-04-23 11:19:30 -07:00
document.getElementById(self.tab).className = 'tab-out tab-view ' + self.tab;
var old = document.getElementById('menu-' + self.tab);
if (old) {
old.className = '';
}
2015-04-23 11:19:30 -07:00
}
2015-04-23 10:12:32 -07:00
2015-04-23 11:19:30 -07:00
if (document.getElementById(tab)) {
document.getElementById(tab).className = 'tab-in tab-view ' + tab;
var newe = document.getElementById('menu-' + tab);
2015-04-23 14:05:31 -07:00
if (newe) {
newe.className = 'active';
}
2015-04-23 11:19:30 -07:00
}
2015-04-23 10:12:32 -07:00
2015-04-23 09:12:30 -07:00
self.tab = tab;
2015-04-23 11:19:30 -07:00
$rootScope.$emit('Local/TabChanged', tab);
2015-04-23 09:12:30 -07:00
};
2015-05-19 10:10:47 -07:00
self.updatePreferences = function(cb) {
var fc = profileService.focusedClient;
fc.getPreferences(function(err, preferences) {
if (err) {
self.handleError(err);
return cb(err);
}
self.preferences = preferences;
return cb(err, preferences);
});
};
2015-05-30 10:28:18 -07:00
var _walletStatusHash = function(walletStatus) {
2015-05-30 18:38:08 -07:00
var bal;
2015-05-30 10:28:18 -07:00
if (walletStatus) {
2015-05-30 18:38:08 -07:00
bal = walletStatus.balance.totalAmount;
2015-05-30 10:28:18 -07:00
} else {
2015-05-30 18:38:08 -07:00
bal = self.totalBalanceSat;
2015-05-30 10:28:18 -07:00
}
2015-05-30 18:38:08 -07:00
return bal;
2015-05-30 10:28:18 -07:00
};
2015-05-19 10:10:47 -07:00
2015-05-30 10:28:18 -07:00
self.updateAll = function(walletStatus, untilItChanges, initStatusHash, tries) {
tries = tries || 0;
2015-05-30 10:28:18 -07:00
if (untilItChanges && lodash.isUndefined(initStatusHash)) {
initStatusHash = _walletStatusHash();
2015-05-30 18:38:08 -07:00
$log.debug('Updating status until it changes. initStatusHash:' + initStatusHash)
}
2015-03-06 07:00:10 -08:00
var get = function(cb) {
if (walletStatus)
return cb(null, walletStatus);
2015-04-13 10:58:07 -07:00
else {
self.updateError = false;
return fc.getStatus(function(err, ret) {
2015-04-15 10:03:38 -07:00
if (err) {
self.updateError = true;
} else {
self.setOngoingProcess('scanning', ret.wallet.scanning);
}
2015-04-13 10:58:07 -07:00
return cb(err, ret);
});
}
2015-03-06 07:00:10 -08:00
};
var fc = profileService.focusedClient;
if (!fc) return;
$timeout(function() {
self.setOngoingProcess('updatingStatus', true);
2015-05-29 08:39:17 -07:00
$log.debug('Updating Status:', fc, tries);
2015-03-06 07:00:10 -08:00
get(function(err, walletStatus) {
2015-05-30 18:38:08 -07:00
var currentStatusHash = _walletStatusHash(walletStatus);
$log.debug('Status update. hash:' + currentStatusHash + ' Try:'+ tries);
if (!err && untilItChanges && initStatusHash == currentStatusHash && tries < 7) {
return $timeout(function() {
2015-05-30 18:38:08 -07:00
$log.debug('Retrying update... Try:' + tries)
2015-05-30 10:28:18 -07:00
return self.updateAll(null, true, initStatusHash, ++tries);
2015-05-29 08:39:17 -07:00
}, 1400 * tries);
}
2015-03-06 07:00:10 -08:00
self.setOngoingProcess('updatingStatus', false);
if (err) {
self.handleError(err);
return;
}
$log.debug('Wallet Status:', walletStatus);
self.setPendingTxps(walletStatus.pendingTxps);
// Status Shortcuts
self.walletName = walletStatus.wallet.name;
self.walletSecret = walletStatus.wallet.secret;
self.walletStatus = walletStatus.wallet.status;
2015-04-15 10:03:38 -07:00
self.walletScanStatus = walletStatus.wallet.scanStatus;
2015-03-06 07:00:10 -08:00
self.copayers = walletStatus.wallet.copayers;
2015-05-19 10:10:47 -07:00
self.preferences = walletStatus.preferences;
2015-03-06 07:00:10 -08:00
self.setBalance(walletStatus.balance);
$rootScope.$apply();
2015-03-06 07:00:10 -08:00
});
});
};
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) {
$log.debug('Wallet Balance ERROR:', err);
$scope.$emit('Local/ClientError', 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');
2015-04-18 03:08:08 -07:00
fc.getTxProposals({}, function(err, txps) {
2015-03-06 07:00:10 -08:00
self.setOngoingProcess('updatingPendingTxps', false);
if (err) {
$log.debug('Wallet PendingTxps ERROR:', err);
$scope.$emit('Local/ClientError', err);
} else {
$log.debug('Wallet PendingTxps:', txps);
self.setPendingTxps(txps);
}
$rootScope.$apply();
});
});
};
self.updateTxHistory = function(skip) {
var fc = profileService.focusedClient;
2015-04-23 23:26:57 -07:00
if (!fc.isComplete()) return;
2015-03-06 07:00:10 -08:00
if (!skip) {
self.txHistory = [];
}
self.skipHistory = skip || 0;
2015-04-23 22:42:10 -07:00
$log.debug('Updating Transaction History');
self.txHistoryError = false;
self.updatingTxHistory = true;
2015-04-23 23:01:26 -07:00
self.txHistoryPaging = false;
2015-03-06 07:00:10 -08:00
$timeout(function() {
fc.getTxHistory({
skip: self.skipHistory,
limit: self.limitHistory + 1
}, function(err, txs) {
2015-04-14 08:51:49 -07:00
self.updatingTxHistory = false;
2015-03-06 07:00:10 -08:00
if (err) {
$log.debug('TxHistory ERROR:', err);
2015-05-15 07:07:19 -07:00
// We do not should errors here, since history is usually
// fetched AFTER others requests.
//self.handleError(err);
2015-04-13 10:58:07 -07:00
self.txHistoryError = true;
} else {
2015-03-06 07:00:10 -08:00
$log.debug('Wallet Transaction History:', txs);
self.skipHistory = self.skipHistory + self.limitHistory;
self.setTxHistory(txs);
}
$rootScope.$apply();
});
});
};
self.handleError = function(err) {
2015-04-27 10:14:51 -07:00
$log.warn('Client ERROR:', err);
2015-03-06 07:00:10 -08:00
if (err.code === 'NOTAUTHORIZED') {
$scope.$emit('Local/NotAuthorized');
} else if (err.code === 'NOTFOUND') {
$scope.$emit('Local/BWSNotFound');
} else {
2015-04-27 10:18:22 -07:00
$scope.$emit('Local/ClientError', (err.error ? err.error : err));
2015-03-06 07:00:10 -08:00
}
};
self.openWallet = function() {
var fc = profileService.focusedClient;
$timeout(function() {
2015-04-23 11:32:11 -07:00
$rootScope.$apply();
2015-03-06 07:00:10 -08:00
self.setOngoingProcess('openingWallet', true);
2015-04-14 12:06:04 -07:00
self.updateError = false;
2015-03-06 07:00:10 -08:00
fc.openWallet(function(err, walletStatus) {
self.setOngoingProcess('openingWallet', false);
if (err) {
2015-04-14 12:06:04 -07:00
self.updateError = true;
2015-03-06 07:00:10 -08:00
self.handleError(err);
return;
}
$log.debug('Wallet Opened');
self.updateAll(lodash.isObject(walletStatus) ? walletStatus : null);
$rootScope.$apply();
});
});
};
self.setPendingTxps = function(txps) {
var config = configService.getSync().wallet.settings;
self.pendingTxProposalsCountForUs = 0;
lodash.each(txps, function(tx) {
var amount = tx.amount * self.satToUnit;
tx.amountStr = profileService.formatAmount(tx.amount) + ' ' + config.unitName;
tx.alternativeAmount = rateService.toFiat(tx.amount, config.alternativeIsoCode) ? rateService.toFiat(tx.amount, config.alternativeIsoCode).toFixed(2) : 'N/A';
tx.alternativeAmountStr = tx.alternativeAmount + " " + config.alternativeIsoCode;
tx.alternativeIsoCode = config.alternativeIsoCode;
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.creatorId == self.copayerId && tx.actions.length == 1) {
tx.couldRemove = true;
};
if (tx.creatorId != self.copayerId) {
self.pendingTxProposalsCountForUs = self.pendingTxProposalsCountForUs + 1;
}
});
self.txps = txps;
};
self.setTxHistory = function(txs) {
var now = new Date();
var c = 0;
self.txHistoryPaging = txs[self.limitHistory] ? true : false;
lodash.each(txs, function(tx) {
tx.ts = tx.minedTs || tx.sentTs;
2015-05-08 08:02:11 -07:00
// no future transaction...
2015-05-19 10:10:47 -07:00
if (tx.ts > now)
2015-05-08 08:02:11 -07:00
ts.ts = now;
2015-03-06 07:00:10 -08:00
tx.rateTs = Math.floor((tx.ts || now) / 1000);
tx.amountStr = profileService.formatAmount(tx.amount); //$filter('noFractionNumber')(
if (c < self.limitHistory) {
self.txHistory.push(tx);
c++;
}
});
};
2015-05-14 06:39:22 -07:00
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;
};
2015-03-06 07:00:10 -08:00
self.updateColor = function() {
var config = configService.getSync();
config.colorFor = config.colorFor || {};
2015-05-29 11:25:41 -07:00
self.backgroundColor = config.colorFor[self.walletId] || '#4A90E2';
2015-03-06 07:00:10 -08:00
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
self.totalBalanceSat = balance.totalAmount;
self.lockedBalanceSat = balance.lockedAmount;
self.availableBalanceSat = self.totalBalanceSat - self.lockedBalanceSat;
// Selected unit
self.unitToSatoshi = config.unitToSatoshi;
self.satToUnit = 1 / self.unitToSatoshi;
self.unitName = config.unitName;
self.totalBalance = strip(self.totalBalanceSat * self.satToUnit);
self.lockedBalance = strip(self.lockedBalanceSat * self.satToUnit);
self.availableBalance = strip(self.availableBalanceSat * self.satToUnit);
// BTC
self.totalBalanceBTC = strip(self.totalBalanceSat / COIN);
self.lockedBalanceBTC = strip(self.lockedBalanceSat / COIN);
self.availableBalanceBTC = strip(self.availableBalanceBTC / COIN);
//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;
self.alternativeName = config.alternativeName;
self.alternativeIsoCode = config.alternativeIsoCode;
// Check address
self.checkLastAddress(balance.byAddress);
rateService.whenAvailable(function() {
var totalBalanceAlternative = rateService.toFiat(self.totalBalance * self.unitToSatoshi, self.alternativeIsoCode);
var lockedBalanceAlternative = rateService.toFiat(self.lockedBalance * self.unitToSatoshi, 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();
}
};
self.checkLastAddress = function(byAddress, cb) {
storageService.getLastAddress(self.walletId, function(err, addr) {
var used = lodash.find(byAddress, {
address: addr
});
if (used) {
$log.debug('Address ' + addr + ' was used. Cleaning Cache.')
2015-04-18 03:23:11 -07:00
storageService.clearLastAddress(self.walletId, function(err) {
2015-04-28 12:58:40 -07:00
$rootScope.$emit('Local/NeedNewAddress', err);
2015-03-06 07:00:10 -08:00
if (cb) return cb();
});
};
});
};
2015-04-26 16:30:09 -07:00
self.clientError = function(err) {
if (isCordova) {
navigator.notification.confirm(
err,
function() {},
'Wallet Server Error', ['OK']
);
} else {
alert(err);
}
};
2015-03-06 07:00:10 -08:00
2015-04-26 16:30:09 -07:00
self.deviceError = function(err) {
if (isCordova) {
navigator.notification.confirm(
err,
function() {},
'Device Error', ['OK']
);
} else {
alert(err);
}
};
2015-03-06 07:00:10 -08:00
self.recreate = function(cb) {
var fc = profileService.focusedClient;
self.setOngoingProcess('recreating', true);
fc.recreateWallet(function(err) {
self.notAuthorized = false;
self.setOngoingProcess('recreating', false);
2015-04-14 08:51:49 -07:00
if (err) {
2015-04-27 10:14:51 -07:00
self.handleError(err);
2015-04-14 08:51:49 -07:00
$rootScope.$apply();
return;
}
2015-03-06 07:00:10 -08:00
profileService.setWalletClients();
2015-04-18 03:23:11 -07:00
$timeout(function() {
$rootScope.$emit('Local/WalletImported', self.walletId);
}, 100);
2015-03-06 07:00:10 -08:00
});
};
self.openMenu = function() {
go.swipe(true);
};
self.closeMenu = function() {
go.swipe();
};
2015-04-15 10:03:38 -07:00
self.retryScan = function() {
var self = this;
self.startScan(self.walletId);
}
2015-03-06 07:00:10 -08:00
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) {
if (self.walletId == walletId)
self.setOngoingProcess('scanning', false);
2015-04-27 10:14:51 -07:00
self.handleError(err);
2015-04-14 08:51:49 -07:00
$rootScope.$apply();
2015-03-06 07:00:10 -08:00
}
});
};
2015-04-22 14:41:30 -07:00
self.setDefaultLanguage = function(setLang) {
var userLang;
2015-04-22 14:41:30 -07:00
if (!setLang) {
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);
}
2015-04-23 11:19:30 -07:00
} else {
2015-04-22 14:41:30 -07:00
configService.set({
wallet: {
settings: {
defaultLanguage: setLang
}
}
}, function() {
gettextCatalog.setCurrentLanguage(setLang);
amMoment.changeLocale(setLang);
});
}
self.defaultLanguageIsoCode = setLang || userLang;
2015-04-23 11:19:30 -07:00
self.defaultLanguageName = lodash.result(lodash.find(self.availableLanguages, {
'isoCode': self.defaultLanguageIsoCode
}), 'name');
2015-04-22 14:41:30 -07:00
};
2015-03-06 07:00:10 -08:00
// UX event handlers
$rootScope.$on('Local/ColorUpdated', function(event) {
self.updateColor();
2015-04-27 09:11:32 -07:00
$timeout(function() {
$rootScope.$apply();
});
2015-03-06 07:00:10 -08:00
});
2015-05-14 06:39:22 -07:00
$rootScope.$on('Local/AliasUpdated', function(event) {
self.updateAlias();
$timeout(function() {
$rootScope.$apply();
});
});
2015-04-25 08:37:04 -07:00
$rootScope.$on('Local/UnitSettingUpdated', function(event) {
2015-03-06 07:00:10 -08:00
self.updateAll();
2015-04-21 08:20:54 -07:00
self.updateTxHistory();
2015-03-06 07:00:10 -08:00
});
2015-04-25 08:37:04 -07:00
2015-05-19 10:10:47 -07:00
$rootScope.$on('Local/EmailUpdated', function(event, cb) {
self.updatePreferences(cb);
});
2015-04-25 08:37:04 -07:00
$rootScope.$on('Local/BWSUpdated', function(event) {
profileService.applyConfig();
});
2015-03-06 07:00:10 -08:00
$rootScope.$on('Local/WalletCompleted', function(event) {
self.setFocusedWallet();
go.walletHome();
});
2015-04-28 15:26:22 -07:00
self.debouncedUpdate = lodash.throttle(function() {
self.updateAll();
self.updateTxHistory();
2015-05-04 08:23:43 -07:00
}, 4000, {
leading: false,
trailing: true
});
2015-04-28 15:26:22 -07:00
2015-04-29 08:16:28 -07:00
// No need ot listing to Local/Resume since
2015-04-28 15:26:22 -07:00
// reconnection and Local/Online will be triggered
2015-04-13 10:58:07 -07:00
lodash.each(['Local/Online', 'Local/Resume'], function(eventName) {
$rootScope.$on(eventName, function(event) {
2015-04-28 15:26:22 -07:00
$log.debug('### ' + eventName + ' event');
self.debouncedUpdate();
2015-04-13 10:58:07 -07:00
});
2015-03-06 07:00:10 -08:00
});
2015-04-28 15:26:22 -07:00
$rootScope.$on('Local/Online', function(event) {
self.isOffline = false;
2015-05-04 08:23:43 -07:00
self.offLineSince = null;
2015-04-28 15:26:22 -07:00
});
2015-05-04 08:23:43 -07:00
self.offLineSince = null;;
2015-04-13 10:58:07 -07:00
$rootScope.$on('Local/Offline', function(event) {
2015-04-28 15:26:22 -07:00
$log.debug('### Offline event');
2015-05-04 08:23:43 -07:00
if (!self.offLineSince) self.offLineSince = Date.now();
if (Date.now() - self.offLineSince > 10000) {
2015-05-04 08:23:43 -07:00
self.isOffline = true;
$timeout(function() {
$rootScope.$apply();
});
}
2015-03-06 07:00:10 -08:00
});
$rootScope.$on('Local/BackupDone', function(event) {
self.needsBackup = false;
2015-04-27 08:35:26 -07:00
storageService.setBackupFlag(self.walletId, function(err) {
if (err) $rootScope.$emit('Local/DeviceError', err)
});
2015-03-06 07:00:10 -08:00
});
$rootScope.$on('Local/NotAuthorized', function(event) {
self.notAuthorized = true;
$rootScope.$apply();
});
$rootScope.$on('Local/BWSNotFound', function(event) {
2015-04-23 23:26:57 -07:00
self.clientError('Could not access Wallet Service: Not found');
2015-03-06 07:00:10 -08:00
$rootScope.$apply();
});
$rootScope.$on('Local/DeviceError', function(event, err) {
self.deviceError(err);
$rootScope.$apply();
});
2015-03-06 07:00:10 -08:00
$rootScope.$on('Local/ClientError', function(event, err) {
2015-04-13 07:57:58 -07:00
if (err.code && err.code === 'NOTAUTHORIZED') {
// Show not error, just redirect to home (where the recreate option is shown)
go.walletHome();
2015-04-13 10:58:07 -07:00
} else if (err && err.cors == 'rejected') {
$log.debug('CORS error:', err);
2015-05-29 07:46:33 -07:00
} else if (err.code === 'ETIMEDOUT' || err.code === 'CONNERROR') {
2015-04-13 12:24:25 -07:00
$log.debug('Time out:', err);
2015-04-13 07:57:58 -07:00
} else {
2015-04-24 06:10:01 -07:00
var msg = 'Error at Wallet Service: ';
if (err.message) msg = msg + err.message;
2015-04-24 10:46:09 -07:00
else if (err.error) msg = msg + err.error;
2015-05-19 10:10:47 -07:00
else msg = msg + (lodash.isObject(err) ? JSON.stringify(err) : err);
2015-04-24 06:10:01 -07:00
self.clientError(msg);
2015-04-13 07:57:58 -07:00
}
2015-03-06 07:00:10 -08:00
$rootScope.$apply();
});
$rootScope.$on('Local/WalletImported', function(event, walletId) {
2015-05-04 11:47:04 -07:00
self.needsBackup = false;
2015-05-04 08:23:43 -07:00
storageService.setBackupFlag(walletId, function() {
storageService.clearLastAddress(walletId, function(err) {
self.startScan(walletId);
});
2015-04-18 03:23:11 -07:00
});
2015-03-06 07:00:10 -08:00
});
2015-04-22 14:41:30 -07:00
$rootScope.$on('Local/DefaultLanguage', function(event, setLang) {
self.setDefaultLanguage(setLang);
});
2015-04-15 10:03:38 -07:00
$rootScope.$on('NewIncomingTx', function() {
self.updateBalance();
$timeout(function() {
self.updateTxHistory();
}, 5000);
2015-03-06 07:00:10 -08:00
});
2015-05-29 08:39:17 -07:00
$rootScope.$on('NewOutgoingTx', function() {
self.updateAll(null, true);
});
lodash.each(['NewTxProposal', 'TxProposalFinallyRejected', 'TxProposalRemoved',
2015-04-15 10:03:38 -07:00
'Local/NewTxProposal', 'Local/TxProposalAction', 'ScanFinished'
2015-03-06 07:00:10 -08:00
], function(eventName) {
$rootScope.$on(eventName, function(event, untilItChanges) {
self.updateAll(null, untilItChanges);
2015-03-06 07:00:10 -08:00
$timeout(function() {
self.updateTxHistory();
2015-04-26 22:07:26 -07:00
}, 3000);
2015-03-06 07:00:10 -08:00
});
});
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;
2015-05-13 07:58:19 -07:00
go.path('import');
2015-03-06 07:00:10 -08:00
});
});
$rootScope.$on('Local/NewFocusedWallet', function() {
self.setFocusedWallet();
self.updateTxHistory();
2015-05-08 11:19:42 -07:00
go.walletHome();
2015-03-06 07:00:10 -08:00
});
2015-04-28 16:13:28 -07:00
$rootScope.$on('Local/SetTab', function(event, tab, reset) {
self.setTab(tab, reset);
2015-04-23 10:37:44 -07:00
});
2015-03-06 07:00:10 -08:00
$rootScope.$on('Local/NeedsPassword', function(event, isSetup, cb) {
self.askPassword = {
isSetup: isSetup,
2015-04-13 10:58:07 -07:00
callback: function(err, pass) {
2015-03-06 07:00:10 -08:00
self.askPassword = null;
return cb(err, pass);
},
};
});
lodash.each(['NewCopayer', 'CopayerUpdated'], function(eventName) {
$rootScope.$on(eventName, function() {
2015-04-29 08:16:28 -07:00
// Re try to open wallet (will triggers)
2015-03-06 07:00:10 -08:00
self.setFocusedWallet();
});
});
});