sailfork / t3code-github / patches/ react-native-image-viewing@0.2.2.patch

diff --git a/dist/ImageViewing.js b/dist/ImageViewing.js
index 3d7641a9a94864415d207c678b71f8c592c90aa1..2b9ef77c3eb58cd284152ad22b2e711a91d50e7d 100644
--- a/dist/ImageViewing.js
+++ b/dist/ImageViewing.js
@@ -5,8 +5,8 @@
  * LICENSE file in the root directory of this source tree.
  *
  */
-import React, { useCallback, useRef, useEffect } from "react";
-import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, } from "react-native";
+import React, { useCallback, useRef, useEffect, useLayoutEffect } from "react";
+import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, Platform, useWindowDimensions, } from "react-native";
 import ImageItem from "./components/ImageItem/ImageItem";
 import ImageDefaultHeader from "./components/ImageDefaultHeader";
 import StatusBarManager from "./components/StatusBarManager";
@@ -17,23 +17,34 @@ const DEFAULT_ANIMATION_TYPE = "fade";
 const DEFAULT_BG_COLOR = "#000";
 const DEFAULT_DELAY_LONG_PRESS = 800;
 const SCREEN = Dimensions.get("screen");
-const SCREEN_WIDTH = SCREEN.width;
+const useViewingDimensions = Platform.OS === "android" ? useWindowDimensions : () => SCREEN;
 function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClose, onLongPress = () => { }, onImageIndexChange, animationType = DEFAULT_ANIMATION_TYPE, backgroundColor = DEFAULT_BG_COLOR, presentationStyle, swipeToCloseEnabled, doubleTapToZoomEnabled, delayLongPress = DEFAULT_DELAY_LONG_PRESS, HeaderComponent, FooterComponent, }) {
+    const viewport = useViewingDimensions();
+    const SCREEN_WIDTH = viewport.width;
+    const viewportKey = Platform.OS === "android" ? `${viewport.width}:${viewport.height}` : undefined;
     const imageList = useRef(null);
     const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose);
-    const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, SCREEN);
-    const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents();
+    const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, viewport);
+    const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents(viewportKey);
     useEffect(() => {
         if (onImageIndexChange) {
             onImageIndexChange(currentImageIndex);
         }
     }, [currentImageIndex]);
-    const onZoom = useCallback((isScaled) => {
+    const activeZoom = useRef(null);
+    const onZoom = useCallback(function handleZoom(isScaled) {
+        if (Platform.OS === "android" && activeZoom.current !== handleZoom) {
+            return;
+        }
         var _a, _b;
         // @ts-ignore
         (_b = (_a = imageList) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.setNativeProps({ scrollEnabled: !isScaled });
         toggleBarsVisible(!isScaled);
-    }, [imageList]);
+    }, [imageList, toggleBarsVisible]);
+    useLayoutEffect(() => {
+        activeZoom.current = onZoom;
+        return () => { activeZoom.current = null; };
+    }, [onZoom]);
     if (!visible) {
         return null;
     }
@@ -45,7 +56,8 @@ function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClos
         imageIndex: currentImageIndex,
     })) : (<ImageDefaultHeader onRequestClose={onRequestCloseEnhanced}/>)}
         </Animated.View>
-        <VirtualizedList ref={imageList} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={imageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({
+        {/* Reset Android image gestures on resize while preserving custom header state. */}
+        <VirtualizedList key={viewportKey} ref={imageList} onScroll={Platform.OS === "android" ? onScroll : undefined} scrollEventThrottle={Platform.OS === "android" ? 16 : undefined} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={currentImageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({
         length: SCREEN_WIDTH,
         offset: SCREEN_WIDTH * index,
         index,
diff --git a/dist/components/ImageItem/ImageItem.android.js b/dist/components/ImageItem/ImageItem.android.js
index 5fcaa9f9a7d893817fee89b85186ee3936118e69..e166dd14349b4b95f96d648cb64f464ed564c8b8 100644
--- a/dist/components/ImageItem/ImageItem.android.js
+++ b/dist/components/ImageItem/ImageItem.android.js
@@ -6,17 +6,17 @@
  *
  */
 import React, { useCallback, useRef, useState } from "react";
-import { Animated, ScrollView, Dimensions, StyleSheet, } from "react-native";
+import { Animated, ScrollView, Dimensions, View, } from "react-native";
 import useImageDimensions from "../../hooks/useImageDimensions";
 import usePanResponder from "../../hooks/usePanResponder";
 import { getImageStyles, getImageTransform } from "../../utils";
 import { ImageLoading } from "./ImageLoading";
 const SWIPE_CLOSE_OFFSET = 75;
 const SWIPE_CLOSE_VELOCITY = 1.75;
-const SCREEN = Dimensions.get("window");
-const SCREEN_WIDTH = SCREEN.width;
-const SCREEN_HEIGHT = SCREEN.height;
 const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled = true, doubleTapToZoomEnabled = true, }) => {
+    const SCREEN = Dimensions.get("window");
+    const SCREEN_WIDTH = SCREEN.width;
+    const SCREEN_HEIGHT = SCREEN.height;
     const imageContainer = useRef(null);
     const imageDimensions = useImageDimensions(imageSrc);
     const [translate, scale] = getImageTransform(imageDimensions, SCREEN);
@@ -28,10 +28,10 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
         onZoom(isZoomed);
         if ((_a = imageContainer) === null || _a === void 0 ? void 0 : _a.current) {
             imageContainer.current.setNativeProps({
-                scrollEnabled: !isZoomed,
+                scrollEnabled: swipeToCloseEnabled && !isZoomed,
             });
         }
-    }, [imageContainer]);
+    }, [imageContainer, onZoom, swipeToCloseEnabled]);
     const onLongPressHandler = useCallback(() => {
         onLongPress(imageSrc);
     }, [imageSrc, onLongPress]);
@@ -64,21 +64,15 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
         const offsetY = (_c = (_b = (_a = nativeEvent) === null || _a === void 0 ? void 0 : _a.contentOffset) === null || _b === void 0 ? void 0 : _b.y, (_c !== null && _c !== void 0 ? _c : 0));
         scrollValueY.setValue(offsetY);
     };
