BlueWallet/screen/wallets/reorderWallets.js

145 lines
4.3 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useRef, useContext, useState, useLayoutEffect } from 'react';
import { StyleSheet, useColorScheme, Platform } from 'react-native';
2023-06-17 10:42:32 -05:00
import DraggableFlatList, { ScaleDecorator } from 'react-native-draggable-flatlist';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2022-01-26 10:54:32 -05:00
import { GestureHandlerRootView } from 'react-native-gesture-handler';
2023-10-23 21:28:44 -04:00
import { useTheme } from '../../components/themes';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import { WalletCarouselItem } from '../../components/WalletsCarousel';
2024-03-24 10:52:10 -04:00
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
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,
},
});
}, [setSearchQuery, setIsSearchFocused, setOptions]);
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
2021-02-15 11:03:54 +03:00
ReorderWallets.navigationOptions = navigationStyle(
{
2023-11-11 07:33:50 -04:00
headerBackVisible: false,
headerLargeTitle: true,
closeButton: 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;