Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 10x 8x 6x 6x 6x 6x 2x 2x 10x 10x 10x 2x 10x 10x 10x 2x 8x 8x 2x 2x | /**
* PostMediaGallery - 帖子媒体画廊
*
* 全宽无边框沉浸式设计
* 支持:图片预览、视频标识、轮播指示器
*/
import React, { memo, useCallback, useState, useRef, useEffect } from 'react';
import { Pressable, Dimensions, FlatList, ViewToken, Image } from 'react-native';
import Animated, { useAnimatedStyle, withSpring } from 'react-native-reanimated';
import { Play } from '@tamagui/lucide-icons';
import { styled, XStack, YStack, Stack, Text } from 'tamagui';
import { OptimizedImage } from '@/src/components/ui/OptimizedImage';
import { VideoPreview } from '../VideoPreview';
import type { PostMedia } from '@/src/lib/supabase';
const { width: SCREEN_WIDTH } = Dimensions.get('window');
// 图片显示配置
const GALLERY_PADDING = 16; // 画廊内边距
const IMAGE_WIDTH = SCREEN_WIDTH - GALLERY_PADDING * 2; // 图片宽度
const MAX_IMAGE_HEIGHT = SCREEN_WIDTH * 0.85; // 最大高度限制
const MIN_IMAGE_HEIGHT = 180; // 最小高度
export interface PostMediaGalleryProps {
/** 媒体列表 */
media: PostMedia[];
/** 点击媒体 */
onMediaPress?: (media: PostMedia, index: number) => void;
}
// 样式组件
const Container = styled(YStack, {
name: 'MediaGallery',
width: '100%',
backgroundColor: '#f8f9fa',
paddingVertical: 12,
});
const MediaItem = styled(Stack, {
name: 'MediaItem',
width: IMAGE_WIDTH,
minHeight: MIN_IMAGE_HEIGHT,
maxHeight: MAX_IMAGE_HEIGHT,
backgroundColor: '#f8f8f8',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 12,
overflow: 'hidden',
});
const VideoOverlay = styled(Stack, {
name: 'VideoOverlay',
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center',
});
const PlayButton = styled(Stack, {
name: 'PlayButton',
width: 64,
height: 64,
borderRadius: 32,
backgroundColor: 'rgba(255, 255, 255, 0.95)',
alignItems: 'center',
justifyContent: 'center',
shadowColor: '#000',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 8,
});
// 指示器样式
const IndicatorContainer = styled(XStack, {
name: 'IndicatorContainer',
marginTop: 12,
justifyContent: 'center',
alignItems: 'center',
gap: 6,
});
const AnimatedDot = Animated.createAnimatedComponent(Stack);
interface DotProps {
index: number;
currentIndex: number;
}
const Dot = memo(function Dot({ index, currentIndex }: DotProps) {
const isActive = index === currentIndex;
const animatedStyle = useAnimatedStyle(() => {
return {
width: withSpring(isActive ? 20 : 6, { damping: 15 }),
backgroundColor: isActive ? '#FEBE98' : 'rgba(0, 0, 0, 0.15)',
};
});
return (
<AnimatedDot
style={[
{
height: 6,
borderRadius: 3,
},
animatedStyle,
]}
/>
);
});
/**
* 单个媒体项组件
*/
interface MediaItemComponentProps {
media: PostMedia;
onPress?: () => void;
}
const MediaItemComponent = memo(function MediaItemComponent({
media,
onPress,
}: MediaItemComponentProps) {
const isVideo = media.mediaType === 'video';
const [imageHeight, setImageHeight] = useState(IMAGE_WIDTH * 0.75);
// 根据图片实际比例计算高度
useEffect(() => {
if (media.fileUrl && !isVideo) {
Image.getSize(
media.fileUrl,
(width, height) => {
const aspectRatio = height / width;
const calculatedHeight = Math.min(
Math.max(IMAGE_WIDTH * aspectRatio, MIN_IMAGE_HEIGHT),
MAX_IMAGE_HEIGHT
);
setImageHeight(calculatedHeight);
},
() => {
setImageHeight(IMAGE_WIDTH * 0.75);
}
);
} else Eif (isVideo) {
// 视频使用 16:9 比例
setImageHeight(IMAGE_WIDTH * 0.5625);
}
}, [media.fileUrl, isVideo]);
return (
<Pressable onPress={onPress}>
<Stack
width={IMAGE_WIDTH}
height={imageHeight}
backgroundColor="#000000"
justifyContent="center"
alignItems="center"
borderRadius={12}
overflow="hidden"
>
{isVideo ? (
// 视频预览:使用 VideoPreview 组件显示第一帧缩略图
<VideoPreview
videoUri={media.fileUrl}
width={IMAGE_WIDTH}
height={imageHeight}
showPlayButton={true}
/>
) : (
<OptimizedImage
source={media.fileUrl}
style={{ width: IMAGE_WIDTH, height: imageHeight }}
contentFit="cover"
cachePolicy="memory-disk"
/>
)}
</Stack>
</Pressable>
);
});
/**
* 帖子媒体画廊组件 - 沉浸式全宽设计
*/
function PostMediaGalleryComponent({ media, onMediaPress }: PostMediaGalleryProps) {
const [currentIndex, setCurrentIndex] = useState(0);
const handlePress = useCallback(
(item: PostMedia, index: number) => {
onMediaPress?.(item, index);
},
[onMediaPress]
);
const onViewableItemsChanged = useCallback(
({ viewableItems }: { viewableItems: ViewToken[] }) => {
if (viewableItems.length > 0 && viewableItems[0].index !== null) {
setCurrentIndex(viewableItems[0].index);
}
},
[]
);
const viewabilityConfig = useRef({
itemVisiblePercentThreshold: 50,
}).current;
if (!media || media.length === 0) {
return null;
}
// 单张图片直接展示(居中显示)
Eif (media.length === 1) {
return (
<Container alignItems="center">
<MediaItemComponent media={media[0]} onPress={() => handlePress(media[0], 0)} />
</Container>
);
}
// 多张图片轮播
return (
<Container>
<FlatList
data={media}
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
keyExtractor={(item) => item.id.toString()}
contentContainerStyle={{ paddingHorizontal: GALLERY_PADDING }}
renderItem={({ item, index }) => (
<Stack marginRight={index < media.length - 1 ? 8 : 0}>
<MediaItemComponent media={item} onPress={() => handlePress(item, index)} />
</Stack>
)}
onViewableItemsChanged={onViewableItemsChanged}
viewabilityConfig={viewabilityConfig}
decelerationRate="fast"
snapToInterval={IMAGE_WIDTH + 8}
snapToAlignment="start"
/>
{/* 精细指示器 */}
<IndicatorContainer>
{media.map((item, index) => (
<Dot key={item.id} index={index} currentIndex={currentIndex} />
))}
</IndicatorContainer>
</Container>
);
}
export const PostMediaGallery = memo(PostMediaGalleryComponent);
|