-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathAnimatedGalleryVideo.tsx
More file actions
213 lines (194 loc) · 5.61 KB
/
AnimatedGalleryVideo.tsx
File metadata and controls
213 lines (194 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, { useState } from 'react';
import { StyleSheet, View, ViewStyle } from 'react-native';
import type { StyleProp } from 'react-native';
import Animated, { SharedValue } from 'react-native-reanimated';
import {
isVideoPlayerAvailable,
NativeHandlers,
PlaybackStatus,
VideoPayloadData,
VideoProgressData,
VideoType,
} from '../../../native';
import { Spinner } from '../../UIComponents/Spinner';
import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle';
const oneEighth = 1 / 8;
export type AnimatedGalleryVideoType = {
attachmentId: string;
handleEnd: () => void;
handleLoad: (index: string, duration: number) => void;
handleProgress: (index: string, progress: number, hasEnd?: boolean) => void;
index: number;
offsetScale: SharedValue<number>;
paused: boolean;
previous: boolean;
scale: SharedValue<number>;
screenHeight: number;
selected: boolean;
shouldRender: boolean;
source: { uri: string };
translateX: SharedValue<number>;
translateY: SharedValue<number>;
videoRef: React.RefObject<VideoType>;
repeat?: boolean;
style?: StyleProp<ViewStyle>;
};
const styles = StyleSheet.create({
activityIndicator: {
alignSelf: 'center',
},
videoPlayer: {
height: '100%',
width: '100%',
},
});
export const AnimatedGalleryVideo = React.memo(
(props: AnimatedGalleryVideoType) => {
const [opacity, setOpacity] = useState<number>(1);
const {
attachmentId,
handleEnd,
handleLoad,
handleProgress,
index,
offsetScale,
paused,
previous,
repeat,
scale,
screenHeight,
selected,
shouldRender,
source,
style,
translateX,
translateY,
videoRef,
} = props;
const onLoadStart = () => {
setOpacity(1);
};
const onLoad = (payload: VideoPayloadData) => {
setOpacity(0);
// Duration is in seconds so we convert to milliseconds.
handleLoad(attachmentId, payload.duration * 1000);
};
const onEnd = () => {
handleEnd();
};
const onProgress = (data: VideoProgressData) => {
handleProgress(attachmentId, data.currentTime / data.seekableDuration);
};
const onBuffer = ({ isBuffering }: { isBuffering: boolean }) => {
if (isBuffering) {
setOpacity(1);
} else {
setOpacity(0);
}
};
const onPlayBackStatusUpdate = (playbackStatus: PlaybackStatus) => {
if (!playbackStatus.isLoaded) {
// Update your UI for the unloaded state
setOpacity(1);
if (playbackStatus.error) {
console.error(`Encountered a fatal error during playback: ${playbackStatus.error}`);
}
} else {
// Update your UI for the loaded state
setOpacity(0);
handleLoad(attachmentId, playbackStatus.durationMillis);
if (playbackStatus.isPlaying) {
// Update your UI for the playing state
handleProgress(
attachmentId,
playbackStatus.positionMillis / playbackStatus.durationMillis,
);
}
if (playbackStatus.isBuffering) {
// Update your UI for the buffering state
setOpacity(1);
}
if (playbackStatus.didJustFinish && !playbackStatus.isLooping) {
// The player has just finished playing and will stop. Maybe you want to play something else?
handleEnd();
}
}
};
const animatedStyles = useAnimatedGalleryStyle({
index,
offsetScale,
previous,
scale,
screenHeight,
selected,
translateX,
translateY,
});
/**
* An empty view is rendered for images not close to the currently
* selected in order to maintain spacing while reducing the image
* load on memory.
*/
if (!shouldRender) {
return (
<View
accessibilityLabel='Empty View Image Gallery'
style={[style, { transform: [{ scale: oneEighth }] }]}
/>
);
}
return (
<Animated.View accessibilityLabel='Image Gallery Video' style={[...animatedStyles, style]}>
{isVideoPlayerAvailable() && NativeHandlers.Video ? (
<NativeHandlers.Video
onBuffer={onBuffer}
onEnd={onEnd}
onLoad={onLoad}
onLoadStart={onLoadStart}
onPlaybackStatusUpdate={onPlayBackStatusUpdate}
onProgress={onProgress}
paused={paused}
repeat={repeat}
resizeMode='contain'
style={style}
testID='video-player'
uri={source.uri}
videoRef={videoRef}
/>
) : null}
<Animated.View
accessibilityLabel='Spinner'
style={[
styles.activityIndicator,
{
opacity,
transform: [
{ scaleX: -1 },
{ translateY: -screenHeight * 4 },
{ scale: 1 / oneEighth },
],
},
]}
>
<Spinner height={40} width={40} />
</Animated.View>
</Animated.View>
);
},
(prevProps, nextProps) => {
if (
prevProps.paused === nextProps.paused &&
prevProps.repeat === nextProps.repeat &&
prevProps.shouldRender === nextProps.shouldRender &&
prevProps.source.uri === nextProps.source.uri &&
prevProps.screenHeight === nextProps.screenHeight &&
prevProps.selected === nextProps.selected &&
prevProps.previous === nextProps.previous &&
prevProps.index === nextProps.index
) {
return true;
}
return false;
},
);
AnimatedGalleryVideo.displayName = 'AnimatedGalleryVideo';