@@ -3,6 +3,7 @@ import { StyleSheet, View, ViewStyle } from 'react-native';
33import type { StyleProp } from 'react-native' ;
44import Animated , { SharedValue } from 'react-native-reanimated' ;
55
6+ import { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext' ;
67import { useStateStore } from '../../../hooks/useStateStore' ;
78import {
89 isVideoPlayerAvailable ,
@@ -13,28 +14,26 @@ import {
1314 VideoType ,
1415} from '../../../native' ;
1516
17+ import {
18+ ImageGalleryAsset ,
19+ ImageGalleryState ,
20+ } from '../../../state-store/image-gallery-state-store' ;
1621import { VideoPlayerState } from '../../../state-store/video-player' ;
1722import { ONE_SECOND_IN_MILLISECONDS } from '../../../utils/constants' ;
1823import { Spinner } from '../../UIComponents/Spinner' ;
1924import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle' ;
2025import { useImageGalleryVideoPlayer } from '../hooks/useImageGalleryVideoPlayer' ;
21- import { Photo } from '../ImageGallery' ;
2226
2327const oneEighth = 1 / 8 ;
2428
2529export type AnimatedGalleryVideoType = {
2630 attachmentId : string ;
27- index : number ;
2831 offsetScale : SharedValue < number > ;
29- previous : boolean ;
3032 scale : SharedValue < number > ;
3133 screenHeight : number ;
32- selected : boolean ;
33- shouldRender : boolean ;
34- photo : Photo ;
34+ photo : ImageGalleryAsset ;
3535 translateX : SharedValue < number > ;
3636 translateY : SharedValue < number > ;
37- repeat ?: boolean ;
3837 style ?: StyleProp < ViewStyle > ;
3938} ;
4039
@@ -52,25 +51,18 @@ const videoPlayerSelector = (state: VideoPlayerState) => ({
5251 isPlaying : state . isPlaying ,
5352} ) ;
5453
54+ const imageGallerySelector = ( state : ImageGalleryState ) => ( {
55+ currentIndex : state . currentIndex ,
56+ } ) ;
57+
5558export const AnimatedGalleryVideo = React . memo (
5659 ( props : AnimatedGalleryVideoType ) => {
60+ const { imageGalleryStateStore } = useImageGalleryContext ( ) ;
5761 const [ opacity , setOpacity ] = useState < number > ( 1 ) ;
62+ const { currentIndex } = useStateStore ( imageGalleryStateStore . state , imageGallerySelector ) ;
5863
59- const {
60- attachmentId,
61- index,
62- offsetScale,
63- previous,
64- repeat,
65- scale,
66- screenHeight,
67- selected,
68- shouldRender,
69- style,
70- photo,
71- translateX,
72- translateY,
73- } = props ;
64+ const { attachmentId, offsetScale, scale, screenHeight, style, photo, translateX, translateY } =
65+ props ;
7466
7567 const videoRef = useRef < VideoType > ( null ) ;
7668
@@ -140,17 +132,21 @@ export const AnimatedGalleryVideo = React.memo(
140132 }
141133 } ;
142134
135+ const index = photo . index ;
136+
143137 const animatedStyles = useAnimatedGalleryStyle ( {
144138 index,
145139 offsetScale,
146- previous,
140+ previous : currentIndex > index ,
147141 scale,
148142 screenHeight,
149- selected,
143+ selected : currentIndex === index ,
150144 translateX,
151145 translateY,
152146 } ) ;
153147
148+ const shouldRender = Math . abs ( currentIndex - index ) < 4 ;
149+
154150 /**
155151 * An empty view is rendered for images not close to the currently
156152 * selected in order to maintain spacing while reducing the image
@@ -176,7 +172,7 @@ export const AnimatedGalleryVideo = React.memo(
176172 onPlaybackStatusUpdate = { onPlayBackStatusUpdate }
177173 onProgress = { onProgress }
178174 paused = { ! isPlaying }
179- repeat = { repeat }
175+ repeat = { true }
180176 resizeMode = 'contain'
181177 style = { style }
182178 testID = 'video-player'
@@ -205,15 +201,7 @@ export const AnimatedGalleryVideo = React.memo(
205201 } ,
206202
207203 ( prevProps , nextProps ) => {
208- if (
209- prevProps . repeat === nextProps . repeat &&
210- prevProps . shouldRender === nextProps . shouldRender &&
211- prevProps . screenHeight === nextProps . screenHeight &&
212- prevProps . selected === nextProps . selected &&
213- prevProps . previous === nextProps . previous &&
214- prevProps . index === nextProps . index &&
215- prevProps . photo === nextProps . photo
216- ) {
204+ if ( prevProps . screenHeight === nextProps . screenHeight && prevProps . photo === nextProps . photo ) {
217205 return true ;
218206 }
219207 return false ;
0 commit comments