Skip to content

Commit 94b1b40

Browse files
committed
fix: image gallery animation and wrong indexing
1 parent 1ba131b commit 94b1b40

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-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: 15 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
@@ -280,17 +281,20 @@ export const useImageGalleryGestures = ({
280281
*/
281282
const finalXPosition = event.translationX - event.velocityX * 0.3;
282283
const finalYPosition = event.translationY + event.velocityY * 0.1;
284+
console.log(finalXPosition);
283285

284286
/**
285287
* If there is a next photo, the image is lined up to the right
286288
* edge, the swipe is to the left, and the final position is more
287289
* than half the screen width, move to the next image
290+
*
291+
* As we move towards the left to move to next image, the translationX value will be negative on X axis.
288292
*/
289293
if (
290294
index < photoLength - 1 &&
291295
Math.abs(halfScreenWidth * (scale.value - 1) + offsetX.value) < 3 &&
292296
translateX.value < 0 &&
293-
finalXPosition < -halfScreenWidth &&
297+
finalXPosition > halfScreenWidth &&
294298
isSwiping.value === IsSwiping.TRUE
295299
) {
296300
cancelAnimation(translationX);
@@ -310,13 +314,15 @@ export const useImageGalleryGestures = ({
310314
/**
311315
* If there is a previous photo, the image is lined up to the left
312316
* edge, the swipe is to the right, and the final position is more
313-
* than half the screen width, move to the previous image
317+
* than half the screen width, move to the previous image.
318+
*
319+
* As we move towards the right to move to previous image, the translationX value will be positive on X axis.
314320
*/
315321
} else if (
316322
index > 0 &&
317323
Math.abs(-halfScreenWidth * (scale.value - 1) + offsetX.value) < 3 &&
318324
translateX.value > 0 &&
319-
finalXPosition > halfScreenWidth &&
325+
finalXPosition < -halfScreenWidth &&
320326
isSwiping.value === IsSwiping.TRUE
321327
) {
322328
cancelAnimation(translationX);
@@ -370,9 +376,11 @@ export const useImageGalleryGestures = ({
370376
*/
371377
translateY.value =
372378
currentImageHeight * scale.value < screenHeight
373-
? withTiming(0)
379+
? withTiming(0, { reduceMotion: ReduceMotion.Never })
374380
: translateY.value > (currentImageHeight / 2) * scale.value - halfScreenHeight
375-
? withTiming((currentImageHeight / 2) * scale.value - halfScreenHeight)
381+
? withTiming((currentImageHeight / 2) * scale.value - halfScreenHeight, {
382+
reduceMotion: ReduceMotion.Never,
383+
})
376384
: translateY.value < (-currentImageHeight / 2) * scale.value + halfScreenHeight
377385
? withTiming((-currentImageHeight / 2) * scale.value + halfScreenHeight)
378386
: withDecay({

0 commit comments

Comments
 (0)