Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/ImageViewing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Props = {
delayLongPress?: number;
HeaderComponent?: ComponentType<{ imageIndex: number }>;
FooterComponent?: ComponentType<{ imageIndex: number }>;
enableSlideShow?: boolean;
slideShowThreshold?: number;
};

const DEFAULT_ANIMATION_TYPE = "fade";
Expand All @@ -56,7 +58,7 @@ function ImageViewing({
imageIndex,
visible,
onRequestClose,
onLongPress = () => {},
onLongPress = () => { },
onImageIndexChange,
animationType = DEFAULT_ANIMATION_TYPE,
backgroundColor = DEFAULT_BG_COLOR,
Expand All @@ -66,18 +68,26 @@ function ImageViewing({
delayLongPress = DEFAULT_DELAY_LONG_PRESS,
HeaderComponent,
FooterComponent,
enableSlideShow = false,
slideShowThreshold = 3000
}: Props) {
const imageList = useRef<VirtualizedList<ImageSource>>(null);
const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose);
const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, SCREEN);
const [currentImageIndex, onScroll, scrollToNextImage] = useImageIndexChange(imageIndex, SCREEN, images);
const [headerTransform, footerTransform, toggleBarsVisible] =
useAnimatedComponents();

useEffect(() => {
if (onImageIndexChange) {
onImageIndexChange(currentImageIndex);
}
}, [currentImageIndex]);
if (enableSlideShow) {
const interval = setInterval(() => {
scrollToNextImage(imageList.current);
}, slideShowThreshold);
return () => clearInterval(interval);
}
}, [currentImageIndex, enableSlideShow]);

const onZoom = useCallback(
(isScaled: boolean) => {
Expand Down Expand Up @@ -148,8 +158,8 @@ function ImageViewing({
keyExtractor
? keyExtractor(imageSrc, index)
: typeof imageSrc === "number"
? `${imageSrc}`
: imageSrc.uri
? `${imageSrc}`
: imageSrc.uri
}
/>
{typeof FooterComponent !== "undefined" && (
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/useImageIndexChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NativeSyntheticEvent, NativeScrollEvent } from "react-native";

import { Dimensions } from "../@types";

const useImageIndexChange = (imageIndex: number, screen: Dimensions) => {
const useImageIndexChange = (imageIndex: number, screen: Dimensions,imagesArray: any) => {
const [currentImageIndex, setImageIndex] = useState(imageIndex);
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
const {
Expand All @@ -26,7 +26,15 @@ const useImageIndexChange = (imageIndex: number, screen: Dimensions) => {
}
};

return [currentImageIndex, onScroll] as const;
const scrollToNextImage = (imageListRef: any) => {
if (imageListRef) {
const nextIndex = currentImageIndex + 1 < imagesArray.length ? currentImageIndex + 1 : 0;
imageListRef.scrollToOffset({ offset: nextIndex * screen.width, animated: true });
setImageIndex(nextIndex);
}
};

return [currentImageIndex, onScroll,scrollToNextImage] as const;
};

export default useImageIndexChange;