BlueWallet/screen/wallets/reorderWallets.js

207 lines
6.3 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState, useRef, useContext } from 'react';
2021-09-13 13:43:26 -04:00
import { View, ActivityIndicator, Image, Text, StyleSheet, StatusBar, I18nManager, TouchableOpacity } from 'react-native';
import { BluePrivateBalance } from '../../BlueComponents';
import SortableList from 'react-native-sortable-list';
import LinearGradient from 'react-native-linear-gradient';
2020-12-04 13:39:47 +00:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2021-07-04 00:21:31 -04:00
import { useNavigation, useTheme } from '@react-navigation/native';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
import { LightningCustodianWallet, LightningLdkWallet, MultisigHDWallet } from '../../class';
import WalletGradient from '../../class/wallet-gradient';
2020-07-20 16:38:46 +03:00
import loc, { formatBalance, transactionTimeToReadable } from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
const styles = StyleSheet.create({
loading: {
flex: 1,
paddingTop: 20,
},
root: {
flex: 1,
},
itemRoot: {
backgroundColor: 'transparent',
padding: 10,
},
gradient: {
padding: 15,
borderRadius: 10,
minHeight: 164,
elevation: 5,
},
image: {
width: 99,
height: 94,
position: 'absolute',
bottom: 0,
2021-03-27 06:47:01 -04:00
right: 0,
},
transparentText: {
backgroundColor: 'transparent',
},
label: {
backgroundColor: 'transparent',
fontSize: 19,
color: '#fff',
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
},
balance: {
backgroundColor: 'transparent',
fontWeight: 'bold',
fontSize: 36,
color: '#fff',
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
},
latestTxLabel: {
backgroundColor: 'transparent',
fontSize: 13,
color: '#fff',
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
},
latestTxValue: {
backgroundColor: 'transparent',
fontWeight: 'bold',
fontSize: 16,
color: '#fff',
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
},
});
2020-08-07 22:56:49 -04:00
const ReorderWallets = () => {
const [isLoading, setIsLoading] = useState(true);
const [data, setData] = useState([]);
const [hasMovedARow, setHasMovedARow] = useState(false);
const sortableList = useRef();
2021-09-13 13:43:26 -04:00
const { colors, closeImage } = useTheme();
const { wallets, setWalletsWithNewOrder } = useContext(BlueStorageContext);
2021-07-04 00:21:31 -04:00
const navigation = useNavigation();
2020-08-07 22:56:49 -04:00
const stylesHook = {
root: {
backgroundColor: colors.elevated,
},
loading: {
backgroundColor: colors.elevated,
},
};
2020-06-09 15:08:18 +01:00
2020-08-07 22:56:49 -04:00
useEffect(() => {
2021-09-13 13:43:26 -04:00
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
accessibilityRole="button"
onPress={() => {
if (sortableList.current?.state.data.length === data.length && hasMovedARow) {
const newWalletsOrderArray = [];
sortableList.current.state.order.forEach(element => {
newWalletsOrderArray.push(data[element]);
});
setWalletsWithNewOrder(newWalletsOrderArray);
}
navigation.goBack();
}}
testID="NavigationCloseButton"
>
<Image source={closeImage} />
</TouchableOpacity>
),
2021-07-04 00:21:31 -04:00
});
2020-08-07 22:56:49 -04:00
// eslint-disable-next-line react-hooks/exhaustive-deps
2021-07-04 00:21:31 -04:00
}, [navigation, hasMovedARow]);
2020-08-07 22:56:49 -04:00
useEffect(() => {
setData(wallets);
2020-08-07 22:56:49 -04:00
setIsLoading(false);
}, [wallets]);
2020-08-07 22:56:49 -04:00
const renderItem = (item, _active) => {
if (!item.data) {
return;
}
item = item.data;
return (
<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}>
<Image
2020-10-05 22:25:14 +01:00
source={(() => {
switch (item.type) {
2021-09-09 12:00:11 +01:00
case LightningLdkWallet.type:
2020-10-05 22:25:14 +01:00
case LightningCustodianWallet.type:
2021-03-27 06:34:26 -04:00
return I18nManager.isRTL ? require('../../img/lnd-shape-rtl.png') : require('../../img/lnd-shape.png');
2020-10-05 22:25:14 +01:00
case MultisigHDWallet.type:
2021-03-27 06:34:26 -04:00
return I18nManager.isRTL ? require('../../img/vault-shape-rtl.png') : require('../../img/vault-shape.png');
2020-10-05 22:25:14 +01:00
default:
2021-03-27 06:34:26 -04:00
return I18nManager.isRTL ? require('../../img/btc-shape-rtl.png') : require('../../img/btc-shape.png');
2020-10-05 22:25:14 +01:00
}
})()}
style={styles.image}
/>
<Text style={styles.transparentText} />
<Text numberOfLines={1} style={styles.label}>
{item.getLabel()}
</Text>
2021-01-05 13:48:13 -05:00
{item.hideBalance ? (
<BluePrivateBalance />
) : (
<Text numberOfLines={1} adjustsFontSizeToFit style={styles.balance}>
{formatBalance(Number(item.getBalance()), item.getPreferredBalanceUnit(), true)}
</Text>
)}
<Text style={styles.transparentText} />
<Text numberOfLines={1} style={styles.latestTxLabel}>
2020-07-20 16:38:46 +03:00
{loc.wallets.list_latest_transaction}
</Text>
<Text numberOfLines={1} style={styles.latestTxValue}>
2021-01-25 18:01:08 +00:00
{item.getTransactions().find(tx => tx.confirmations === 0)
? loc.transactions.pending.toLowerCase()
: transactionTimeToReadable(item.getLatestTransactionTime())}
</Text>
</LinearGradient>
</View>
);
};
2020-08-07 22:56:49 -04:00
const onChangeOrder = () => {
ReactNativeHapticFeedback.trigger('impactMedium', { ignoreAndroidSystemSettings: false });
setHasMovedARow(true);
};
2020-08-07 22:56:49 -04:00
const onActivateRow = () => {
ReactNativeHapticFeedback.trigger('selection', { ignoreAndroidSystemSettings: false });
};
2020-08-07 22:56:49 -04:00
const onReleaseRow = () => {
ReactNativeHapticFeedback.trigger('impactLight', { ignoreAndroidSystemSettings: false });
};
return isLoading ? (
<View style={[styles.loading, stylesHook.loading]}>
<ActivityIndicator />
</View>
) : (
2020-09-15 20:48:24 -04:00
<View style={[styles.root, stylesHook.root]}>
2021-09-13 13:43:26 -04:00
<StatusBar barStyle="default" />
2021-08-26 00:37:19 -04:00
<SortableList
ref={sortableList}
data={data}
renderRow={renderItem}
onChangeOrder={onChangeOrder}
onActivateRow={onActivateRow}
onReleaseRow={onReleaseRow}
style={styles.root}
/>
2020-09-15 20:48:24 -04:00
</View>
2020-08-07 22:56:49 -04:00
);
};
2020-07-15 13:32:59 -04:00
2021-02-15 11:03:54 +03:00
ReorderWallets.navigationOptions = navigationStyle(
{
2021-09-13 13:43:26 -04:00
headerHideBackButton: true,
2020-12-25 19:09:53 +03:00
},
2021-09-14 01:36:00 -04:00
opts => ({ ...opts, headerTitle: loc.wallets.reorder_title }),
2021-02-15 11:03:54 +03:00
);
2020-08-07 22:56:49 -04:00
export default ReorderWallets;