Merge pull request 'Issue #34: fix image operations, gestures, and zoom controls in modules (Issue #34)' (#48) from fix/issue-34-module-image-operations into main

Reviewed-on: https://10.10.0.4/AcuPedia/acupedia-mobile-ui/pulls/48
Reviewed-by: Amit Salunke <amit@noreply.localhost>
This commit is contained in:
Amit Salunke 2026-10-05 10:59:29 +00:00
commit 82d0b54b93
5 changed files with 363 additions and 231 deletions

View File

@ -31,7 +31,7 @@ function sortMedia(media: ContentMediaItem[]): ContentMediaItem[] {
.sort((a, b) => {
if (a.media_role === "primary" && b.media_role !== "primary") return -1;
if (b.media_role === "primary" && a.media_role !== "primary") return 1;
return a.sort_order - b.sort_order || a.id.localeCompare(b.id);
return (a.sort_order ?? 0) - (b.sort_order ?? 0) || a.id.localeCompare(b.id);
});
}
@ -232,7 +232,7 @@ const styles = StyleSheet.create({
slide: { height: 290, alignItems: "center", justifyContent: "center" },
image: { width: "100%", height: "100%", backgroundColor: Colors.surface },
loadingOverlay: {
...StyleSheet.absoluteFillObject,
...(StyleSheet.absoluteFill as object),
alignItems: "center",
justifyContent: "center",
backgroundColor: Colors.surface,

View File

@ -10,13 +10,14 @@ import {
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 } from "./ZoomableImage";
import { ZoomableImage, type ZoomableImageHandle } from "./ZoomableImage";
interface FullScreenMediaViewerProps {
visible: boolean;
@ -32,6 +33,7 @@ export function FullScreenMediaViewer({
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);
@ -61,6 +63,24 @@ export function FullScreenMediaViewer({
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}
@ -69,11 +89,45 @@ export function FullScreenMediaViewer({
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}
@ -81,7 +135,7 @@ export function FullScreenMediaViewer({
accessibilityLabel="Close image viewer"
style={({ pressed }) => [styles.closeButton, pressed && styles.pressed]}
>
<Ionicons name="close" size={28} color={Colors.white} />
<Ionicons name="close" size={26} color={Colors.white} />
</Pressable>
</View>
@ -104,6 +158,13 @@ export function FullScreenMediaViewer({
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}
@ -118,14 +179,18 @@ export function FullScreenMediaViewer({
<Text numberOfLines={2} style={styles.caption}>
{mediaLabel(active, activeIndex)}
</Text>
<Text style={styles.hint}>Pinch or double-tap to zoom</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,
@ -136,6 +201,21 @@ const styles = StyleSheet.create({
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,

View File

@ -1,4 +1,11 @@
import React, { memo, useEffect, useMemo, useState } from "react";
import React, {
forwardRef,
memo,
useEffect,
useImperativeHandle,
useMemo,
useState,
} from "react";
import { ActivityIndicator, StyleSheet, Text, View } from "react-native";
import { Image } from "expo-image";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
@ -16,6 +23,13 @@ import { buildMediaImageSourceFromValues } from "./media-source";
const MIN_SCALE = 1;
const MAX_SCALE = 4;
const DOUBLE_TAP_SCALE = 2.5;
const SCALE_STEP = 0.75;
export interface ZoomableImageHandle {
zoomIn: () => void;
zoomOut: () => void;
resetZoom: () => void;
}
interface ZoomableImageProps {
item: ContentMediaItem;
@ -31,14 +45,18 @@ function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}
export const ZoomableImage = memo(function ZoomableImage({
export const ZoomableImage = memo(
forwardRef<ZoomableImageHandle, ZoomableImageProps>(function ZoomableImage(
{
item,
accessToken,
width,
height,
active,
onZoomStateChange,
}: ZoomableImageProps) {
},
ref,
) {
const scale = useSharedValue(1);
const savedScale = useSharedValue(1);
const translateX = useSharedValue(0);
@ -48,7 +66,6 @@ export const ZoomableImage = memo(function ZoomableImage({
const [loading, setLoading] = useState(true);
const [failed, setFailed] = useState(false);
const [isZoomed, setIsZoomed] = useState(false);
const source = useMemo(
() =>
@ -72,20 +89,48 @@ export const ZoomableImage = memo(function ZoomableImage({
}, [sourceUri]);
const notifyZoom = (zoomed: boolean) => {
setIsZoomed(zoomed);
onZoomStateChange?.(zoomed);
};
const reset = () => {
"worklet";
scale.value = withTiming(1);
scale.value = withTiming(1, { duration: 250 });
savedScale.value = 1;
translateX.value = withTiming(0);
translateY.value = withTiming(0);
translateX.value = withTiming(0, { duration: 250 });
translateY.value = withTiming(0, { duration: 250 });
savedX.value = 0;
savedY.value = 0;
};
useImperativeHandle(
ref,
() => ({
zoomIn: () => {
const next = Math.min(savedScale.value + SCALE_STEP, MAX_SCALE);
savedScale.value = next;
scale.value = withTiming(next, { duration: 250 });
notifyZoom(next > 1.01);
},
zoomOut: () => {
const next = Math.max(savedScale.value - SCALE_STEP, MIN_SCALE);
savedScale.value = next;
scale.value = withTiming(next, { duration: 250 });
if (next <= 1.01) {
translateX.value = withTiming(0, { duration: 250 });
translateY.value = withTiming(0, { duration: 250 });
savedX.value = 0;
savedY.value = 0;
notifyZoom(false);
}
},
resetZoom: () => {
reset();
notifyZoom(false);
},
}),
[],
);
useEffect(() => {
if (!active) {
scale.value = 1;
@ -94,7 +139,6 @@ export const ZoomableImage = memo(function ZoomableImage({
translateY.value = 0;
savedX.value = 0;
savedY.value = 0;
setIsZoomed(false);
onZoomStateChange?.(false);
}
}, [active, onZoomStateChange, savedScale, savedX, savedY, scale, translateX, translateY]);
@ -115,12 +159,12 @@ export const ZoomableImage = memo(function ZoomableImage({
});
const pan = Gesture.Pan()
.enabled(isZoomed)
.minDistance(8)
.maxPointers(1)
.onUpdate((event) => {
if (scale.value <= 1) return;
const maxX = ((scale.value - 1) * width) / 2;
const maxY = ((scale.value - 1) * height) / 2;
if (scale.value <= 1.01) return;
const maxX = Math.max(0, ((scale.value - 1) * width) / 2);
const maxY = Math.max(0, ((scale.value - 1) * height) / 2);
translateX.value = clamp(savedX.value + event.translationX, -maxX, maxX);
translateY.value = clamp(savedY.value + event.translationY, -maxY, maxY);
})
@ -138,13 +182,13 @@ export const ZoomableImage = memo(function ZoomableImage({
reset();
runOnJS(notifyZoom)(false);
} else {
scale.value = withTiming(DOUBLE_TAP_SCALE);
scale.value = withTiming(DOUBLE_TAP_SCALE, { duration: 250 });
savedScale.value = DOUBLE_TAP_SCALE;
runOnJS(notifyZoom)(true);
}
});
const gesture = Gesture.Race(doubleTap, Gesture.Simultaneous(pinch, pan));
const gesture = Gesture.Exclusive(doubleTap, Gesture.Simultaneous(pinch, pan));
const animatedStyle = useAnimatedStyle(() => ({
transform: [
@ -199,8 +243,9 @@ export const ZoomableImage = memo(function ZoomableImage({
</GestureDetector>
)}
</View>
);;
}, (previous, next) =>
);
}),
(previous, next) =>
previous.item.id === next.item.id &&
previous.item.url === next.item.url &&
previous.item.storage_key === next.item.storage_key &&
@ -209,7 +254,7 @@ export const ZoomableImage = memo(function ZoomableImage({
previous.width === next.width &&
previous.height === next.height &&
previous.active === next.active &&
previous.onZoomStateChange === next.onZoomStateChange
previous.onZoomStateChange === next.onZoomStateChange,
);
const styles = StyleSheet.create({
@ -222,7 +267,7 @@ const styles = StyleSheet.create({
imageWrap: { width: "100%", height: "100%" },
image: { width: "100%", height: "100%" },
loader: {
...StyleSheet.absoluteFillObject,
...(StyleSheet.absoluteFill as object),
zIndex: 2,
alignItems: "center",
justifyContent: "center",

View File

@ -82,7 +82,7 @@ export function mediaLabel(item: ContentMediaItem, index: number): string {
return (
item.alt_text?.trim() ||
item.caption?.trim() ||
item.usage_type.replace(/_/g, " ").trim() ||
(item.usage_type ? item.usage_type.replace(/_/g, " ").trim() : "") ||
`Image ${index + 1}`
);
}

View File

@ -5,6 +5,8 @@ import { ContentMediaGallery } from "../../media/ContentMediaGallery";
import { useRelationalContentMedia } from "../../media/ContentMediaContext";
import { asString } from "../helpers/value";
import type { ContentMediaItem } from "../../types/media.types";
export function ImageGallery({ images }: { images: Record<string, unknown>[] }) {
const relationalMedia = useRelationalContentMedia();
@ -15,22 +17,27 @@ export function ImageGallery({ images }: { images: Record<string, unknown>[] })
const visible = images.filter((item) => asString(item.image_path).trim().length > 0);
if (!visible.length) return null;
return (
<View style={styles.root}>
{visible.map((item, index) => {
const path = asString(item.image_path);
const name = asString(item.image_name);
return (
<View key={`${path}-${index}`} style={styles.item}>
<View style={styles.imageFrame}>
<Image source={{ uri: path }} contentFit="contain" style={styles.image} />
</View>
{name ? <Text style={styles.caption}>{name}</Text> : null}
</View>
);
})}
</View>
);
const fallbackMedia: ContentMediaItem[] = visible.map((item, index) => {
const path = asString(item.image_path).trim();
const name = asString(item.image_name).trim();
return {
id: `${path}-${index}`,
media_asset_id: `${path}-${index}`,
media_role: index === 0 ? "primary" : "gallery",
usage_type: "visual_reference",
sort_order: index,
caption: name || null,
alt_text: name || "Module image",
mime_type: "image/jpeg",
width: null,
height: null,
url: path,
storage_key: null,
requires_auth: false,
};
});
return <ContentMediaGallery media={fallbackMedia} title="Visual reference" />;
}
const styles = StyleSheet.create({