File size: 6,413 Bytes
f0ba57a e179c31 f0ba57a 2c5bbee f0ba57a c0cd69c 2c5bbee c0cd69c 2c5bbee e179c31 bb27193 2c5bbee c0cd69c 2c5bbee c0cd69c 2c5bbee bb27193 2c5bbee bb27193 2c5bbee bb27193 2c5bbee bb27193 2c5bbee bb27193 2c5bbee e179c31 2c5bbee c0cd69c 2c5bbee bb27193 c0cd69c 2c5bbee c0cd69c 2c5bbee f0ba57a e179c31 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 e179c31 bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a e179c31 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a bb27193 f0ba57a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | import React, { useRef, useState, useEffect } from 'react';
import { View, StyleSheet, Platform, useWindowDimensions, PanResponder } from 'react-native';
import { Image } from 'expo-image';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import { FlatList } from 'react-native-gesture-handler';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
runOnJS,
} from 'react-native-reanimated';
import CustomVideoPlayer from './CustomVideoPlayer';
interface FullScreenSwiperProps {
assets: any[];
initialIndex: number;
onClose: () => void;
onIndexChange: (index: number) => void;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ZoomableImage
β’ Gesture.Pinch() β native multi-touch pinch-to-zoom
β’ Gesture.Pan() β single-finger swipe-down-to-close
β’ Both run on the UI thread via reanimated worklets
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
function ZoomableImage({
uri, thumbUri, width, height, onSwipeDown, onZoomChange,
}: {
uri: string; thumbUri?: string; width: number;
onSwipeDown: () => void; onZoomChange: (zooming: boolean) => void;
}) {
const scale = useSharedValue(1);
const savedScale = useSharedValue(1);
const translateY = useSharedValue(0);
const pinchGesture = Gesture.Pinch()
.onStart(() => {
'worklet';
runOnJS(onZoomChange)(true);
})
.onUpdate((e) => {
'worklet';
scale.value = Math.max(0.5, Math.min(5, savedScale.value * e.scale));
})
.onEnd(() => {
'worklet';
runOnJS(onZoomChange)(false);
if (scale.value < 1.15) {
scale.value = withSpring(1, { damping: 15, stiffness: 150 });
savedScale.value = 1;
} else {
savedScale.value = Math.min(5, scale.value);
}
});
const panGesture = Gesture.Pan()
.minPointers(1)
.maxPointers(1)
.activeOffsetY(20)
.failOffsetX([-15, 15])
.onStart(() => {
'worklet';
runOnJS(onZoomChange)(true);
})
.onUpdate((e) => {
'worklet';
translateY.value = e.translationY;
})
.onEnd((e) => {
'worklet';
runOnJS(onZoomChange)(false);
if (e.translationY > 100) {
runOnJS(onSwipeDown)();
} else {
translateY.value = withSpring(0, { damping: 15, stiffness: 150 });
}
});
const composedGesture = Gesture.Simultaneous(pinchGesture, panGesture);
const animatedStyle = useAnimatedStyle(() => ({
transform: [
{ scale: scale.value },
{ translateY: translateY.value },
],
}));
return (
<GestureDetector gesture={composedGesture}>
<Animated.View
style={[
{ width, height: '100%', justifyContent: 'center', alignItems: 'center' },
animatedStyle,
]}
>
<Image
source={{ uri }}
placeholder={thumbUri ? { uri: thumbUri } : undefined}
style={{ width: '100%', height: '100%' }}
contentFit="contain"
cachePolicy="memory-disk"
transition={200}
backgroundColor="transparent"
allowDownscaling={false}
/>
</Animated.View>
</GestureDetector>
);
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FullScreenSwiper
Uses RNGH's FlatList so gesture handlers inside items
properly negotiate with the scroll gesture on Android.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
export default function FullScreenSwiper({
assets, initialIndex, onClose, onIndexChange,
}: FullScreenSwiperProps) {
const { width } = useWindowDimensions();
const [activeIndex, setActiveIndex] = useState(initialIndex);
const [scrollEnabled, setScrollEnabled] = useState(true);
const onIndexChangeRef = useRef(onIndexChange);
useEffect(() => { onIndexChangeRef.current = onIndexChange; }, [onIndexChange]);
const onViewableItemsChanged = useRef(({ viewableItems }: any) => {
if (viewableItems.length > 0) {
const idx = viewableItems[0].index;
setActiveIndex(idx);
onIndexChangeRef.current(idx);
}
}).current;
const viewabilityConfig = useRef({ itemVisiblePercentThreshold: 50 }).current;
const renderItem = ({ item, index }: { item: any; index: number }) => {
const isVideo =
item.mediaType === 'video' ||
item.filename?.toLowerCase().match(/\.(mp4|mov|avi|webm)$/i);
const isActive = index === activeIndex;
if (isVideo) {
return (
<View style={{ width, height: '100%', justifyContent: 'center', backgroundColor: '#000' }}>
<CustomVideoPlayer
uri={item.uri}
shouldPlay={isActive}
onScrubStart={() => setScrollEnabled(false)}
onScrubEnd={() => setScrollEnabled(true)}
/>
</View>
);
}
return (
<ZoomableImage
uri={item.uri}
thumbUri={item.cloudThumbUri}
width={width}
onSwipeDown={onClose}
onZoomChange={(zooming) => setScrollEnabled(!zooming)}
/>
);
};
return (
<View style={styles.container}>
<FlatList
key={`swiper-${width}`}
data={assets}
keyExtractor={(item) => item.id}
renderItem={renderItem}
horizontal
pagingEnabled
scrollEnabled={scrollEnabled}
showsHorizontalScrollIndicator={false}
initialScrollIndex={activeIndex}
getItemLayout={(_, index) => ({
length: width,
offset: width * index,
index,
})}
onViewableItemsChanged={onViewableItemsChanged}
viewabilityConfig={viewabilityConfig}
windowSize={3}
maxToRenderPerBatch={3}
removeClippedSubviews={Platform.OS === 'android'}
extraData={activeIndex}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
},
});
|