BlueWallet/screen/wallets/reorderWallets.js

137 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-05-20 10:54:13 +01:00
import React, { useContext, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { Platform, StyleSheet, useColorScheme } from 'react-native';
2023-06-17 10:42:32 -05:00
import DraggableFlatList, { ScaleDecorator } from 'react-native-draggable-flatlist';
2022-01-26 10:54:32 -05:00
import { GestureHandlerRootView } from 'react-native-gesture-handler';
2024-05-20 10:54:13 +01:00
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
2024-05-20 10:54:13 +01:00
import { BlueStorageContext } from '../../blue_modules/storage-context';
import { useTheme } from '../../components/themes';
import { WalletCarouselItem } from '../../components/WalletsCarousel';
2024-03-24 10:52:10 -04:00
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
2024-05-20 10:54:13 +01:00
import loc from '../../loc';
const styles = StyleSheet.create({
root: {
flex: 1,
},
padding16: {
padding: 16,
},
});
2020-08-07 22:56:49 -04:00
const ReorderWallets = () => {
const sortableList = useRef();
const { colors } = useTheme();
const { wallets, setWalletsWithNewOrder } = useContext(BlueStorageContext);
2023-10-20 13:47:37 -04:00
const colorScheme = useColorScheme();
2024-03-24 10:52:10 -04:00
const { navigate, setOptions } = useExtendedNavigation();
const [searchQuery, setSearchQuery] = useState('');
const [isSearchFocused, setIsSearchFocused] = useState(false);
const [walletData, setWalletData] = useState([]);
2020-08-07 22:56:49 -04:00
const stylesHook = {
root: {
backgroundColor: colors.elevated,
},
tip: {
backgroundColor: colors.ballOutgoingExpired,
},
2020-08-07 22:56:49 -04:00
};
2020-06-09 15:08:18 +01:00
2020-08-07 22:56:49 -04:00
useEffect(() => {
setWalletData(wallets);
}, [wallets]);
2023-10-20 13:47:37 -04:00
useEffect(() => {
setOptions({
statusBarStyle: Platform.select({ ios: 'light', default: colorScheme === 'dark' ? 'light' : 'dark' }),
});
}, [colorScheme, setOptions]);
useEffect(() => {
// Filter wallets based on search query
const filteredWallets = wallets.filter(wallet => wallet.getLabel().toLowerCase().includes(searchQuery.toLowerCase()));
setWalletData(filteredWallets);
}, [wallets, searchQuery]);
useLayoutEffect(() => {
// Set navigation options dynamically
setOptions({
headerSearchBarOptions: {
hideWhenScrolling: false,
onChangeText: event => setSearchQuery(event.nativeEvent.text),
onClear: () => setSearchQuery(''),
onFocus: () => setIsSearchFocused(true),
onBlur: () => setIsSearchFocused(false),
placeholder: loc.wallets.search_wallets,
},
});
2024-05-09 21:20:39 -04:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
2022-01-27 17:32:00 -05:00
const navigateToWallet = wallet => {
const walletID = wallet.getID();
navigate('WalletTransactions', {
walletID,
walletType: wallet.type,
});
};
const renderItem = ({ item, drag, isActive }) => {
const itemOpacity = isActive ? 1 : 0.5; // Set opacity to 0.5 if not active
return (
<ScaleDecorator>
<WalletCarouselItem
item={item}
handleLongPress={isDraggingDisabled ? null : drag}
isActive={isActive}
onPress={navigateToWallet}
customStyle={[styles.padding16, { opacity: itemOpacity }]} // Apply opacity style
/>
</ScaleDecorator>
);
};
2020-08-07 22:56:49 -04:00
const onChangeOrder = () => {
triggerHapticFeedback(HapticFeedbackTypes.ImpactMedium);
2020-08-07 22:56:49 -04:00
};
const onDragBegin = () => {
triggerHapticFeedback(HapticFeedbackTypes.Selection);
2020-08-07 22:56:49 -04:00
};
const onRelease = () => {
triggerHapticFeedback(HapticFeedbackTypes.ImpactLight);
2020-08-07 22:56:49 -04:00
};
const onDragEnd = ({ data }) => {
setWalletsWithNewOrder(data);
setWalletData(data);
};
const _keyExtractor = (_item, index) => index.toString();
const isDraggingDisabled = searchQuery.length > 0 || isSearchFocused;
2023-10-20 13:47:37 -04:00
return (
2022-01-26 10:54:32 -05:00
<GestureHandlerRootView style={[styles.root, stylesHook.root]}>
<DraggableFlatList
2021-08-26 00:37:19 -04:00
ref={sortableList}
contentInsetAdjustmentBehavior="automatic"
automaticallyAdjustContentInsets
data={walletData}
keyExtractor={_keyExtractor}
renderItem={renderItem}
2021-08-26 00:37:19 -04:00
onChangeOrder={onChangeOrder}
onDragBegin={onDragBegin}
onRelease={onRelease}
onDragEnd={onDragEnd}
containerStyle={styles.root}
2021-08-26 00:37:19 -04:00
/>
2022-01-26 10:54:32 -05:00
</GestureHandlerRootView>
2020-08-07 22:56:49 -04:00
);
};
2020-07-15 13:32:59 -04:00
2020-08-07 22:56:49 -04:00
export default ReorderWallets;