-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathPlaybackContext.tsx
118 lines (103 loc) · 4.45 KB
/
PlaybackContext.tsx
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
import type {AVPlaybackStatusToSet} from 'expo-av';
import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import type {View} from 'react-native';
import type {VideoWithOnFullScreenUpdate} from '@components/VideoPlayer/types';
import useCurrentReportID from '@hooks/useCurrentReportID';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type {PlaybackContext, StatusCallback} from './types';
const Context = React.createContext<PlaybackContext | null>(null);
function PlaybackContextProvider({children}: ChildrenProps) {
const [currentlyPlayingURL, setCurrentlyPlayingURL] = useState<string | null>(null);
const [sharedElement, setSharedElement] = useState<View | HTMLDivElement | null>(null);
const [originalParent, setOriginalParent] = useState<View | HTMLDivElement | null>(null);
const currentVideoPlayerRef = useRef<VideoWithOnFullScreenUpdate | null>(null);
const {currentReportID} = useCurrentReportID() ?? {};
const videoResumeTryNumber = useRef<number>(0);
const pauseVideo = useCallback(() => {
currentVideoPlayerRef.current?.setStatusAsync?.({shouldPlay: false});
}, [currentVideoPlayerRef]);
const stopVideo = useCallback(() => {
currentVideoPlayerRef.current?.stopAsync?.();
}, [currentVideoPlayerRef]);
const playVideo = useCallback(() => {
currentVideoPlayerRef.current?.getStatusAsync?.().then((status) => {
const newStatus: AVPlaybackStatusToSet = {shouldPlay: true};
if ('durationMillis' in status && status.durationMillis === status.positionMillis) {
newStatus.positionMillis = 0;
}
currentVideoPlayerRef.current?.setStatusAsync(newStatus);
});
}, [currentVideoPlayerRef]);
const unloadVideo = useCallback(() => {
currentVideoPlayerRef.current?.unloadAsync?.();
}, [currentVideoPlayerRef]);
const updateCurrentlyPlayingURL = useCallback(
(url: string | null) => {
if (currentlyPlayingURL && url !== currentlyPlayingURL) {
pauseVideo();
}
setCurrentlyPlayingURL(url);
},
[currentlyPlayingURL, pauseVideo],
);
const shareVideoPlayerElements = useCallback(
(ref: VideoWithOnFullScreenUpdate | null, parent: View | HTMLDivElement | null, child: View | HTMLDivElement | null, isUploading: boolean) => {
currentVideoPlayerRef.current = ref;
setOriginalParent(parent);
setSharedElement(child);
// Prevents autoplay when uploading the attachment
if (!isUploading) {
playVideo();
}
},
[playVideo],
);
const checkVideoPlaying = useCallback(
(statusCallback: StatusCallback) => {
currentVideoPlayerRef.current?.getStatusAsync?.().then((status) => {
statusCallback('isPlaying' in status && status.isPlaying);
});
},
[currentVideoPlayerRef],
);
const resetVideoPlayerData = useCallback(() => {
videoResumeTryNumber.current = 0;
stopVideo();
setCurrentlyPlayingURL(null);
setSharedElement(null);
setOriginalParent(null);
currentVideoPlayerRef.current = null;
unloadVideo();
}, [stopVideo, unloadVideo]);
useEffect(() => {
if (!currentReportID) {
return;
}
resetVideoPlayerData();
}, [currentReportID, resetVideoPlayerData]);
const contextValue = useMemo(
() => ({
updateCurrentlyPlayingURL,
currentlyPlayingURL,
originalParent,
sharedElement,
currentVideoPlayerRef,
shareVideoPlayerElements,
playVideo,
pauseVideo,
checkVideoPlaying,
videoResumeTryNumber,
}),
[updateCurrentlyPlayingURL, currentlyPlayingURL, originalParent, sharedElement, shareVideoPlayerElements, playVideo, pauseVideo, checkVideoPlaying],
);
return <Context.Provider value={contextValue}>{children}</Context.Provider>;
}
function usePlaybackContext() {
const playbackContext = useContext(Context);
if (!playbackContext) {
throw new Error('usePlaybackContext must be used within a PlaybackContextProvider');
}
return playbackContext;
}
PlaybackContextProvider.displayName = 'PlaybackContextProvider';
export {Context as PlaybackContext, PlaybackContextProvider, usePlaybackContext};