Skip to content

Commit 8e74e6f

Browse files
authored
fix: image gallery animation and wrong indexing (#3241)
* fix: image gallery animation and wrong indexing * fix: image gallery animation and wrong indexing
1 parent 37ddded commit 8e74e6f

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

package/src/components/ImageGallery/ImageGallery.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,8 @@ export const ImageGallery = (props: Props) => {
309309
}
310310
};
311311

312-
const newIndex = photos.findIndex(
313-
(photo) =>
314-
photo.messageId === selectedMessage?.messageId &&
315-
stripQueryFromUrl(photo.uri) === stripQueryFromUrl(selectedMessage?.url || ''),
316-
);
317-
318-
runOnUI(updatePosition)(newIndex);
319-
}, [selectedMessage, photos, index, translationX, fullWindowWidth]);
312+
runOnUI(updatePosition)(photoSelectedIndex);
313+
}, [fullWindowWidth, index, photoSelectedIndex, translationX]);
320314

321315
/**
322316
* Image heights are not provided and therefore need to be calculated.
@@ -389,7 +383,7 @@ export const ImageGallery = (props: Props) => {
389383
const pagerStyle = useAnimatedStyle<ImageStyle>(
390384
() => ({
391385
transform: [
392-
{ scaleX: -1 },
386+
{ scaleX: 1 },
393387
{
394388
translateX: translationX.value,
395389
},

package/src/components/ImageGallery/components/ImageGalleryFooter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export const ImageGalleryFooterWithContext = (props: ImageGalleryFooterPropsWith
205205
<View style={[styles.centerContainer, centerContainer]}>
206206
<Text style={[styles.imageCountText, { color: black }, imageCountText]}>
207207
{t('{{ index }} of {{ photoLength }}', {
208-
index: photoLength - selectedIndex,
208+
index: selectedIndex + 1,
209209
photoLength,
210210
})}
211211
</Text>

package/src/components/ImageGallery/hooks/useImageGalleryGestures.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Gesture, GestureType } from 'react-native-gesture-handler';
44
import {
55
cancelAnimation,
66
Easing,
7+
ReduceMotion,
78
runOnJS,
89
SharedValue,
910
useSharedValue,
@@ -242,8 +243,8 @@ export const useImageGalleryGestures = ({
242243
*/
243244
translateX.value =
244245
scale.value !== offsetScale.value
245-
? offsetX.value * localEvtScale - event.translationX
246-
: offsetX.value - event.translationX;
246+
? offsetX.value * localEvtScale + event.translationX
247+
: offsetX.value + event.translationX;
247248
translateY.value =
248249
isSwiping.value !== IsSwiping.TRUE
249250
? scale.value !== offsetScale.value
@@ -285,12 +286,14 @@ export const useImageGalleryGestures = ({
285286
* If there is a next photo, the image is lined up to the right
286287
* edge, the swipe is to the left, and the final position is more
287288
* than half the screen width, move to the next image
289+
*
290+
* As we move towards the left to move to next image, the translationX value will be negative on X axis.
288291
*/
289292
if (
290293
index < photoLength - 1 &&
291294
Math.abs(halfScreenWidth * (scale.value - 1) + offsetX.value) < 3 &&
292295
translateX.value < 0 &&
293-
finalXPosition < -halfScreenWidth &&
296+
finalXPosition > halfScreenWidth &&
294297
isSwiping.value === IsSwiping.TRUE
295298
) {
296299
cancelAnimation(translationX);
@@ -310,13 +313,15 @@ export const useImageGalleryGestures = ({
310313
/**
311314
* If there is a previous photo, the image is lined up to the left
312315
* edge, the swipe is to the right, and the final position is more
313-
* than half the screen width, move to the previous image
316+
* than half the screen width, move to the previous image.
317+
*
318+
* As we move towards the right to move to previous image, the translationX value will be positive on X axis.
314319
*/
315320
} else if (
316321
index > 0 &&
317322
Math.abs(-halfScreenWidth * (scale.value - 1) + offsetX.value) < 3 &&
318323
translateX.value > 0 &&
319-
finalXPosition > halfScreenWidth &&
324+
finalXPosition < -halfScreenWidth &&
320325
isSwiping.value === IsSwiping.TRUE
321326
) {
322327
cancelAnimation(translationX);
@@ -370,9 +375,11 @@ export const useImageGalleryGestures = ({
370375
*/
371376
translateY.value =
372377
currentImageHeight * scale.value < screenHeight
373-
? withTiming(0)
378+
? withTiming(0, { reduceMotion: ReduceMotion.Never })
374379
: translateY.value > (currentImageHeight / 2) * scale.value - halfScreenHeight
375-
? withTiming((currentImageHeight / 2) * scale.value - halfScreenHeight)
380+
? withTiming((currentImageHeight / 2) * scale.value - halfScreenHeight, {
381+
reduceMotion: ReduceMotion.Never,
382+
})
376383
: translateY.value < (-currentImageHeight / 2) * scale.value + halfScreenHeight
377384
? withTiming((-currentImageHeight / 2) * scale.value + halfScreenHeight)
378385
: withDecay({

0 commit comments

Comments
 (0)