237 lines
7.3 KiB
TypeScript
237 lines
7.3 KiB
TypeScript
import { Ionicons } from "@expo/vector-icons";
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
import {
|
|
FlatList,
|
|
Modal,
|
|
Pressable,
|
|
StyleSheet,
|
|
Text,
|
|
useWindowDimensions,
|
|
View,
|
|
ViewToken,
|
|
} from "react-native";
|
|
import { GestureHandlerRootView } from "react-native-gesture-handler";
|
|
import { SafeAreaView } from "react-native-safe-area-context";
|
|
|
|
import { useAuthStore } from "@/store/auth.store";
|
|
import { Colors } from "@/theme/colors";
|
|
import type { ContentMediaItem } from "../types/media.types";
|
|
import { mediaLabel } from "./media-source";
|
|
import { ZoomableImage, type ZoomableImageHandle } from "./ZoomableImage";
|
|
|
|
interface FullScreenMediaViewerProps {
|
|
visible: boolean;
|
|
images: ContentMediaItem[];
|
|
initialIndex: number;
|
|
onClose: () => void;
|
|
}
|
|
|
|
export function FullScreenMediaViewer({
|
|
visible,
|
|
images,
|
|
initialIndex,
|
|
onClose,
|
|
}: FullScreenMediaViewerProps) {
|
|
const listRef = useRef<FlatList<ContentMediaItem>>(null);
|
|
const imageRefs = useRef<Map<string, ZoomableImageHandle>>(new Map());
|
|
const { width, height } = useWindowDimensions();
|
|
const accessToken = useAuthStore((state) => state.accessToken);
|
|
const [activeIndex, setActiveIndex] = useState(initialIndex);
|
|
const [zoomed, setZoomed] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!visible) return;
|
|
const nextIndex = Math.min(Math.max(initialIndex, 0), Math.max(images.length - 1, 0));
|
|
setActiveIndex(nextIndex);
|
|
setZoomed(false);
|
|
requestAnimationFrame(() => {
|
|
listRef.current?.scrollToIndex({ index: nextIndex, animated: false });
|
|
});
|
|
}, [images.length, initialIndex, visible]);
|
|
|
|
const onViewableItemsChanged = useRef(
|
|
({ viewableItems }: { viewableItems: ViewToken<ContentMediaItem>[] }) => {
|
|
const index = viewableItems.find((item) => item.isViewable)?.index;
|
|
if (typeof index === "number") {
|
|
setActiveIndex(index);
|
|
setZoomed(false);
|
|
}
|
|
},
|
|
).current;
|
|
|
|
if (!images.length) return null;
|
|
|
|
const active = images[activeIndex];
|
|
|
|
const handleZoomIn = () => {
|
|
if (active) {
|
|
imageRefs.current.get(active.id)?.zoomIn();
|
|
}
|
|
};
|
|
|
|
const handleZoomOut = () => {
|
|
if (active) {
|
|
imageRefs.current.get(active.id)?.zoomOut();
|
|
}
|
|
};
|
|
|
|
const handleResetZoom = () => {
|
|
if (active) {
|
|
imageRefs.current.get(active.id)?.resetZoom();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
visible={visible}
|
|
animationType="fade"
|
|
presentationStyle="fullScreen"
|
|
statusBarTranslucent
|
|
onRequestClose={onClose}
|
|
>
|
|
<GestureHandlerRootView style={styles.modalRoot}>
|
|
<SafeAreaView style={styles.root}>
|
|
<View style={styles.toolbar}>
|
|
<Text style={styles.counter} accessibilityLiveRegion="polite">
|
|
{activeIndex + 1} / {images.length}
|
|
</Text>
|
|
|
|
<View style={styles.controlsGroup}>
|
|
<Pressable
|
|
onPress={handleZoomOut}
|
|
hitSlop={8}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Zoom out"
|
|
style={({ pressed }) => [styles.controlButton, pressed && styles.pressed]}
|
|
>
|
|
<Ionicons name="remove" size={20} color={Colors.white} />
|
|
</Pressable>
|
|
|
|
<Pressable
|
|
onPress={handleResetZoom}
|
|
hitSlop={8}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Reset zoom"
|
|
style={({ pressed }) => [styles.controlButton, pressed && styles.pressed]}
|
|
>
|
|
<Ionicons name="refresh" size={16} color={Colors.white} />
|
|
</Pressable>
|
|
|
|
<Pressable
|
|
onPress={handleZoomIn}
|
|
hitSlop={8}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Zoom in"
|
|
style={({ pressed }) => [styles.controlButton, pressed && styles.pressed]}
|
|
>
|
|
<Ionicons name="add" size={20} color={Colors.white} />
|
|
</Pressable>
|
|
</View>
|
|
|
|
<Pressable
|
|
onPress={onClose}
|
|
hitSlop={16}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Close image viewer"
|
|
style={({ pressed }) => [styles.closeButton, pressed && styles.pressed]}
|
|
>
|
|
<Ionicons name="close" size={26} color={Colors.white} />
|
|
</Pressable>
|
|
</View>
|
|
|
|
<FlatList
|
|
ref={listRef}
|
|
data={images}
|
|
horizontal
|
|
pagingEnabled
|
|
scrollEnabled={!zoomed}
|
|
showsHorizontalScrollIndicator={false}
|
|
keyExtractor={(item) => item.id}
|
|
initialScrollIndex={Math.min(initialIndex, Math.max(images.length - 1, 0))}
|
|
getItemLayout={(_data, index) => ({ length: width, offset: width * index, index })}
|
|
onScrollToIndexFailed={(info) => {
|
|
requestAnimationFrame(() => {
|
|
listRef.current?.scrollToOffset({ offset: info.index * width, animated: false });
|
|
});
|
|
}}
|
|
viewabilityConfig={{ itemVisiblePercentThreshold: 60 }}
|
|
onViewableItemsChanged={onViewableItemsChanged}
|
|
renderItem={({ item, index }) => (
|
|
<ZoomableImage
|
|
ref={(handle) => {
|
|
if (handle) {
|
|
imageRefs.current.set(item.id, handle);
|
|
} else {
|
|
imageRefs.current.delete(item.id);
|
|
}
|
|
}}
|
|
item={item}
|
|
accessToken={accessToken}
|
|
width={width}
|
|
height={Math.max(height - 150, 320)}
|
|
active={index === activeIndex}
|
|
onZoomStateChange={index === activeIndex ? setZoomed : undefined}
|
|
/>
|
|
)}
|
|
/>
|
|
|
|
<View style={styles.footer}>
|
|
<Text numberOfLines={2} style={styles.caption}>
|
|
{mediaLabel(active, activeIndex)}
|
|
</Text>
|
|
<Text style={styles.hint}>
|
|
Pinch, double-tap, or use +/- to zoom · Drag to pan
|
|
</Text>
|
|
</View>
|
|
</SafeAreaView>
|
|
</GestureHandlerRootView>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
modalRoot: { flex: 1, backgroundColor: Colors.black },
|
|
root: { flex: 1, backgroundColor: Colors.black },
|
|
toolbar: {
|
|
height: 56,
|
|
marginTop: 16,
|
|
paddingHorizontal: 16,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
},
|
|
counter: { color: Colors.white, fontSize: 15, fontWeight: "700" },
|
|
controlsGroup: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
backgroundColor: "rgba(255,255,255,0.12)",
|
|
borderRadius: 20,
|
|
paddingHorizontal: 4,
|
|
gap: 4,
|
|
},
|
|
controlButton: {
|
|
width: 36,
|
|
height: 36,
|
|
borderRadius: 18,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
closeButton: {
|
|
width: 44,
|
|
height: 44,
|
|
borderRadius: 22,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
backgroundColor: "rgba(255,255,255,0.12)",
|
|
},
|
|
pressed: { opacity: 0.65 },
|
|
footer: {
|
|
minHeight: 64,
|
|
paddingHorizontal: 20,
|
|
paddingTop: 6,
|
|
paddingBottom: 8,
|
|
},
|
|
caption: { color: Colors.white, fontSize: 15, lineHeight: 20, textAlign: "center" },
|
|
hint: { marginTop: 4, color: "#AFAFAF", fontSize: 12, textAlign: "center" },
|
|
});
|