BlueWallet/screen/wallets/selectWallet.js

180 lines
5.6 KiB
JavaScript
Raw Normal View History

2020-01-02 06:32:35 +01:00
/* eslint-disable react/prop-types */
import React, { useContext, useEffect, useState } from 'react';
import { View, ActivityIndicator, Image, Text, TouchableOpacity, FlatList, StyleSheet, StatusBar } from 'react-native';
2018-12-24 01:44:31 +01:00
import LinearGradient from 'react-native-linear-gradient';
2020-12-04 14:39:47 +01:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import { useRoute, useTheme } from '@react-navigation/native';
2020-12-25 17:09:53 +01:00
import { SafeBlueArea, BlueText, BlueSpacing20, BluePrivateBalance } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
import { LightningCustodianWallet } from '../../class/wallets/lightning-custodian-wallet';
import WalletGradient from '../../class/wallet-gradient';
2020-07-20 15:38:46 +02:00
import loc, { formatBalance, transactionTimeToReadable } from '../../loc';
2020-10-05 23:25:14 +02:00
import { MultisigHDWallet } from '../../class';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2018-12-24 01:44:31 +01:00
2021-01-27 01:04:11 +01:00
const SelectWallet = () => {
const { chainType, onWalletSelect, availableWallets } = useRoute().params;
2020-01-02 06:32:35 +01:00
const [isLoading, setIsLoading] = useState(true);
const { wallets } = useContext(BlueStorageContext);
2020-07-15 19:32:59 +02:00
const { colors } = useTheme();
let data = chainType
? wallets.filter(item => item.chain === chainType && item.allowSend())
: wallets.filter(item => item.allowSend()) || [];
if (availableWallets && availableWallets.length > 0) {
// availableWallets if provided, overrides chainType argument and `allowSend()` check
data = availableWallets;
}
2020-07-15 19:32:59 +02:00
const styles = StyleSheet.create({
root: {
flex: 1,
backgroundColor: colors.background,
},
loading: {
flex: 1,
justifyContent: 'center',
alignContent: 'center',
paddingTop: 20,
backgroundColor: colors.background,
2020-07-15 19:32:59 +02:00
},
itemRoot: {
backgroundColor: 'transparent',
padding: 10,
marginVertical: 17,
},
gradient: {
padding: 15,
borderRadius: 10,
minHeight: 164,
elevation: 5,
},
image: {
width: 99,
height: 94,
position: 'absolute',
bottom: 0,
right: 0,
},
transparentText: {
backgroundColor: 'transparent',
},
label: {
backgroundColor: 'transparent',
fontSize: 19,
color: '#fff',
},
balance: {
backgroundColor: 'transparent',
fontWeight: 'bold',
fontSize: 36,
color: '#fff',
},
latestTxLabel: {
backgroundColor: 'transparent',
fontSize: 13,
color: '#fff',
},
latestTxValue: {
backgroundColor: 'transparent',
fontWeight: 'bold',
fontSize: 16,
color: '#fff',
},
noWallets: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: 20,
},
center: {
textAlign: 'center',
},
});
2020-01-02 06:32:35 +01:00
useEffect(() => {
setIsLoading(false);
2020-03-18 12:56:59 +01:00
}, []);
2018-12-24 01:44:31 +01:00
2020-01-02 06:32:35 +01:00
const renderItem = ({ item }) => {
2018-12-24 01:44:31 +01:00
return (
<TouchableOpacity
onPress={() => {
2019-05-03 14:36:11 +02:00
ReactNativeHapticFeedback.trigger('selection', { ignoreAndroidSystemSettings: false });
2021-01-27 01:04:11 +01:00
onWalletSelect(item);
2018-12-24 01:44:31 +01:00
}}
>
<View shadowOpacity={40 / 100} shadowOffset={{ width: 0, height: 0 }} shadowRadius={5} style={styles.itemRoot}>
<LinearGradient shadowColor="#000000" colors={WalletGradient.gradientsFor(item.type)} style={styles.gradient}>
2018-12-24 01:44:31 +01:00
<Image
2020-10-05 23:25:14 +02:00
source={(() => {
switch (item.type) {
case LightningCustodianWallet.type:
return require('../../img/lnd-shape.png');
case MultisigHDWallet.type:
return require('../../img/vault-shape.png');
default:
return require('../../img/btc-shape.png');
}
})()}
style={styles.image}
2018-12-24 01:44:31 +01:00
/>
<Text style={styles.transparentText} />
<Text numberOfLines={1} style={styles.label}>
2018-12-24 01:44:31 +01:00
{item.getLabel()}
</Text>
2019-08-03 23:29:15 +02:00
{item.hideBalance ? (
<BluePrivateBalance />
) : (
<Text numberOfLines={1} adjustsFontSizeToFit style={styles.balance}>
2020-07-20 15:38:46 +02:00
{formatBalance(Number(item.getBalance()), item.getPreferredBalanceUnit(), true)}
2019-08-03 23:29:15 +02:00
</Text>
)}
<Text style={styles.transparentText} />
<Text numberOfLines={1} style={styles.latestTxLabel}>
2020-07-20 15:38:46 +02:00
{loc.wallets.list_latest_transaction}
2018-12-24 01:44:31 +01:00
</Text>
<Text numberOfLines={1} style={styles.latestTxValue}>
2020-07-20 15:38:46 +02:00
{transactionTimeToReadable(item.getLatestTransactionTime())}
2018-12-24 01:44:31 +01:00
</Text>
</LinearGradient>
</View>
</TouchableOpacity>
);
};
2020-01-02 06:32:35 +01:00
if (isLoading) {
return (
<View style={styles.loading}>
2020-07-15 19:32:59 +02:00
<StatusBar barStyle="light-content" />
2020-01-02 06:32:35 +01:00
<ActivityIndicator />
</View>
);
} else if (data.length <= 0) {
return (
<SafeBlueArea style={styles.root}>
2020-07-15 19:32:59 +02:00
<StatusBar barStyle="light-content" />
<View style={styles.noWallets}>
2020-07-20 15:38:46 +02:00
<BlueText style={styles.center}>{loc.wallets.select_no_bitcoin}</BlueText>
2020-01-02 06:32:35 +01:00
<BlueSpacing20 />
2020-07-20 15:38:46 +02:00
<BlueText style={styles.center}>{loc.wallets.select_no_bitcoin_exp}</BlueText>
2018-12-24 01:44:31 +01:00
</View>
2020-01-02 06:32:35 +01:00
</SafeBlueArea>
);
} else {
2018-12-24 01:44:31 +01:00
return (
<SafeBlueArea style={styles.root}>
2020-07-15 19:32:59 +02:00
<StatusBar barStyle="default" />
2020-01-02 06:32:35 +01:00
<FlatList extraData={data} data={data} renderItem={renderItem} keyExtractor={(_item, index) => `${index}`} />
2018-12-24 01:44:31 +01:00
</SafeBlueArea>
);
}
};
2020-01-02 06:32:35 +01:00
2020-12-25 17:09:53 +01:00
SelectWallet.navigationOptions = navigationStyle({
title: loc.wallets.select_wallet,
2020-01-02 06:32:35 +01:00
});
export default SelectWallet;