mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2024-11-20 10:12:01 +01:00
290 lines
8.7 KiB
JavaScript
290 lines
8.7 KiB
JavaScript
import React, { Component } from 'react';
|
|
import { View, TouchableOpacity, Text, FlatList, RefreshControl, ScrollView } from 'react-native';
|
|
import { BlueLoading, SafeBlueArea, WalletsCarousel, BlueList, BlueHeaderDefaultMain, BlueListTransactionItem } from '../../BlueComponents';
|
|
import { Icon } from 'react-native-elements';
|
|
import { NavigationEvents } from 'react-navigation';
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
|
import PropTypes from 'prop-types';
|
|
import WalletGradient from '../../class/walletGradient';
|
|
let EV = require('../../events');
|
|
let A = require('../../analytics');
|
|
/** @type {AppStorage} */
|
|
let BlueApp = require('../../BlueApp');
|
|
let loc = require('../../loc');
|
|
|
|
export default class WalletsList extends Component {
|
|
static navigationOptions = ({ navigation }) => ({
|
|
headerStyle: {
|
|
backgroundColor: '#FFFFFF',
|
|
borderBottomWidth: 0,
|
|
elevation: 0,
|
|
},
|
|
headerRight: (
|
|
<TouchableOpacity
|
|
style={{ marginHorizontal: 16, width: 40, height: 40, justifyContent: 'center', alignItems: 'flex-end' }}
|
|
onPress={() => navigation.navigate('Settings')}
|
|
>
|
|
<Icon name="kebab-horizontal" size={22} type="octicon" color={BlueApp.settings.foregroundColor} />
|
|
</TouchableOpacity>
|
|
),
|
|
});
|
|
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = {
|
|
isLoading: true,
|
|
wallets: BlueApp.getWallets().concat(false),
|
|
lastSnappedTo: 0,
|
|
};
|
|
EV(EV.enum.WALLETS_COUNT_CHANGED, this.refreshFunction.bind(this));
|
|
|
|
// 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));
|
|
}
|
|
|
|
componentDidMount() {
|
|
this.refreshFunction();
|
|
}
|
|
|
|
/**
|
|
* Forcefully fetches TXs and balance for lastSnappedTo (i.e. current) wallet.
|
|
* Triggered manually by user on pull-to-refresh.
|
|
*/
|
|
refreshTransactions() {
|
|
if (!(this.lastSnappedTo < BlueApp.getWallets().length)) {
|
|
// last card, nop
|
|
console.log('last card, nop');
|
|
return;
|
|
}
|
|
|
|
this.setState(
|
|
{
|
|
isTransactionsLoading: true,
|
|
},
|
|
async function() {
|
|
let that = this;
|
|
setTimeout(async function() {
|
|
// more responsive
|
|
let noErr = true;
|
|
try {
|
|
let balanceStart = +new Date();
|
|
await BlueApp.fetchWalletBalances(that.lastSnappedTo || 0);
|
|
let balanceEnd = +new Date();
|
|
console.log('fetch balance took', (balanceEnd - balanceStart) / 1000, 'sec');
|
|
let start = +new Date();
|
|
await BlueApp.fetchWalletTransactions(that.lastSnappedTo || 0);
|
|
let end = +new Date();
|
|
console.log('fetch tx took', (end - start) / 1000, 'sec');
|
|
} catch (err) {
|
|
noErr = false;
|
|
console.warn(err);
|
|
}
|
|
if (noErr) await BlueApp.saveToDisk(); // caching
|
|
|
|
that.refreshFunction();
|
|
}, 1);
|
|
},
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Redraws the screen
|
|
*/
|
|
refreshFunction() {
|
|
if (BlueApp.getBalance() !== 0) {
|
|
A(A.ENUM.GOT_NONZERO_BALANCE);
|
|
}
|
|
|
|
this.setState({
|
|
isLoading: false,
|
|
isTransactionsLoading: false,
|
|
dataSource: BlueApp.getTransactions(),
|
|
wallets: BlueApp.getWallets().concat(false),
|
|
});
|
|
}
|
|
|
|
txMemo(hash) {
|
|
if (BlueApp.tx_metadata[hash] && BlueApp.tx_metadata[hash]['memo']) {
|
|
return BlueApp.tx_metadata[hash]['memo'];
|
|
}
|
|
return '';
|
|
}
|
|
|
|
handleClick(index) {
|
|
console.log('click', index);
|
|
let wallet = BlueApp.wallets[index];
|
|
if (wallet) {
|
|
this.props.navigation.navigate('WalletTransactions', {
|
|
wallet: wallet,
|
|
headerColor: WalletGradient.headerColorFor(wallet.type),
|
|
});
|
|
} 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({ lastSnappedTo: index });
|
|
|
|
if (index < BlueApp.getWallets().length) {
|
|
// not the last
|
|
}
|
|
|
|
// 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();
|
|
if (!wallets[index]) {
|
|
return;
|
|
}
|
|
|
|
let oldBalance = wallets[index].getBalance();
|
|
let noErr = true;
|
|
let didRefresh = false;
|
|
|
|
try {
|
|
if (wallets && wallets[index] && wallets[index].timeToRefreshBalance()) {
|
|
console.log('snapped to, and now its time to refresh wallet #', index);
|
|
await wallets[index].fetchBalance();
|
|
if (oldBalance !== wallets[index].getBalance() || wallets[index].getUnconfirmedBalance() !== 0) {
|
|
console.log('balance changed, thus txs too');
|
|
// balance changed, thus txs too
|
|
await wallets[index].fetchTransactions();
|
|
this.refreshFunction();
|
|
didRefresh = true;
|
|
} else if (wallets[index].timeToRefreshTransaction()) {
|
|
console.log(wallets[index].getLabel(), 'thinks its time to refresh TXs');
|
|
await wallets[index].fetchTransactions();
|
|
if (wallets[index].fetchPendingTransactions) {
|
|
await wallets[index].fetchPendingTransactions();
|
|
}
|
|
if (wallets[index].fetchUserInvoices) {
|
|
await wallets[index].fetchUserInvoices();
|
|
}
|
|
this.refreshFunction();
|
|
didRefresh = true;
|
|
} else {
|
|
console.log('balance not changed');
|
|
}
|
|
}
|
|
} catch (Err) {
|
|
noErr = false;
|
|
console.warn(Err);
|
|
}
|
|
|
|
if (noErr && didRefresh) {
|
|
await BlueApp.saveToDisk(); // caching
|
|
}
|
|
}
|
|
|
|
_keyExtractor = (_item, index) => index.toString();
|
|
|
|
renderListHeaderComponent = () => {
|
|
return (
|
|
<View>
|
|
<Text
|
|
style={{
|
|
paddingLeft: 15,
|
|
fontWeight: 'bold',
|
|
fontSize: 24,
|
|
marginVertical: 8,
|
|
color: BlueApp.settings.foregroundColor,
|
|
}}
|
|
>
|
|
{loc.transactions.list.title}
|
|
</Text>
|
|
</View>
|
|
);
|
|
};
|
|
|
|
handleLongPress = () => {
|
|
if (BlueApp.getWallets().length > 1) {
|
|
this.props.navigation.navigate('ReorderWallets');
|
|
} else {
|
|
ReactNativeHapticFeedback.trigger('notificationError', false);
|
|
}
|
|
};
|
|
|
|
_renderItem = data => <BlueListTransactionItem item={data.item} itemPriceUnit={data.item.walletPreferredBalanceUnit} />;
|
|
|
|
render() {
|
|
if (this.state.isLoading) {
|
|
return <BlueLoading />;
|
|
}
|
|
return (
|
|
<SafeBlueArea style={{ flex: 1, backgroundColor: '#FFFFFF' }}>
|
|
<NavigationEvents
|
|
onWillFocus={() => {
|
|
this.refreshFunction();
|
|
}}
|
|
/>
|
|
<ScrollView
|
|
refreshControl={<RefreshControl onRefresh={() => this.refreshTransactions()} refreshing={this.state.isTransactionsLoading} />}
|
|
>
|
|
<BlueHeaderDefaultMain leftText={loc.wallets.list.title} onNewWalletPress={() => this.props.navigation.navigate('AddWallet')} />
|
|
<WalletsCarousel
|
|
data={this.state.wallets}
|
|
handleClick={index => {
|
|
this.handleClick(index);
|
|
}}
|
|
handleLongPress={this.handleLongPress}
|
|
onSnapToItem={index => {
|
|
this.onSnapToItem(index);
|
|
}}
|
|
/>
|
|
<BlueList>
|
|
<FlatList
|
|
ListHeaderComponent={this.renderListHeaderComponent}
|
|
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>
|
|
}
|
|
data={this.state.dataSource}
|
|
extraData={this.state.dataSource}
|
|
keyExtractor={this._keyExtractor}
|
|
renderItem={this._renderItem}
|
|
/>
|
|
</BlueList>
|
|
</ScrollView>
|
|
</SafeBlueArea>
|
|
);
|
|
}
|
|
}
|
|
|
|
WalletsList.propTypes = {
|
|
navigation: PropTypes.shape({
|
|
navigate: PropTypes.func,
|
|
}),
|
|
};
|