Skip to content

Commit

Permalink
Merge pull request #41054 from KMichel1030/fix/issue-40647
Browse files Browse the repository at this point in the history
  • Loading branch information
blimpich authored Apr 30, 2024
2 parents 421581d + 9415778 commit 4d9e102
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/components/VideoPlayer/BaseVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Hoverable from '@components/Hoverable';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import {useFullScreenContext} from '@components/VideoPlayerContexts/FullScreenContext';
import {usePlaybackContext} from '@components/VideoPlayerContexts/PlaybackContext';
import {useVideoPopoverMenuContext} from '@components/VideoPlayerContexts/VideoPopoverMenuContext';
import {useVolumeContext} from '@components/VideoPlayerContexts/VolumeContext';
import VideoPopoverMenu from '@components/VideoPopoverMenu';
import useNetwork from '@hooks/useNetwork';
Expand Down Expand Up @@ -79,6 +80,7 @@ function BaseVideoPlayer({
const isUploading = CONST.ATTACHMENT_LOCAL_URL_PREFIX.some((prefix) => url.startsWith(prefix));
const videoStateRef = useRef<AVPlaybackStatus | null>(null);
const {updateVolume} = useVolumeContext();
const {videoPopoverMenuPlayerRef} = useVideoPopoverMenuContext();

const togglePlayCurrentVideo = useCallback(() => {
videoResumeTryNumber.current = 0;
Expand All @@ -93,6 +95,7 @@ function BaseVideoPlayer({

const showPopoverMenu = (event?: GestureResponderEvent | KeyboardEvent) => {
setIsPopoverVisible(true);
videoPopoverMenuPlayerRef.current = videoPlayerRef.current;
if (!event || !('nativeEvent' in event)) {
return;
}
Expand Down
16 changes: 11 additions & 5 deletions src/components/VideoPlayerContexts/VideoPopoverMenuContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useCallback, useContext, useMemo, useState} from 'react';
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
import * as Expensicons from '@components/Icon/Expensicons';
import type {PopoverMenuItem} from '@components/PopoverMenu';
import type {VideoWithOnFullScreenUpdate} from '@components/VideoPlayer/types';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
Expand All @@ -18,6 +19,7 @@ function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
const [currentPlaybackSpeed, setCurrentPlaybackSpeed] = useState<PlaybackSpeed>(CONST.VIDEO_PLAYER.PLAYBACK_SPEEDS[2]);
const {isOffline} = useNetwork();
const isLocalFile = currentlyPlayingURL && CONST.ATTACHMENT_LOCAL_URL_PREFIX.some((prefix) => currentlyPlayingURL.startsWith(prefix));
const videoPopoverMenuPlayerRef = useRef<VideoWithOnFullScreenUpdate | null>(null);

const updatePlaybackSpeed = useCallback(
(speed: PlaybackSpeed) => {
Expand All @@ -28,12 +30,16 @@ function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
);

const downloadAttachment = useCallback(() => {
if (currentlyPlayingURL === null) {
if (videoPopoverMenuPlayerRef.current === null) {
return;
}
const sourceURI = addEncryptedAuthTokenToURL(currentlyPlayingURL);
const {source} = videoPopoverMenuPlayerRef.current?.props ?? {};
if (typeof source === 'number' || !source) {
return;
}
const sourceURI = addEncryptedAuthTokenToURL(source.uri);
fileDownload(sourceURI);
}, [currentlyPlayingURL]);
}, [videoPopoverMenuPlayerRef]);

const menuItems = useMemo(() => {
const items: PopoverMenuItem[] = [];
Expand Down Expand Up @@ -63,7 +69,7 @@ function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
return items;
}, [currentPlaybackSpeed, downloadAttachment, translate, updatePlaybackSpeed, isOffline, isLocalFile]);

const contextValue = useMemo(() => ({menuItems, updatePlaybackSpeed}), [menuItems, updatePlaybackSpeed]);
const contextValue = useMemo(() => ({menuItems, videoPopoverMenuPlayerRef, updatePlaybackSpeed}), [menuItems, videoPopoverMenuPlayerRef, updatePlaybackSpeed]);
return <Context.Provider value={contextValue}>{children}</Context.Provider>;
}

Expand Down
1 change: 1 addition & 0 deletions src/components/VideoPlayerContexts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type VolumeContext = {

type VideoPopoverMenuContext = {
menuItems: PopoverMenuItem[];
videoPopoverMenuPlayerRef: MutableRefObject<VideoWithOnFullScreenUpdate | null>;
updatePlaybackSpeed: (speed: PlaybackSpeed) => void;
};

Expand Down

0 comments on commit 4d9e102

Please sign in to comment.