2020-10-24 19:20:59 +02:00
|
|
|
import React, { useContext, useEffect, useRef, useState } from 'react';
|
2021-06-16 08:05:04 +02:00
|
|
|
import { StatusBar, View, StyleSheet, Alert } from 'react-native';
|
2020-09-08 18:06:41 +02:00
|
|
|
import { DrawerContentScrollView } from '@react-navigation/drawer';
|
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-12-25 17:09:53 +01:00
|
|
|
import { useTheme } from '@react-navigation/native';
|
|
|
|
|
2021-06-16 08:05:04 +02:00
|
|
|
import { BlueHeaderDefaultMain, BlueSpacing20 } from '../../BlueComponents';
|
2020-12-25 17:09:53 +01:00
|
|
|
import WalletsCarousel from '../../components/WalletsCarousel';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { PlaceholderWallet } from '../../class';
|
2020-09-08 18:06:41 +02:00
|
|
|
import WalletImport from '../../class/wallet-import';
|
|
|
|
import loc from '../../loc';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2021-01-31 06:02:48 +01:00
|
|
|
import { BlurView } from '@react-native-community/blur';
|
2020-09-08 18:06:41 +02:00
|
|
|
|
|
|
|
const DrawerList = props => {
|
2020-10-06 12:28:06 +02:00
|
|
|
console.log('drawerList rendering...');
|
2020-09-08 18:06:41 +02:00
|
|
|
const walletsCarousel = useRef();
|
2021-02-08 04:34:19 +01:00
|
|
|
const { wallets, selectedWallet, pendingWallets, isDrawerListBlurred } = useContext(BlueStorageContext);
|
2020-10-24 19:20:59 +02:00
|
|
|
const [carouselData, setCarouselData] = useState([]);
|
2020-09-08 18:06:41 +02:00
|
|
|
const { colors } = useTheme();
|
2021-02-08 04:34:19 +01:00
|
|
|
const walletsCount = useRef(wallets.length);
|
2020-09-08 18:06:41 +02:00
|
|
|
const stylesHook = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
backgroundColor: colors.brandingColor,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const allWallets = wallets.concat(pendingWallets);
|
|
|
|
setCarouselData(allWallets.concat(false));
|
|
|
|
const newCarouselData = allWallets.concat(false);
|
|
|
|
setCarouselData(newCarouselData);
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [wallets, pendingWallets]);
|
|
|
|
|
2021-02-08 04:54:04 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (walletsCount.current < wallets.length) {
|
2021-06-16 08:05:04 +02:00
|
|
|
walletsCarousel.current?.scrollToItem({ item: wallets[walletsCount.current] });
|
2021-02-08 04:54:04 +01:00
|
|
|
}
|
|
|
|
walletsCount.current = wallets.length;
|
|
|
|
}, [wallets]);
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
useEffect(() => {
|
2021-02-08 04:34:19 +01:00
|
|
|
if (pendingWallets.length > 0) {
|
2021-06-16 08:05:04 +02:00
|
|
|
walletsCarousel.current?.scrollToItem(carouselData.length - pendingWallets.length);
|
2020-10-24 19:20:59 +02:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2021-02-08 04:34:19 +01:00
|
|
|
}, [pendingWallets]);
|
2020-10-24 19:20:59 +02:00
|
|
|
|
2020-09-08 18:06:41 +02:00
|
|
|
const handleClick = index => {
|
|
|
|
console.log('click', index);
|
2020-10-24 19:20:59 +02:00
|
|
|
const wallet = carouselData[index];
|
2020-09-08 18:06:41 +02:00
|
|
|
if (wallet) {
|
|
|
|
if (wallet.type === PlaceholderWallet.type) {
|
|
|
|
Alert.alert(
|
|
|
|
loc.wallets.add_details,
|
|
|
|
loc.wallets.list_import_problem,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: loc.wallets.details_delete,
|
|
|
|
onPress: () => {
|
|
|
|
WalletImport.removePlaceholderWallet();
|
|
|
|
},
|
|
|
|
style: 'destructive',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: loc.wallets.list_tryagain,
|
|
|
|
onPress: () => {
|
|
|
|
props.navigation.navigate('AddWalletRoot', { screen: 'ImportWallet', params: { label: wallet.getSecret() } });
|
|
|
|
WalletImport.removePlaceholderWallet();
|
|
|
|
},
|
|
|
|
style: 'default',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ cancelable: false },
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
props.navigation.navigate('WalletTransactions', {
|
2020-10-24 19:20:59 +02:00
|
|
|
walletID: wallet.getID(),
|
|
|
|
walletType: wallet.type,
|
2020-09-08 18:06:41 +02:00
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if its out of index - this must be last card with incentive to create wallet
|
2020-10-24 19:20:59 +02:00
|
|
|
if (!carouselData.some(wallet => wallet.type === PlaceholderWallet.type)) {
|
2020-09-08 18:06:41 +02:00
|
|
|
props.navigation.navigate('Navigation', { screen: 'AddWalletRoot' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLongPress = () => {
|
2020-10-24 19:20:59 +02:00
|
|
|
if (carouselData.length > 1 && !carouselData.some(wallet => wallet.type === PlaceholderWallet.type)) {
|
2020-09-08 18:06:41 +02:00
|
|
|
props.navigation.navigate('ReorderWallets');
|
|
|
|
} else {
|
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-16 08:05:04 +02:00
|
|
|
const onNewWalletPress = () => {
|
|
|
|
return !carouselData.some(wallet => wallet.type === PlaceholderWallet.type) ? props.navigation.navigate('AddWalletRoot') : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ListHeaderComponent = () => {
|
2020-09-08 18:06:41 +02:00
|
|
|
return (
|
2021-06-16 08:05:04 +02:00
|
|
|
<>
|
|
|
|
<BlueHeaderDefaultMain leftText={loc.wallets.list_title} onNewWalletPress={onNewWalletPress} isDrawerList />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
</>
|
2020-09-08 18:06:41 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-06-16 08:05:04 +02:00
|
|
|
const renderWalletsCarousel = (
|
|
|
|
<WalletsCarousel
|
|
|
|
removeClippedSubviews={false}
|
|
|
|
data={carouselData}
|
|
|
|
onPress={handleClick}
|
|
|
|
handleLongPress={handleLongPress}
|
|
|
|
ref={walletsCarousel}
|
|
|
|
testID="WalletsList"
|
|
|
|
selectedWallet={selectedWallet}
|
|
|
|
ListHeaderComponent={ListHeaderComponent}
|
|
|
|
/>
|
|
|
|
);
|
2020-10-08 22:03:57 +02:00
|
|
|
|
2020-09-08 18:06:41 +02:00
|
|
|
return (
|
2021-06-16 08:05:04 +02:00
|
|
|
<DrawerContentScrollView {...props}>
|
2020-09-08 18:06:41 +02:00
|
|
|
<View styles={[styles.root, stylesHook.root]}>
|
|
|
|
<StatusBar barStyle="default" />
|
2021-06-16 08:05:04 +02:00
|
|
|
{renderWalletsCarousel}
|
2020-09-08 18:06:41 +02:00
|
|
|
</View>
|
2021-01-31 06:02:48 +01:00
|
|
|
{isDrawerListBlurred && (
|
|
|
|
<BlurView style={styles.absolute} blurType="light" blurAmount={10} reducedTransparencyFallbackColor="white" />
|
|
|
|
)}
|
2020-09-08 18:06:41 +02:00
|
|
|
</DrawerContentScrollView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DrawerList;
|
2020-12-25 17:09:53 +01:00
|
|
|
|
2020-09-08 18:06:41 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
2021-06-16 08:05:04 +02:00
|
|
|
|
2021-01-31 06:02:48 +01:00
|
|
|
absolute: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
},
|
2020-09-08 18:06:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
DrawerList.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
|
|
|
navigate: PropTypes.func,
|
|
|
|
addListener: PropTypes.func,
|
|
|
|
}),
|
|
|
|
route: PropTypes.shape({
|
|
|
|
name: PropTypes.string,
|
|
|
|
params: PropTypes.object,
|
|
|
|
}),
|
|
|
|
};
|