BlueWallet/screen/wallets/list.js

357 lines
11 KiB
JavaScript
Raw Normal View History

2018-01-30 23:42:38 +01:00
import React, { Component } from 'react';
2018-06-28 03:43:28 +02:00
import { View, Dimensions, Text, ListView } from 'react-native';
2018-01-30 23:42:38 +01:00
import {
2018-03-17 21:39:21 +01:00
BlueLoading,
SafeBlueArea,
2018-06-25 00:22:46 +02:00
WalletsCarousel,
BlueTransactionIncommingIcon,
BlueTransactionOutgoingIcon,
BlueTransactionPendingIcon,
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';
2018-03-18 03:48:23 +01:00
import PropTypes from 'prop-types';
2018-03-17 21:39:21 +01:00
let EV = require('../../events');
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-05-12 22:27:34 +02:00
const { height, width } = Dimensions.get('window');
2018-03-17 21:39:21 +01:00
let ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
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 {
await BlueApp.fetchWalletTransactions(that.lastSnappedTo || 0);
2018-06-28 03:43:28 +02:00
await BlueApp.fetchWalletBalances(that.lastSnappedTo || 0);
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() {
setTimeout(() => {
this.setState({
isLoading: false,
isTransactionsLoading: false,
showReceiveButton: true,
showSendButton: true,
2018-06-30 14:52:18 +02:00
showRereshButton: true,
2018-06-25 00:22:46 +02:00
final_balance: BlueApp.getBalance(),
dataSource: ds.cloneWithRows(
BlueApp.getTransactions(this.lastSnappedTo || 0),
),
});
}, 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(),
});
} 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,
showSendButton: false,
2018-06-30 14:52:18 +02:00
showRereshButton: false,
2018-06-25 00:22:46 +02:00
final_balance: BlueApp.getBalance(),
2018-07-02 13:09:34 +02:00
// TODO: погуглить че это за ебала ds.cloneWithRows, можно ли быстрее сделать прогрузку транзакций на экран
2018-06-25 00:22:46 +02:00
dataSource: ds.cloneWithRows(BlueApp.getTransactions(index)),
});
if (index < BlueApp.getWallets().length) {
// do not show for last card
setTimeout(
2018-06-30 14:52:18 +02:00
() =>
this.setState({
showReceiveButton: true,
showSendButton: true,
showRereshButton: true,
}),
2018-06-25 00:22:46 +02:00
50,
); // just to animate it, no real function
}
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-06-28 03:43:28 +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.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) {
return (
<BlueRefreshIcon
onPress={() => this.refreshTransactions()}
/>
);
}
})()}
</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
{(() => {
if (
BlueApp.getTransactions(this.lastSnappedTo || 0)
.length === 0
) {
return (
<View>
<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-06-25 00:22:46 +02:00
</View>
2018-06-29 00:17:14 +02:00
<View style={{ top: 30, position: 'absolute' }}>
<BlueList>
<ListView
maxHeight={height - 330 + 10}
width={width - 5}
left={5}
enableEmptySections
dataSource={this.state.dataSource}
renderRow={rowData => {
return (
<BlueListItem
avatar={(() => {
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>
);
}
})()}
title={loc.transactionTimeToReadable(
rowData.received,
)}
subtitle={
(rowData.confirmations < 200
? loc.transactions.list.conf +
': ' +
rowData.confirmations +
' '
: '') + this.txMemo(rowData.hash)
2018-06-25 00:22:46 +02:00
}
2018-06-29 00:17:14 +02:00
onPress={() => {
navigate('TransactionDetails', {
hash: rowData.hash,
});
}}
badge={{
value: 3,
textStyle: { color: 'orange' },
containerStyle: { marginTop: 0 },
}}
chevron={false}
chevronColor="transparent"
rightTitle={rowData.value / 100000000 + ''}
rightTitleStyle={{
position: 'relative',
right: -30,
top: -7,
fontWeight: '600',
fontSize: 16,
color:
rowData.value / 100000000 < 0
? BlueApp.settings.foregroundColor
: '#37c0a1',
}}
/>
);
}}
/>
</BlueList>
</View>
2018-06-25 00:22:46 +02:00
</View>
);
}
})()}
{(() => {
if (this.state.showReceiveButton) {
return (
<BlueReceiveButtonIcon
onPress={() => {
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 ', w.getAddress());
navigate('ReceiveDetails', { address: w.getAddress() });
EV(EV.enum.RECEIVE_ADDRESS_CHANGED, w.getAddress());
}
}
}}
/>
);
}
})()}
{(() => {
if (this.state.showReceiveButton) {
return (
<BlueSendButtonIcon
onPress={() => {
let walletIndex = this.lastSnappedTo || 0;
let c = 0;
for (let w of BlueApp.getWallets()) {
if (c++ === walletIndex) {
navigate('SendDetails', { fromAddress: w.getAddress() });
}
}
}}
/>
);
}
})()}
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,
}),
};