2018-01-30 23:42:38 +01:00
|
|
|
import React, { Component } from 'react';
|
2018-10-30 10:35:24 +01:00
|
|
|
import { View, TouchableOpacity, Text, FlatList, RefreshControl, ScrollView } from 'react-native';
|
2018-01-30 23:42:38 +01:00
|
|
|
import {
|
2018-09-01 01:28:19 +02:00
|
|
|
BlueTransactionOnchainIcon,
|
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
|
|
|
BlueList,
|
|
|
|
BlueListItem,
|
2018-06-28 03:43:28 +02:00
|
|
|
BlueHeaderDefaultMain,
|
2018-03-17 21:39:21 +01:00
|
|
|
} from '../../BlueComponents';
|
2018-09-01 01:28:19 +02:00
|
|
|
import { Icon } from 'react-native-elements';
|
2018-12-11 23:52:46 +01:00
|
|
|
import { NavigationEvents } from 'react-navigation';
|
2018-03-18 03:48:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-22 16:49:59 +02:00
|
|
|
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-01-30 23:42:38 +01:00
|
|
|
|
|
|
|
export default class WalletsList extends Component {
|
2018-09-30 11:56:32 +02:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
headerStyle: {
|
|
|
|
backgroundColor: '#FFFFFF',
|
|
|
|
borderBottomWidth: 0,
|
2018-12-11 23:52:46 +01:00
|
|
|
elevation: 0,
|
2018-09-19 03:59:16 +02:00
|
|
|
},
|
2018-09-30 11:56:32 +02:00
|
|
|
headerRight: (
|
2018-12-14 17:40:38 +01:00
|
|
|
<TouchableOpacity
|
2018-12-16 04:10:30 +01:00
|
|
|
style={{ marginHorizontal: 16, width: 40, height: 40, justifyContent: 'center', alignItems: 'flex-end' }}
|
2018-12-14 17:40:38 +01:00
|
|
|
onPress={() => navigation.navigate('Settings')}
|
|
|
|
>
|
2018-09-30 11:56:32 +02:00
|
|
|
<Icon name="kebab-horizontal" size={22} type="octicon" color={BlueApp.settings.foregroundColor} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
),
|
|
|
|
});
|
2018-01-30 23:42:38 +01:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
2018-12-11 23:52:46 +01:00
|
|
|
wallets: BlueApp.getWallets().concat(false),
|
2018-03-17 21:39:21 +01:00
|
|
|
};
|
|
|
|
EV(EV.enum.WALLETS_COUNT_CHANGED, this.refreshFunction.bind(this));
|
2018-10-09 06:25:36 +02:00
|
|
|
|
|
|
|
// here, when we receive TRANSACTIONS_COUNT_CHANGED we do not query
|
|
|
|
// remote server, we just redraw the screen
|
|
|
|
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED, this.refreshFunction.bind(this));
|
2018-01-30 23:42:38 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
componentDidMount() {
|
2018-03-17 21:39:21 +01:00
|
|
|
this.refreshFunction();
|
2018-10-09 06:25:36 +02:00
|
|
|
}
|
2018-01-30 23:42:38 +01:00
|
|
|
|
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-10-09 06:25:36 +02:00
|
|
|
if (!(this.lastSnappedTo < BlueApp.getWallets().length)) {
|
|
|
|
// last card, nop
|
|
|
|
console.log('last card, nop');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
2018-07-22 16:49:59 +02:00
|
|
|
let start = +new Date();
|
|
|
|
await BlueApp.fetchWalletTransactions(that.lastSnappedTo || 0);
|
|
|
|
let end = +new Date();
|
2018-10-09 06:25:36 +02:00
|
|
|
console.log('fetch 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-10-09 06:25:36 +02:00
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
isTransactionsLoading: false,
|
|
|
|
dataSource: BlueApp.getTransactions(),
|
2018-12-11 23:52:46 +01:00
|
|
|
wallets: BlueApp.getWallets().concat(false),
|
2018-10-09 06:25:36 +02:00
|
|
|
});
|
2018-06-25 00:22:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 '';
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
handleClick(index, gradients) {
|
2018-10-09 06:25:36 +02:00
|
|
|
console.log('click', index);
|
2018-06-25 00:22:46 +02:00
|
|
|
let wallet = BlueApp.wallets[index];
|
|
|
|
if (wallet) {
|
2018-10-09 06:25:36 +02:00
|
|
|
this.props.navigation.navigate('WalletTransactions', {
|
|
|
|
wallet: wallet,
|
2018-12-11 23:52:46 +01:00
|
|
|
gradients: gradients,
|
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;
|
|
|
|
|
|
|
|
if (index < BlueApp.getWallets().length) {
|
2018-10-09 06:25:36 +02:00
|
|
|
// not the last
|
2018-06-25 00:22:46 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2018-07-05 02:56:31 +02:00
|
|
|
if (!wallets[index]) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-09 06:25:36 +02:00
|
|
|
|
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) {
|
2018-07-05 02:56:31 +02:00
|
|
|
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()) {
|
2018-09-01 01:28:19 +02:00
|
|
|
console.log(wallets[index].getLabel(), 'thinks its time to refresh TXs');
|
2018-07-14 22:32:36 +02:00
|
|
|
await wallets[index].fetchTransactions();
|
2018-09-01 01:28:19 +02:00
|
|
|
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 {
|
2018-07-05 02:56:31 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
_keyExtractor = (_item, index) => index.toString();
|
2018-09-18 09:24:42 +02:00
|
|
|
|
2018-10-09 06:25:36 +02:00
|
|
|
renderListHeaderComponent = () => {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
paddingLeft: 15,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
fontSize: 24,
|
|
|
|
marginVertical: 8,
|
|
|
|
color: BlueApp.settings.foregroundColor,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{loc.transactions.list.title}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
handleLongPress = () => {
|
|
|
|
if (BlueApp.getWallets().length > 1) {
|
|
|
|
this.props.navigation.navigate('ReorderWallets');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 (
|
2018-10-09 06:25:36 +02:00
|
|
|
<SafeBlueArea style={{ flex: 1, backgroundColor: '#FFFFFF' }}>
|
2018-12-12 04:33:28 +01:00
|
|
|
<NavigationEvents
|
|
|
|
onWillFocus={() => {
|
|
|
|
this.refreshFunction();
|
|
|
|
}}
|
|
|
|
/>
|
2018-10-09 06:25:36 +02:00
|
|
|
<ScrollView
|
|
|
|
refreshControl={<RefreshControl onRefresh={() => this.refreshTransactions()} refreshing={this.state.isTransactionsLoading} />}
|
2018-10-01 05:12:42 +02:00
|
|
|
>
|
2018-10-09 06:25:36 +02:00
|
|
|
<BlueHeaderDefaultMain leftText={loc.wallets.list.title} onNewWalletPress={() => this.props.navigation.navigate('AddWallet')} />
|
|
|
|
<WalletsCarousel
|
2018-12-11 23:52:46 +01:00
|
|
|
data={this.state.wallets}
|
|
|
|
handleClick={(index, headerColor) => {
|
|
|
|
this.handleClick(index, headerColor);
|
2018-10-09 06:25:36 +02:00
|
|
|
}}
|
2018-12-11 23:52:46 +01:00
|
|
|
handleLongPress={this.handleLongPress}
|
2018-10-09 06:25:36 +02:00
|
|
|
onSnapToItem={index => {
|
|
|
|
this.onSnapToItem(index);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<BlueList>
|
|
|
|
<FlatList
|
|
|
|
ListHeaderComponent={this.renderListHeaderComponent}
|
2018-10-10 21:36:32 +02:00
|
|
|
ListEmptyComponent={
|
|
|
|
<View style={{ top: 50, height: 100 }}>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
fontSize: 18,
|
|
|
|
color: '#9aa0aa',
|
|
|
|
textAlign: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{loc.wallets.list.empty_txs1}
|
|
|
|
</Text>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
fontSize: 18,
|
|
|
|
color: '#9aa0aa',
|
|
|
|
textAlign: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{loc.wallets.list.empty_txs2}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
}
|
2018-10-09 06:25:36 +02:00
|
|
|
data={this.state.dataSource}
|
|
|
|
extraData={this.state.dataSource}
|
|
|
|
keyExtractor={this._keyExtractor}
|
|
|
|
renderItem={rowData => {
|
|
|
|
return (
|
|
|
|
<BlueListItem
|
|
|
|
avatar={(() => {
|
|
|
|
// is it lightning refill tx?
|
|
|
|
if (rowData.item.category === 'receive' && rowData.item.confirmations < 3) {
|
|
|
|
return (
|
|
|
|
<View style={{ width: 25 }}>
|
|
|
|
<BlueTransactionPendingIcon />
|
|
|
|
</View>
|
|
|
|
);
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
2018-09-29 00:17:26 +02:00
|
|
|
|
2018-10-09 06:25:36 +02:00
|
|
|
if (rowData.item.type && rowData.item.type === 'bitcoind_tx') {
|
|
|
|
return (
|
|
|
|
<View style={{ width: 25 }}>
|
|
|
|
<BlueTransactionOnchainIcon />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (rowData.item.type === 'paid_invoice') {
|
|
|
|
// is it lightning offchain payment?
|
|
|
|
return (
|
|
|
|
<View style={{ width: 25 }}>
|
|
|
|
<BlueTransactionOffchainIcon />
|
|
|
|
</View>
|
|
|
|
);
|
2018-09-29 00:17:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 06:25:36 +02:00
|
|
|
if (!rowData.item.confirmations) {
|
|
|
|
return (
|
|
|
|
<View style={{ width: 25 }}>
|
|
|
|
<BlueTransactionPendingIcon />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else if (rowData.item.value < 0) {
|
|
|
|
return (
|
|
|
|
<View style={{ width: 25 }}>
|
|
|
|
<BlueTransactionOutgoingIcon />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<View style={{ width: 25 }}>
|
|
|
|
<BlueTransactionIncommingIcon />
|
|
|
|
</View>
|
|
|
|
);
|
2018-09-29 00:17:26 +02:00
|
|
|
}
|
2018-10-09 06:25:36 +02:00
|
|
|
})()}
|
|
|
|
title={loc.transactionTimeToReadable(rowData.item.received)}
|
|
|
|
subtitle={
|
|
|
|
(rowData.item.confirmations < 7 ? loc.transactions.list.conf + ': ' + rowData.item.confirmations + ' ' : '') +
|
|
|
|
this.txMemo(rowData.item.hash) +
|
|
|
|
(rowData.item.memo || '')
|
2018-09-29 00:17:26 +02:00
|
|
|
}
|
2018-10-09 06:25:36 +02:00
|
|
|
onPress={() => {
|
|
|
|
if (rowData.item.hash) {
|
|
|
|
navigate('TransactionDetails', {
|
|
|
|
hash: rowData.item.hash,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
badge={{
|
|
|
|
value: 3,
|
|
|
|
textStyle: { color: 'orange' },
|
|
|
|
containerStyle: { marginTop: 0 },
|
|
|
|
}}
|
|
|
|
hideChevron
|
2018-10-20 23:10:21 +02:00
|
|
|
rightTitle={new BigNumber((rowData.item.value && rowData.item.value) || 0).dividedBy(100000000).toString()}
|
2018-10-09 06:25:36 +02:00
|
|
|
rightTitleStyle={{
|
|
|
|
fontWeight: '600',
|
|
|
|
fontSize: 16,
|
|
|
|
color: rowData.item.value / 100000000 < 0 ? BlueApp.settings.foregroundColor : '#37c0a1',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</BlueList>
|
|
|
|
</ScrollView>
|
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,
|
|
|
|
}),
|
|
|
|
};
|