-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathPlaybackContext.tsx
114 lines (99 loc) · 4.08 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
import type {AVPlaybackStatusToSet, Video} from 'expo-av';
import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import type {View} from 'react-native';
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 | null>(null);
const [originalParent, setOriginalParent] = useState<View | null>(null);
const currentVideoPlayerRef = useRef<Video | null>(null);
const {currentReportID} = useCurrentReportID() ?? {};
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) => {
if (currentlyPlayingURL && url !== currentlyPlayingURL) {
pauseVideo();
}
setCurrentlyPlayingURL(url);
},
[currentlyPlayingURL, pauseVideo],
);
const shareVideoPlayerElements = useCallback(
(ref: Video, parent: View, child: View, 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(() => {
stopVideo();
unloadVideo();
setCurrentlyPlayingURL(null);
setSharedElement(null);
setOriginalParent(null);
currentVideoPlayerRef.current = null;
}, [stopVideo, unloadVideo]);
useEffect(() => {
if (!currentReportID) {
return;
}
resetVideoPlayerData();
}, [currentReportID, resetVideoPlayerData]);
const contextValue = useMemo(
() => ({
updateCurrentlyPlayingURL,
currentlyPlayingURL,
originalParent,
sharedElement,
currentVideoPlayerRef,
shareVideoPlayerElements,
playVideo,
pauseVideo,
checkVideoPlaying,
}),
[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 {PlaybackContextProvider, usePlaybackContext};