-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e23bb7d
commit d2121e7
Showing
6 changed files
with
343 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
apps/mobile/src/components/GalleryEditor/SortableTokenGrid/SortableToken.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { FlashList } from '@shopify/flash-list'; | ||
import { useCallback } from 'react'; | ||
import { useWindowDimensions } from 'react-native'; | ||
import { Gesture, GestureDetector } from 'react-native-gesture-handler'; | ||
import { trigger } from 'react-native-haptic-feedback'; | ||
import Animated, { | ||
AnimatedRef, | ||
Easing, | ||
runOnJS, | ||
scrollTo, | ||
SharedValue, | ||
useAnimatedReaction, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withTiming, | ||
} from 'react-native-reanimated'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
|
||
import { ListItemType } from '../GalleryEditorRenderer'; | ||
import { getOrder, getPosition, Positions } from './utils'; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
id: string; | ||
positions: SharedValue<Positions>; | ||
size: number; | ||
columns: number; | ||
|
||
scrollContentOffsetY: SharedValue<number>; | ||
scrollViewRef: AnimatedRef<FlashList<ListItemType>>; | ||
|
||
onDragEnd: (data: string[]) => void; | ||
}; | ||
|
||
export function SortableToken({ | ||
children, | ||
columns, | ||
positions, | ||
size, | ||
id, | ||
|
||
scrollContentOffsetY, | ||
scrollViewRef, | ||
onDragEnd, | ||
}: Props) { | ||
const position = getPosition(positions.value[id] || 0, columns, size); | ||
|
||
// Shared values for gesture handling | ||
const contextX = useSharedValue(0); | ||
const contextY = useSharedValue(0); | ||
|
||
const translateX = useSharedValue(position.x); | ||
const translateY = useSharedValue(position.y); | ||
|
||
const isGestureActive = useSharedValue(false); | ||
|
||
// Get safe area insets and window dimensions | ||
const inset = useSafeAreaInsets(); | ||
const { height: windowHeight } = useWindowDimensions(); | ||
const containerHeight = windowHeight - inset.top; | ||
|
||
useAnimatedReaction( | ||
() => positions.value[id], | ||
(newOrder) => { | ||
// if (!newOrder) return; | ||
const newPosition = getPosition(newOrder || 0, columns, size); | ||
translateX.value = withTiming(newPosition.x, { | ||
duration: 350, | ||
easing: Easing.inOut(Easing.ease), | ||
}); | ||
translateY.value = withTiming(newPosition.y, { | ||
duration: 350, | ||
easing: Easing.inOut(Easing.ease), | ||
}); | ||
} | ||
); | ||
|
||
// Callback to handle edge cases while scrolling | ||
// (when the user drags the item to the top or bottom of the list) | ||
// Since we need to update the scroll position of the scroll view (scrollTo) | ||
const scrollLogic = useCallback( | ||
({ absoluteY }: { absoluteY: number }) => { | ||
'worklet'; | ||
|
||
const lowerBound = 1.5 * size; | ||
const upperBound = scrollContentOffsetY.value + containerHeight; | ||
const scrollSpeed = size * 0.1; | ||
|
||
if (absoluteY <= lowerBound) { | ||
// while scrolling to the top of the list | ||
const nextPosition = scrollContentOffsetY.value - scrollSpeed; | ||
scrollTo(scrollViewRef, 0, Math.max(nextPosition, 0), false); | ||
} else if (absoluteY + scrollContentOffsetY.value >= upperBound) { | ||
// while scrolling to the bottom of the list | ||
const nextPosition = scrollContentOffsetY.value + scrollSpeed; | ||
scrollTo(scrollViewRef, 0, Math.max(nextPosition, 0), false); | ||
} | ||
}, | ||
[containerHeight, scrollContentOffsetY, scrollViewRef, size] | ||
); | ||
|
||
const panGesture = Gesture.Pan() | ||
.activateAfterLongPress(300) | ||
.onStart(() => { | ||
isGestureActive.value = true; | ||
contextX.value = position.x; | ||
contextY.value = position.y - scrollContentOffsetY.value; | ||
|
||
runOnJS(trigger)('impactLight'); | ||
}) | ||
.onUpdate((event) => { | ||
const { translationX, translationY, absoluteY } = event; | ||
|
||
translateX.value = contextX.value + translationX; | ||
translateY.value = contextY.value + translationY + scrollContentOffsetY.value; | ||
|
||
const oldOrder = positions.value[id]; | ||
const newOrder = getOrder(translateX.value, translateY.value, columns, size); | ||
|
||
if (oldOrder !== newOrder) { | ||
const idToSwap = Object.keys(positions.value).find( | ||
(key) => positions.value[key] === newOrder | ||
); | ||
|
||
if (idToSwap) { | ||
const newPositions = JSON.parse(JSON.stringify(positions.value)); | ||
newPositions[id] = newOrder; | ||
newPositions[idToSwap] = oldOrder; | ||
positions.value = newPositions; | ||
} | ||
} | ||
|
||
scrollLogic({ absoluteY }); | ||
}) | ||
.onFinalize(() => { | ||
const destination = getPosition(positions.value[id] || 0, columns, size); | ||
|
||
translateX.value = withTiming( | ||
destination.x, | ||
{ | ||
easing: Easing.inOut(Easing.ease), | ||
duration: 350, | ||
}, | ||
() => { | ||
isGestureActive.value = false; | ||
|
||
const newPositions = Object.keys(positions.value).map((key) => positions.value[key]); | ||
|
||
runOnJS(onDragEnd)(newPositions.map(String)); | ||
} | ||
); | ||
|
||
translateY.value = withTiming(destination.y, { | ||
easing: Easing.inOut(Easing.ease), | ||
duration: 350, | ||
}); | ||
}); | ||
|
||
const style = useAnimatedStyle(() => { | ||
const zIndex = isGestureActive.value ? 100 : 0; | ||
const scale = isGestureActive.value ? 1.1 : 1; | ||
|
||
return { | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: size, | ||
height: size, | ||
zIndex, | ||
transform: [{ translateX: translateX.value }, { translateY: translateY.value }, { scale }], | ||
}; | ||
}); | ||
|
||
return ( | ||
<GestureDetector gesture={panGesture}> | ||
<Animated.View style={style}>{children}</Animated.View> | ||
</GestureDetector> | ||
); | ||
} |
85 changes: 85 additions & 0 deletions
85
apps/mobile/src/components/GalleryEditor/SortableTokenGrid/SortableTokenGrid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { FlashList } from '@shopify/flash-list'; | ||
import { useMemo } from 'react'; | ||
import { View, ViewProps } from 'react-native'; | ||
import { AnimatedRef, SharedValue, useSharedValue } from 'react-native-reanimated'; | ||
|
||
import { StagedItem } from '~/contexts/GalleryEditor/types'; | ||
|
||
import { ListItemType } from '../GalleryEditorRenderer'; | ||
import { GalleryEditorTokenPreview } from '../GalleryEditorTokenPreview'; | ||
import { SortableToken } from './SortableToken'; | ||
import { Positions } from './utils'; | ||
|
||
type Props = { | ||
columns: number; | ||
items: StagedItem[]; | ||
size: number; | ||
|
||
scrollContentOffsetY: SharedValue<number>; | ||
scrollViewRef: AnimatedRef<FlashList<ListItemType>>; | ||
|
||
onDragEnd: (data: string[]) => void; | ||
}; | ||
|
||
export function SortableTokenGrid({ | ||
columns, | ||
items, | ||
size, | ||
scrollContentOffsetY, | ||
scrollViewRef, | ||
onDragEnd, | ||
}: Props) { | ||
const positions = useSharedValue<Positions>( | ||
Object.assign({}, ...items.map((item, index) => ({ [item.id]: index }))) | ||
); | ||
|
||
const containerHeight = useMemo(() => { | ||
return Math.ceil(items.length / columns) * size; | ||
}, [items.length, columns, size]); | ||
|
||
return ( | ||
<View | ||
style={{ | ||
height: containerHeight, | ||
width: '100%', | ||
}} | ||
> | ||
{items.map((item) => ( | ||
<SortableToken | ||
key={item.id} | ||
id={item.id} | ||
positions={positions} | ||
size={size} | ||
columns={columns} | ||
scrollContentOffsetY={scrollContentOffsetY} | ||
scrollViewRef={scrollViewRef} | ||
onDragEnd={onDragEnd} | ||
> | ||
<View> | ||
{item.kind === 'whitespace' ? ( | ||
<WhiteSpace size={size - 8} /> | ||
) : ( | ||
<View | ||
className="aspect-square" | ||
style={{ | ||
width: size - 8, | ||
}} | ||
> | ||
<GalleryEditorTokenPreview tokenRef={item.tokenRef} /> | ||
</View> | ||
)} | ||
</View> | ||
</SortableToken> | ||
))} | ||
</View> | ||
); | ||
} | ||
|
||
type WhiteSpaceProps = { | ||
size: number; | ||
style?: ViewProps['style']; | ||
}; | ||
|
||
function WhiteSpace({ size, style }: WhiteSpaceProps) { | ||
return <View style={[{ width: size, height: size }, style]} />; | ||
} |
Oops, something went wrong.