-    return (<ScrollView ref={imageContainer} style={styles.listItem} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={styles.imageScrollContainer} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
+    return (<ScrollView ref={imageContainer} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={{ height: SCREEN_HEIGHT * 2 }} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
         onScroll,
         onScrollEndDrag,
     })}>
-      <Animated.Image {...panHandlers} source={imageSrc} style={imageStylesWithOpacity} onLoad={onLoaded}/>
-      {(!isLoaded || !imageDimensions) && <ImageLoading />}
+      {/* Pinch from anywhere on screen, not only on the fitted image. */}
+      <View {...panHandlers} pointerEvents={isLoaded && imageDimensions ? "auto" : "none"} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }}>
+        <Animated.Image source={imageSrc} style={imageStylesWithOpacity} onLoad={onLoaded}/>
+        {(!isLoaded || !imageDimensions) && <ImageLoading />}
+      </View>
     </ScrollView>);
 };
-const styles = StyleSheet.create({
-    listItem: {
-        width: SCREEN_WIDTH,
-        height: SCREEN_HEIGHT,
-    },
-    imageScrollContainer: {
-        height: SCREEN_HEIGHT * 2,
-    },
-});
 export default React.memo(ImageItem);
diff --git a/dist/components/ImageItem/ImageLoading.js b/dist/components/ImageItem/ImageLoading.js
index 9770f7db158a2fb2b1c69a49e5fcbc89d8624603..995fab7524bb8673258d44e5eda361c62dae0324 100644
--- a/dist/components/ImageItem/ImageLoading.js
+++ b/dist/components/ImageItem/ImageLoading.js
@@ -6,25 +6,17 @@
  *
  */
 import React from "react";
-import { ActivityIndicator, Dimensions, StyleSheet, View } from "react-native";
+import { ActivityIndicator, Dimensions, Platform, StyleSheet, View } from "react-native";
 const SCREEN = Dimensions.get("screen");
