|
| 1 | +import React, {PropsWithChildren, useCallback} from 'react'; |
| 2 | +import {LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native'; |
| 3 | +import {Gesture, GestureDetector} from 'react-native-gesture-handler'; |
| 4 | +import Animated, { |
| 5 | + runOnJS, |
| 6 | + useAnimatedReaction, |
| 7 | + useAnimatedStyle, |
| 8 | + useSharedValue, |
| 9 | + withSpring, |
| 10 | + withTiming |
| 11 | +} from 'react-native-reanimated'; |
| 12 | +import usePresenter, {ItemsOrder, animationConfig} from './usePresenter'; |
| 13 | +import View from '../view'; |
| 14 | + |
| 15 | +interface SortableItemProps extends ReturnType<typeof usePresenter> { |
| 16 | + index: number; |
| 17 | + itemsOrder: Animated.SharedValue<ItemsOrder>; |
| 18 | + onChange: () => void; |
| 19 | + style: StyleProp<ViewStyle>; |
| 20 | +} |
| 21 | + |
| 22 | +function SortableItem(props: PropsWithChildren<SortableItemProps>) { |
| 23 | + const { |
| 24 | + index, |
| 25 | + itemsOrder, |
| 26 | + onChange, |
| 27 | + style, |
| 28 | + getItemOrderById, |
| 29 | + getOrderByPosition, |
| 30 | + getIdByItemOrder, |
| 31 | + getTranslationByOrderChange, |
| 32 | + updateItemLayout |
| 33 | + } = props; |
| 34 | + const translateX = useSharedValue(0); |
| 35 | + const translateY = useSharedValue(0); |
| 36 | + |
| 37 | + const isFloating = useSharedValue(false); |
| 38 | + const isDragging = useSharedValue(false); |
| 39 | + const tempItemsOrder = useSharedValue(itemsOrder.value); |
| 40 | + const tempTranslateX = useSharedValue(0); |
| 41 | + const tempTranslateY = useSharedValue(0); |
| 42 | + |
| 43 | + const onLayout = useCallback((event: LayoutChangeEvent) => { |
| 44 | + 'worklet'; |
| 45 | + const {width, height} = event.nativeEvent.layout; |
| 46 | + updateItemLayout(index, {width, height}); |
| 47 | + }, []); |
| 48 | + |
| 49 | + useAnimatedReaction(() => itemsOrder.value.indexOf(index), // Note: It doesn't work with the getItemOrderById util |
| 50 | + (newOrder, prevOrder) => { |
| 51 | + if (prevOrder !== null && newOrder !== prevOrder) { |
| 52 | + const translation = getTranslationByOrderChange(newOrder, prevOrder); |
| 53 | + translateX.value = withTiming(translateX.value + translation.x, animationConfig); |
| 54 | + translateY.value = withTiming(translateY.value + translation.y, animationConfig); |
| 55 | + } else if (newOrder === index) { |
| 56 | + translateX.value = withTiming(0, animationConfig); |
| 57 | + translateY.value = withTiming(0, animationConfig); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + const longPressGesture = Gesture.LongPress() |
| 62 | + .onStart(() => { |
| 63 | + isFloating.value = true; |
| 64 | + }) |
| 65 | + .onTouchesCancelled(() => { |
| 66 | + if (!isDragging.value) { |
| 67 | + isFloating.value = false; |
| 68 | + } |
| 69 | + }) |
| 70 | + .minDuration(250); |
| 71 | + |
| 72 | + const dragGesture = Gesture.Pan() |
| 73 | + .manualActivation(true) |
| 74 | + .onTouchesMove((_e, state) => { |
| 75 | + if (isFloating.value) { |
| 76 | + isDragging.value = true; |
| 77 | + state.activate(); |
| 78 | + } else { |
| 79 | + isDragging.value = false; |
| 80 | + state.fail(); |
| 81 | + } |
| 82 | + }) |
| 83 | + .onStart(() => { |
| 84 | + tempTranslateX.value = translateX.value; |
| 85 | + tempTranslateY.value = translateY.value; |
| 86 | + tempItemsOrder.value = itemsOrder.value; |
| 87 | + }) |
| 88 | + .onUpdate(event => { |
| 89 | + translateX.value = tempTranslateX.value + event.translationX; |
| 90 | + translateY.value = tempTranslateY.value + event.translationY; |
| 91 | + |
| 92 | + // Swapping items |
| 93 | + const oldOrder = getItemOrderById(itemsOrder.value, index); |
| 94 | + const newOrder = getOrderByPosition(translateX.value, translateY.value) + index; |
| 95 | + |
| 96 | + if (oldOrder !== newOrder) { |
| 97 | + const itemIdToSwap = getIdByItemOrder(itemsOrder.value, newOrder); |
| 98 | + |
| 99 | + if (itemIdToSwap !== undefined) { |
| 100 | + const newItemsOrder = [...itemsOrder.value]; |
| 101 | + newItemsOrder[newOrder] = index; |
| 102 | + newItemsOrder[oldOrder] = itemIdToSwap; |
| 103 | + itemsOrder.value = newItemsOrder; |
| 104 | + } |
| 105 | + } |
| 106 | + }) |
| 107 | + .onEnd(() => { |
| 108 | + const translation = getTranslationByOrderChange(getItemOrderById(itemsOrder.value, index), |
| 109 | + getItemOrderById(tempItemsOrder.value, index)); |
| 110 | + |
| 111 | + translateX.value = withTiming(tempTranslateX.value + translation.x, animationConfig); |
| 112 | + translateY.value = withTiming(tempTranslateY.value + translation.y, animationConfig); |
| 113 | + }) |
| 114 | + .onFinalize(() => { |
| 115 | + if (isDragging.value) { |
| 116 | + isDragging.value = false; |
| 117 | + isFloating.value = false; |
| 118 | + if (tempItemsOrder.value.toString() !== itemsOrder.value.toString()) { |
| 119 | + runOnJS(onChange)(); |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | + .simultaneousWithExternalGesture(longPressGesture); |
| 124 | + |
| 125 | + const gesture = Gesture.Race(dragGesture, longPressGesture); |
| 126 | + |
| 127 | + const animatedStyle = useAnimatedStyle(() => { |
| 128 | + const scale = withSpring(isFloating.value ? 1.1 : 1); |
| 129 | + const zIndex = isFloating.value ? 100 : withTiming(0, animationConfig); |
| 130 | + |
| 131 | + return { |
| 132 | + zIndex, |
| 133 | + transform: [{translateX: translateX.value}, {translateY: translateY.value}, {scale}] |
| 134 | + }; |
| 135 | + }); |
| 136 | + return ( |
| 137 | + <View reanimated style={[style, animatedStyle]} onLayout={onLayout}> |
| 138 | + <GestureDetector gesture={gesture}> |
| 139 | + <View>{props.children}</View> |
| 140 | + </GestureDetector> |
| 141 | + </View> |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +export default SortableItem; |
0 commit comments