BlueWallet/screen/wallets/list.js

553 lines
19 KiB
JavaScript
Raw Normal View History

2018-01-30 23:42:38 +01:00
import React, { Component } from 'react';
2018-09-16 08:24:45 +02:00
import { View, TouchableOpacity, Dimensions, Text, FlatList } from 'react-native';
2018-01-30 23:42:38 +01:00
import {
BlueText,
BlueTransactionOnchainIcon,
ManageFundsBigButton,
2018-03-17 21:39:21 +01:00
BlueLoading,
SafeBlueArea,
2018-06-25 00:22:46 +02:00
WalletsCarousel,
BlueTransactionIncommingIcon,
BlueTransactionOutgoingIcon,
BlueTransactionPendingIcon,
2018-09-05 00:18:24 +02:00
BlueTransactionOffchainIcon,
2018-06-25 00:22:46 +02:00
BlueSendButtonIcon,
BlueReceiveButtonIcon,
BlueRefreshIcon,
BlueList,
BlueListItem,
2018-06-28 03:43:28 +02:00
BlueHeaderDefaultMain,
2018-07-01 19:00:03 +02:00
is,
2018-03-17 21:39:21 +01:00
} from '../../BlueComponents';
import { Icon } from 'react-native-elements';
2018-03-18 03:48:23 +01:00
import PropTypes from 'prop-types';
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
const BigNumber = require('bignumber.js');
2018-03-17 21:39:21 +01:00
let EV = require('../../events');
2018-07-06 17:41:48 +02:00
let A = require('../../analytics');
2018-03-18 03:48:23 +01:00
/** @type {AppStorage} */
let BlueApp = require('../../BlueApp');
2018-05-28 21:18:11 +02:00
let loc = require('../../loc');
2018-09-16 08:24:45 +02:00
const { width } = Dimensions.get('window');
2018-01-30 23:42:38 +01:00
export default class WalletsList extends Component {
static navigationOptions = {
2018-06-25 00:22:46 +02:00
tabBarVisible: false,
2018-03-17 21:39:21 +01:00
};
2018-01-30 23:42:38 +01:00
constructor(props) {
super(props);
this.state = {
isLoading: true,
2018-03-17 21:39:21 +01:00
};
EV(EV.enum.WALLETS_COUNT_CHANGED, this.refreshFunction.bind(this));
2018-01-30 23:42:38 +01:00
}
async componentDidMount() {
2018-03-17 21:39:21 +01:00
this.refreshFunction();
2018-01-30 23:42:38 +01:00
} // end of componendDidMount
2018-07-02 13:09:34 +02:00
/**
* Forcefully fetches TXs and balance for lastSnappedTo (i.e. current) wallet
*/
2018-06-25 00:22:46 +02:00
refreshTransactions() {
2018-03-17 21:39:21 +01:00
this.setState(
2018-06-25 00:22:46 +02:00
{
isTransactionsLoading: true,
},
async function() {
let that = this;
setTimeout(async function() {
// more responsive
let noErr = true;
try {
2018-06-28 03:43:28 +02:00
await BlueApp.fetchWalletBalances(that.lastSnappedTo || 0);
let start = +new Date();
await BlueApp.fetchWalletTransactions(that.lastSnappedTo || 0);
let end = +new Date();
console.log('tx took', (end - start) / 1000, 'sec');
2018-06-25 00:22:46 +02:00
} catch (err) {
noErr = false;
console.warn(err);
}
if (noErr) await BlueApp.saveToDisk(); // caching
that.refreshFunction();
}, 1);
},
);
}
2018-07-02 13:09:34 +02:00
/**
* Redraws the screen
*/
2018-06-25 00:22:46 +02:00
refreshFunction() {
2018-07-06 17:41:48 +02:00
if (BlueApp.getBalance() !== 0) {
A(A.ENUM.GOT_NONZERO_BALANCE);
}
2018-06-25 00:22:46 +02:00
setTimeout(() => {
console.log('refreshFunction()');
2018-07-07 13:30:50 +02:00
let showSend = false;
let showReceive = false;
let showManageFundsBig = false;
let showManageFundsSmallButton = false;
2018-07-07 13:30:50 +02:00
let wallets = BlueApp.getWallets();
let wallet = wallets[this.lastSnappedTo || 0];
if (wallet) {
showSend = wallet.allowSend();
showReceive = wallet.allowReceive();
}
let showRereshButton = (BlueApp.getWallets().length > 0 && true) || false;
if (wallet && wallet.type === new LightningCustodianWallet().type && !showSend) {
showManageFundsBig = true;
showManageFundsSmallButton = false;
showRereshButton = false;
}
if (wallet && wallet.type === new LightningCustodianWallet().type && wallet.getBalance() > 0) {
showRereshButton = false;
showManageFundsSmallButton = true;
}
2018-07-07 13:30:50 +02:00
2018-06-25 00:22:46 +02:00
this.setState({
isLoading: false,
isTransactionsLoading: false,
2018-07-07 13:30:50 +02:00
showReceiveButton: showReceive,
showSendButton: showSend,
showManageFundsBigButton: showManageFundsBig,
showManageFundsSmallButton,
showRereshButton,
2018-09-16 08:24:45 +02:00
dataSource: BlueApp.getTransactions(this.lastSnappedTo || 0),
2018-06-25 00:22:46 +02:00
});
}, 1);
}
txMemo(hash) {
if (BlueApp.tx_metadata[hash] && BlueApp.tx_metadata[hash]['memo']) {
2018-06-28 03:43:28 +02:00
return BlueApp.tx_metadata[hash]['memo'];
2018-06-25 00:22:46 +02:00
}
return '';
}
handleClick(index) {
console.log('cick', index);
let wallet = BlueApp.wallets[index];
if (wallet) {
this.props.navigation.navigate('WalletDetails', {
address: wallet.getAddress(), // either one of them will work
secret: wallet.getSecret(),
2018-06-25 00:22:46 +02:00
});
} else {
// if its out of index - this must be last card with incentive to create wallet
this.props.navigation.navigate('AddWallet');
}
}
onSnapToItem(index) {
console.log('onSnapToItem', index);
this.lastSnappedTo = index;
this.setState({
isLoading: false,
showReceiveButton: false,
showManageFundsBigButton: false,
showManageFundsSmallButton: false,
2018-06-25 00:22:46 +02:00
showSendButton: false,
2018-06-30 14:52:18 +02:00
showRereshButton: false,
2018-07-02 13:09:34 +02:00
// TODO: ŠæŠ¾Š³ŃƒŠ³Š»Šøть чŠµ этŠ¾ Š·Š° ŠµŠ±Š°Š»Š° ds.cloneWithRows, Š¼Š¾Š¶Š½Š¾ Š»Šø Š±Ń‹ŃŃ‚Ń€ŠµŠµ сŠ“ŠµŠ»Š°Ń‚ŃŒ ŠæрŠ¾Š³Ń€ŃƒŠ·Šŗу трŠ°Š½Š·Š°ŠŗцŠøŠ¹ Š½Š° эŠŗрŠ°Š½
2018-09-16 08:24:45 +02:00
dataSource: BlueApp.getTransactions(index),
2018-06-25 00:22:46 +02:00
});
if (index < BlueApp.getWallets().length) {
// do not show for last card
2018-07-07 13:30:50 +02:00
let showSend = false;
let showReceive = false;
let showManageFundsBig = false;
2018-07-07 13:30:50 +02:00
let wallets = BlueApp.getWallets();
let wallet = wallets[this.lastSnappedTo || 0];
if (wallet) {
showSend = wallet.allowSend();
showReceive = wallet.allowReceive();
}
console.log({ showSend });
let showRereshButton = true;
let showManageFundsSmallButton = true;
if (wallet && wallet.type === new LightningCustodianWallet().type && !showSend) {
showManageFundsBig = true;
showRereshButton = false;
showManageFundsSmallButton = false;
}
if (wallet && wallet.type === new LightningCustodianWallet().type) {
showRereshButton = false;
} else {
showManageFundsSmallButton = false;
}
console.log({ showManageFundsBig });
2018-07-07 13:30:50 +02:00
2018-06-25 00:22:46 +02:00
setTimeout(
2018-06-30 14:52:18 +02:00
() =>
this.setState({
2018-07-07 13:30:50 +02:00
showReceiveButton: showReceive,
showManageFundsBigButton: showManageFundsBig,
showManageFundsSmallButton,
2018-07-07 13:30:50 +02:00
showSendButton: showSend,
showRereshButton,
2018-06-30 14:52:18 +02:00
}),
2018-06-25 00:22:46 +02:00
50,
); // just to animate it, no real function
}
2018-07-02 15:51:24 +02:00
// now, lets try to fetch balance and txs for this wallet in case it has changed
this.lazyRefreshWallet(index);
}
isLightning() {
let w = BlueApp.getWallets()[this.lastSnappedTo || 0];
if (w && w.type === new LightningCustodianWallet().type) {
return true;
}
return false;
}
2018-07-02 15:51:24 +02:00
/**
* Decides whether wallet with such index shoud be refreshed,
* refreshes if yes and redraws the screen
* @param index {Integer} Index of the wallet.
* @return {Promise.<void>}
*/
async lazyRefreshWallet(index) {
/** @type {Array.<AbstractWallet>} wallets */
let wallets = BlueApp.getWallets();
if (!wallets[index]) {
return;
}
2018-07-02 15:51:24 +02:00
let oldBalance = wallets[index].getBalance();
let noErr = true;
2018-07-14 22:32:36 +02:00
let didRefresh = false;
2018-07-02 15:51:24 +02:00
try {
2018-07-14 22:32:36 +02:00
if (wallets && wallets[index] && wallets[index].timeToRefreshBalance()) {
2018-07-02 15:51:24 +02:00
console.log('snapped to, and now its time to refresh wallet #', index);
await wallets[index].fetchBalance();
2018-07-07 15:04:32 +02:00
if (oldBalance !== wallets[index].getBalance() || wallets[index].getUnconfirmedBalance() !== 0) {
console.log('balance changed, thus txs too');
2018-07-02 15:51:24 +02:00
// balance changed, thus txs too
await wallets[index].fetchTransactions();
this.refreshFunction();
2018-07-14 22:32:36 +02:00
didRefresh = true;
} else if (wallets[index].timeToRefreshTransaction()) {
console.log(wallets[index].getLabel(), 'thinks its time to refresh TXs');
2018-07-14 22:32:36 +02:00
await wallets[index].fetchTransactions();
if (wallets[index].fetchPendingTransactions) {
await wallets[index].fetchPendingTransactions();
}
2018-07-14 22:32:36 +02:00
this.refreshFunction();
didRefresh = true;
2018-07-03 22:11:02 +02:00
} else {
console.log('balance not changed');
2018-07-02 15:51:24 +02:00
}
}
} catch (Err) {
noErr = false;
console.warn(Err);
}
2018-07-14 22:32:36 +02:00
if (noErr && didRefresh) {
2018-07-02 15:51:24 +02:00
await BlueApp.saveToDisk(); // caching
}
2018-01-30 23:42:38 +01:00
}
render() {
2018-03-17 21:39:21 +01:00
const { navigate } = this.props.navigation;
2018-01-30 23:42:38 +01:00
if (this.state.isLoading) {
2018-03-17 21:39:21 +01:00
return <BlueLoading />;
2018-01-30 23:42:38 +01:00
}
return (
<SafeBlueArea>
2018-07-07 15:04:32 +02:00
<BlueHeaderDefaultMain leftText={loc.wallets.list.title} onClose={() => navigate('Settings')} />
2018-06-25 00:22:46 +02:00
<WalletsCarousel
data={BlueApp.getWallets().concat(false)}
handleClick={index => {
this.handleClick(index);
}}
onSnapToItem={index => {
this.onSnapToItem(index);
2018-03-17 21:39:21 +01:00
}}
2018-01-30 23:42:38 +01:00
/>
2018-06-25 00:22:46 +02:00
{(() => {
if (this.state.showManageFundsSmallButton) {
return (
<TouchableOpacity
style={{ alignSelf: 'flex-end', right: 10, flexDirection: 'row' }}
onPress={() => {
let walletIndex = this.lastSnappedTo || 0;
let c = 0;
for (let w of BlueApp.getWallets()) {
if (c++ === walletIndex) {
console.log('navigating to secret ', w.getSecret());
navigate('ManageFunds', { fromSecret: w.getSecret() });
}
}
}}
>
<BlueText style={{ fontWeight: '600', fontSize: 16 }}>Manage funds</BlueText>
<Icon
style={{ position: 'relative' }}
name="link"
type="font-awesome"
size={14}
color={BlueApp.settings.foregroundColor}
iconStyle={{ left: 5, transform: [{ rotate: '90deg' }] }}
/>
</TouchableOpacity>
);
}
})()}
2018-06-25 00:22:46 +02:00
{(() => {
if (this.state.isTransactionsLoading) {
return <BlueLoading />;
} else {
return (
<View style={{ flex: 1 }}>
2018-06-29 00:17:14 +02:00
<View style={{ flex: 1, flexDirection: 'row', height: 50 }}>
2018-06-25 00:22:46 +02:00
<Text
style={{
paddingLeft: 15,
paddingTop: 15,
fontWeight: 'bold',
fontSize: 24,
2018-05-22 19:10:53 +02:00
color: BlueApp.settings.foregroundColor,
2018-03-17 21:39:21 +01:00
}}
2018-06-25 00:22:46 +02:00
>
{loc.transactions.list.title}
</Text>
2018-06-30 14:52:18 +02:00
{(() => {
if (this.state.showRereshButton) {
2018-07-07 15:04:32 +02:00
return <BlueRefreshIcon onPress={() => this.refreshTransactions()} />;
2018-06-30 14:52:18 +02:00
}
})()}
</View>
2018-07-01 19:00:03 +02:00
<View
style={{
top: is.ipad() ? 60 : 120,
position: 'absolute',
width: width,
}}
>
2018-06-30 14:52:18 +02:00
{(() => {
2018-07-07 15:04:32 +02:00
if (BlueApp.getTransactions(this.lastSnappedTo || 0).length === 0) {
2018-06-30 14:52:18 +02:00
return (
<View>
<Text
style={{
fontSize: 18,
color: '#9aa0aa',
textAlign: 'center',
}}
>
{(this.isLightning() &&
'Lightning wallet should be used for your daily\ntransactions. Fees are unfairly cheap and\nspeed is blazing fast.') ||
loc.wallets.list.empty_txs1}
2018-06-30 14:52:18 +02:00
</Text>
<Text
style={{
fontSize: 18,
color: '#9aa0aa',
textAlign: 'center',
}}
>
{(this.isLightning() && '\nTo start using it tap on "manage funds"\nand topup your balance') ||
loc.wallets.list.empty_txs2}
2018-06-30 14:52:18 +02:00
</Text>
</View>
);
}
})()}
2018-06-25 00:22:46 +02:00
</View>
2018-06-29 00:17:14 +02:00
<View style={{ top: 30, position: 'absolute' }}>
<BlueList>
2018-09-16 08:24:45 +02:00
<FlatList
data={this.state.dataSource}
extraData={this.state.dataSource}
renderItem={rowData => {
2018-06-29 00:17:14 +02:00
return (
<BlueListItem
avatar={(() => {
if (rowData.category && rowData.category === 'receive') {
// is it lightning onchain tx?
if (rowData.confirmations < 3) {
return (
<View style={{ width: 25 }}>
<BlueTransactionPendingIcon />
</View>
);
} else {
return (
<View style={{ width: 25 }}>
<BlueTransactionOnchainIcon />
</View>
);
}
}
2018-09-05 00:18:24 +02:00
if (rowData.preimage) {
// is it lightning offchain payment?
return (
<View style={{ width: 25 }}>
<BlueTransactionOffchainIcon />
</View>
);
}
2018-06-29 00:17:14 +02:00
if (!rowData.confirmations) {
return (
<View style={{ width: 25 }}>
<BlueTransactionPendingIcon />
</View>
);
} else if (rowData.value < 0) {
return (
<View style={{ width: 25 }}>
<BlueTransactionOutgoingIcon />
</View>
);
} else {
return (
<View style={{ width: 25 }}>
<BlueTransactionIncommingIcon />
</View>
);
}
})()}
2018-07-07 15:04:32 +02:00
title={loc.transactionTimeToReadable(rowData.received)}
2018-06-29 00:17:14 +02:00
subtitle={
2018-07-12 01:38:24 +02:00
(rowData.confirmations < 7 ? loc.transactions.list.conf + ': ' + rowData.confirmations + ' ' : '') +
this.txMemo(rowData.hash) +
(rowData.memo || '')
2018-06-25 00:22:46 +02:00
}
2018-06-29 00:17:14 +02:00
onPress={() => {
if (rowData.hash) {
navigate('TransactionDetails', {
hash: rowData.hash,
});
}
2018-06-29 00:17:14 +02:00
}}
badge={{
value: 3,
textStyle: { color: 'orange' },
containerStyle: { marginTop: 0 },
}}
chevron={false}
chevronColor="transparent"
rightTitle={new BigNumber((rowData.value && rowData.value) || 0).div(100000000).toString()}
2018-06-29 00:17:14 +02:00
rightTitleStyle={{
position: 'relative',
right: -30,
top: -7,
fontWeight: '600',
fontSize: 16,
2018-07-07 15:04:32 +02:00
color: rowData.value / 100000000 < 0 ? BlueApp.settings.foregroundColor : '#37c0a1',
2018-06-29 00:17:14 +02:00
}}
/>
);
}}
/>
</BlueList>
</View>
2018-06-25 00:22:46 +02:00
</View>
);
}
})()}
{(() => {
if (this.state.showReceiveButton) {
return (
<BlueReceiveButtonIcon
onPress={() => {
let start = +new Date();
2018-06-25 00:22:46 +02:00
let walletIndex = this.lastSnappedTo || 0;
console.log('receiving on #', walletIndex);
let c = 0;
for (let w of BlueApp.getWallets()) {
if (c++ === walletIndex) {
console.log('found receiving address, secret=', w.getAddress(), ',', w.getSecret());
navigate('ReceiveDetails', { address: w.getAddress(), secret: w.getSecret() });
if (w.getAddress()) {
// EV(EV.enum.RECEIVE_ADDRESS_CHANGED, w.getAddress());
}
2018-06-25 00:22:46 +02:00
}
}
let end = +new Date();
console.log('took', (end - start) / 1000, 'sec');
2018-06-25 00:22:46 +02:00
}}
/>
);
}
})()}
{(() => {
2018-07-07 13:30:50 +02:00
if (this.state.showSendButton) {
2018-06-25 00:22:46 +02:00
return (
<BlueSendButtonIcon
onPress={() => {
let walletIndex = this.lastSnappedTo || 0;
let c = 0;
for (let w of BlueApp.getWallets()) {
if (c++ === walletIndex) {
if (w.type === new LightningCustodianWallet().type) {
navigate('ScanLndInvoice', { fromSecret: w.getSecret() });
} else {
navigate('SendDetails', { fromAddress: w.getAddress(), fromSecret: w.getSecret() });
}
}
}
}}
/>
);
}
})()}
{(() => {
if (this.state.showManageFundsBigButton) {
return (
<ManageFundsBigButton
onPress={() => {
let walletIndex = this.lastSnappedTo || 0;
let c = 0;
for (let w of BlueApp.getWallets()) {
if (c++ === walletIndex) {
console.log('navigating to secret ', w.getSecret());
navigate('ManageFunds', { fromSecret: w.getSecret() });
2018-06-25 00:22:46 +02:00
}
}
}}
/>
);
}
})()}
2018-01-30 23:42:38 +01:00
</SafeBlueArea>
);
}
2018-03-17 21:39:21 +01:00
}
2018-03-18 03:48:23 +01:00
WalletsList.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
}),
};