-const SCREEN_WIDTH = SCREEN.width;
-const SCREEN_HEIGHT = SCREEN.height;
-export const ImageLoading = () => (<View style={styles.loading}>
+export const ImageLoading = () => {
+    const { width, height } = Platform.OS === "android" ? Dimensions.get("window") : SCREEN;
+    return (<View style={[styles.loading, { width, height }]}>
     <ActivityIndicator size="small" color="#FFF"/>
   </View>);
+};
 const styles = StyleSheet.create({
-    listItem: {
-        width: SCREEN_WIDTH,
-        height: SCREEN_HEIGHT,
-    },
     loading: {
-        width: SCREEN_WIDTH,
-        height: SCREEN_HEIGHT,
         alignItems: "center",
         justifyContent: "center",
     },
-    imageScrollContainer: {
-        height: SCREEN_HEIGHT,
-    },
 });
diff --git a/dist/hooks/useAnimatedComponents.d.ts b/dist/hooks/useAnimatedComponents.d.ts
index de5957458fea62f33743103acaa170a2c90d3638..dbafa8c5c5f3f86161e8520822cf10e19e88ea25 100644
--- a/dist/hooks/useAnimatedComponents.d.ts
+++ b/dist/hooks/useAnimatedComponents.d.ts
@@ -6,7 +6,7 @@
  *
  */
 import { Animated } from "react-native";
-declare const useAnimatedComponents: () => readonly [{
+declare const useAnimatedComponents: (resetKey?: string) => readonly [{
     [key: string]: Animated.Value;
 }[], {
     [key: string]: Animated.Value;
diff --git a/dist/hooks/useAnimatedComponents.js b/dist/hooks/useAnimatedComponents.js
index 4730fe40735bd80afc97abdd14150a2fb4abf3e5..0ed2100744ccc9bcee7f1b15b0328a729d47bb16 100644
--- a/dist/hooks/useAnimatedComponents.js
+++ b/dist/hooks/useAnimatedComponents.js
@@ -5,16 +5,19 @@
  * LICENSE file in the root directory of this source tree.
  *
  */
+import { useCallback, useMemo } from "react";
 import { Animated } from "react-native";
 const INITIAL_POSITION = { x: 0, y: 0 };
 const ANIMATION_CONFIG = {
     duration: 200,
     useNativeDriver: true,
 };
-const useAnimatedComponents = () => {
-    const headerTranslate = new Animated.ValueXY(INITIAL_POSITION);
-    const footerTranslate = new Animated.ValueXY(INITIAL_POSITION);
-    const toggleVisible = (isVisible) => {
+const useAnimatedComponents = (resetKey) => {
+    const [headerTranslate, footerTranslate] = useMemo(() => [
+        new Animated.ValueXY(INITIAL_POSITION),
+        new Animated.ValueXY(INITIAL_POSITION),
+    ], [resetKey]);
+    const toggleVisible = useCallback((isVisible) => {
         if (isVisible) {
             Animated.parallel([
                 Animated.timing(headerTranslate.y, { ...ANIMATION_CONFIG, toValue: 0 }),
@@ -33,7 +36,7 @@ const useAnimatedComponents = () => {
                 }),
             ]).start();
         }
-    };
+    }, [headerTranslate, footerTranslate]);
     const headerTransform = headerTranslate.getTranslateTransform();
     const footerTransform = footerTranslate.getTranslateTransform();
     return [headerTransform, footerTransform, toggleVisible];
diff --git a/dist/hooks/useImageIndexChange.js b/dist/hooks/useImageIndexChange.js
index cb5f0f798e5cfeedf459b7d22e92871db3092020..97ca2c9c326b1c53e6a5d90c3bb95d53342088d1 100644
--- a/dist/hooks/useImageIndexChange.js
+++ b/dist/hooks/useImageIndexChange.js
@@ -5,16 +5,34 @@
  * LICENSE file in the root directory of this source tree.
  *
  */
-import { useState } from "react";
+import { useCallback, useLayoutEffect, useRef, useState } from "react";
+import { Platform } from "react-native";
 const useImageIndexChange = (imageIndex, screen) => {
     const [currentImageIndex, setImageIndex] = useState(imageIndex);
-    const onScroll = (event) => {
+    const indexRef = useRef(imageIndex);
+    const activeScroll = useRef(null);
+    const onScroll = useCallback(function handleScroll(event) {
+        if (Platform.OS === "android") {
+            // Events at this width stay valid until commit; replaced or relaid-out lists do not.
+            if (activeScroll.current !== handleScroll ||
+                Math.abs(event.nativeEvent.layoutMeasurement.width - screen.width) > 1) {
+                return;
+            }
+        }
         const { nativeEvent: { contentOffset: { x: scrollX }, }, } = event;
         if (screen.width) {
             const nextIndex = Math.round(scrollX / screen.width);
-            setImageIndex(nextIndex < 0 ? 0 : nextIndex);
+            const boundedIndex = nextIndex < 0 ? 0 : nextIndex;
+            if (boundedIndex !== indexRef.current) {
+                indexRef.current = boundedIndex;
+                setImageIndex(boundedIndex);
+            }
         }
-    };
+    }, [screen]);
+    useLayoutEffect(() => {
+        activeScroll.current = onScroll;
+        return () => { activeScroll.current = null; };
+    }, [onScroll]);
     return [currentImageIndex, onScroll];
 };
 export default useImageIndexChange;
diff --git a/dist/hooks/usePanResponder.js b/dist/hooks/usePanResponder.js
index 99dcbc7efea4eba86a8bc2bad71dde73c8ebf1b4..7a7601eef4d330448d876b6460f53bd518a2c18c 100644
--- a/dist/hooks/usePanResponder.js
+++ b/dist/hooks/usePanResponder.js
@@ -8,14 +8,14 @@
 import { useMemo, useEffect } from "react";
 import { Animated, Dimensions, } from "react-native";
 import { createPanResponder, getDistanceBetweenTouches, getImageTranslate, getImageDimensionsByTranslate, } from "../utils";
-const SCREEN = Dimensions.get("window");
-const SCREEN_WIDTH = SCREEN.width;
-const SCREEN_HEIGHT = SCREEN.height;
-const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
 const SCALE_MAX = 2;
 const DOUBLE_TAP_DELAY = 300;
 const OUT_BOUND_MULTIPLIER = 0.75;
 const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, }) => {
+    const SCREEN = Dimensions.get("window");
+    const SCREEN_WIDTH = SCREEN.width;
+    const SCREEN_HEIGHT = SCREEN.height;
+    const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
     let numberInitialTouches = 1;
     let initialTouches = [];
     let currentScale = initialScale;
@@ -266,6 +266,9 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
                 tmpTranslate = null;
             }
         },
+        // A gesture taken over mid-pinch must commit like a release, or pans stay
+        // gated on the pre-pinch scale while the image still shows zoomed.
+        onTerminate: () => handlers.onRelease(),
     };
     const panResponder = useMemo(() => createPanResponder(handlers), [handlers]);
     return [panResponder.panHandlers, scaleValue, translateValue];