From c543a4632af56db57fb6d6541beab17c61c42485 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Tue, 16 Apr 2024 16:52:51 +0800 Subject: [PATCH] fix video play control broken after coming back from thread --- .../HTMLRenderers/VideoRenderer.tsx | 1 + .../VideoPlayerContexts/PlaybackContext.tsx | 20 ++++++++++++++++--- src/components/VideoPlayerContexts/types.ts | 1 + src/components/VideoPlayerPreview/index.tsx | 11 ++++++---- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/components/HTMLEngineProvider/HTMLRenderers/VideoRenderer.tsx b/src/components/HTMLEngineProvider/HTMLRenderers/VideoRenderer.tsx index 43d1be85d21a..6802a4518ba1 100644 --- a/src/components/HTMLEngineProvider/HTMLRenderers/VideoRenderer.tsx +++ b/src/components/HTMLEngineProvider/HTMLRenderers/VideoRenderer.tsx @@ -29,6 +29,7 @@ function VideoRenderer({tnode, key}: VideoRendererProps) { (null); function PlaybackContextProvider({children}: ChildrenProps) { const [currentlyPlayingURL, setCurrentlyPlayingURL] = useState(null); + const [currentlyPlayingURLReportID, setCurrentlyPlayingURLReportID] = useState(); const [sharedElement, setSharedElement] = useState(null); const [originalParent, setOriginalParent] = useState(null); const currentVideoPlayerRef = useRef(null); @@ -21,7 +22,7 @@ function PlaybackContextProvider({children}: ChildrenProps) { }, [currentVideoPlayerRef]); const stopVideo = useCallback(() => { - currentVideoPlayerRef.current?.stopAsync?.(); + currentVideoPlayerRef.current?.setStatusAsync?.({shouldPlay: false, positionMillis: 0}); }, [currentVideoPlayerRef]); const playVideo = useCallback(() => { @@ -43,9 +44,10 @@ function PlaybackContextProvider({children}: ChildrenProps) { if (currentlyPlayingURL && url !== currentlyPlayingURL) { pauseVideo(); } + setCurrentlyPlayingURLReportID(currentReportID); setCurrentlyPlayingURL(url); }, - [currentlyPlayingURL, pauseVideo], + [currentlyPlayingURL, currentReportID, pauseVideo], ); const shareVideoPlayerElements = useCallback( @@ -91,6 +93,7 @@ function PlaybackContextProvider({children}: ChildrenProps) { () => ({ updateCurrentlyPlayingURL, currentlyPlayingURL, + currentlyPlayingURLReportID, originalParent, sharedElement, currentVideoPlayerRef, @@ -101,7 +104,18 @@ function PlaybackContextProvider({children}: ChildrenProps) { checkVideoPlaying, videoResumeTryNumber, }), - [updateCurrentlyPlayingURL, currentlyPlayingURL, originalParent, sharedElement, shareVideoPlayerElements, playVideo, pauseVideo, checkVideoPlaying, setCurrentlyPlayingURL], + [ + updateCurrentlyPlayingURL, + currentlyPlayingURL, + currentlyPlayingURLReportID, + originalParent, + sharedElement, + shareVideoPlayerElements, + playVideo, + pauseVideo, + checkVideoPlaying, + setCurrentlyPlayingURL, + ], ); return {children}; } diff --git a/src/components/VideoPlayerContexts/types.ts b/src/components/VideoPlayerContexts/types.ts index e6a20ec090fe..ff8d9378caf7 100644 --- a/src/components/VideoPlayerContexts/types.ts +++ b/src/components/VideoPlayerContexts/types.ts @@ -9,6 +9,7 @@ import type CONST from '@src/CONST'; type PlaybackContext = { updateCurrentlyPlayingURL: (url: string | null) => void; currentlyPlayingURL: string | null; + currentlyPlayingURLReportID: string | undefined; originalParent: View | HTMLDivElement | null; sharedElement: View | HTMLDivElement | null; videoResumeTryNumber: MutableRefObject; diff --git a/src/components/VideoPlayerPreview/index.tsx b/src/components/VideoPlayerPreview/index.tsx index 923ab919a55a..414e95b0ff32 100644 --- a/src/components/VideoPlayerPreview/index.tsx +++ b/src/components/VideoPlayerPreview/index.tsx @@ -21,6 +21,9 @@ type VideoPlayerPreviewProps = { /** Url to a video. */ videoUrl: string; + /** reportID of the video */ + reportID: string; + /** Dimension of a video. */ videoDimensions: VideoDimensions; @@ -37,10 +40,10 @@ type VideoPlayerPreviewProps = { onShowModalPress: (event?: GestureResponderEvent | KeyboardEvent) => void | Promise; }; -function VideoPlayerPreview({videoUrl, thumbnailUrl, fileName, videoDimensions, videoDuration, onShowModalPress}: VideoPlayerPreviewProps) { +function VideoPlayerPreview({videoUrl, thumbnailUrl, reportID, fileName, videoDimensions, videoDuration, onShowModalPress}: VideoPlayerPreviewProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); - const {currentlyPlayingURL, updateCurrentlyPlayingURL} = usePlaybackContext(); + const {currentlyPlayingURL, currentlyPlayingURLReportID, updateCurrentlyPlayingURL} = usePlaybackContext(); const {isSmallScreenWidth} = useWindowDimensions(); const [isThumbnail, setIsThumbnail] = useState(true); const [measuredDimensions, setMeasuredDimensions] = useState(videoDimensions); @@ -60,11 +63,11 @@ function VideoPlayerPreview({videoUrl, thumbnailUrl, fileName, videoDimensions, }; useEffect(() => { - if (videoUrl !== currentlyPlayingURL) { + if (videoUrl !== currentlyPlayingURL || reportID !== currentlyPlayingURLReportID) { return; } setIsThumbnail(false); - }, [currentlyPlayingURL, updateCurrentlyPlayingURL, videoUrl]); + }, [currentlyPlayingURL, currentlyPlayingURLReportID, updateCurrentlyPlayingURL, videoUrl, reportID]); return (