From 61be1c37ac2356fcc3fa110188d55276ff2447ca Mon Sep 17 00:00:00 2001 From: Adrian Castro <22133246+castdrian@users.noreply.github.com> Date: Mon, 5 Feb 2024 18:32:38 +0100 Subject: [PATCH] refactor: make video player function component --- apps/expo/src/app/video-player.tsx | 153 +++++++++++++---------------- 1 file changed, 67 insertions(+), 86 deletions(-) diff --git a/apps/expo/src/app/video-player.tsx b/apps/expo/src/app/video-player.tsx index 7652df3..0ddefc2 100644 --- a/apps/expo/src/app/video-player.tsx +++ b/apps/expo/src/app/video-player.tsx @@ -1,18 +1,11 @@ import type { VideoRef } from "react-native-video"; -import React, { Component } from "react"; +import React, { useEffect, useRef, useState } from "react"; import { ActivityIndicator, StyleSheet } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import Video from "react-native-video"; import { useLocalSearchParams } from "expo-router"; import * as ScreenOrientation from "expo-screen-orientation"; -interface VideoPlayerState { - videoUrl: string; - fullscreen: boolean; - isLoading: boolean; - paused: boolean; -} - interface VideoPlayerProps { videoUrl: string; } @@ -22,87 +15,75 @@ export default function VideoPlayerWrapper() { const videoUrl = typeof params.videoUrl === "string" ? params.videoUrl : ""; return ; } - -class VideoPlayer extends Component { - private videoPlayer: React.RefObject; - - constructor(props: VideoPlayerProps) { - super(props); - this.state = { - videoUrl: props.videoUrl || "", - fullscreen: true, - isLoading: true, - paused: false, - }; - this.videoPlayer = React.createRef(); - } - - componentDidMount() { - const lockOrientation = async () => { - await ScreenOrientation.lockAsync( - ScreenOrientation.OrientationLock.LANDSCAPE, - ); - }; - - const { videoUrl } = this.props; - - this.setState({ videoUrl }, () => { - if (this.videoPlayer.current) { - this.videoPlayer.current.presentFullscreenPlayer(); - void lockOrientation(); - } - }); - } - - componentWillUnmount() { - const unlockOrientation = async () => { - await ScreenOrientation.lockAsync( - ScreenOrientation.OrientationLock.PORTRAIT_UP, - ); - }; - - if (this.videoPlayer.current) { - this.videoPlayer.current.dismissFullscreenPlayer(); - } - void unlockOrientation(); - } - - onVideoLoadStart = () => { - this.setState({ isLoading: true }); - }; - - onReadyForDisplay = () => { - this.setState({ isLoading: false }); - }; - - // onVideoError = () => { // probably useful later - // console.log("Video playback error"); - // }; - - render() { - return ( - - - ); - } +interface VideoPlayerProps { + videoUrl: string; } +const VideoPlayer: React.FC = ({ videoUrl }) => { + const [isLoading, setIsLoading] = useState(true); + const videoPlayer = useRef(null); + + useEffect(() => { + const lockOrientation = async () => { + await ScreenOrientation.lockAsync( + ScreenOrientation.OrientationLock.LANDSCAPE + ); + }; + + const unlockOrientation = async () => { + await ScreenOrientation.lockAsync( + ScreenOrientation.OrientationLock.PORTRAIT_UP + ); + }; + + const presentFullscreenPlayer = async () => { + if (videoPlayer.current) { + videoPlayer.current.presentFullscreenPlayer(); + await lockOrientation(); + } + }; + + const dismissFullscreenPlayer = async () => { + if (videoPlayer.current) { + videoPlayer.current.dismissFullscreenPlayer(); + await unlockOrientation(); + } + }; + + setIsLoading(true); + void presentFullscreenPlayer(); + + return () => { + void dismissFullscreenPlayer(); + }; + }, [videoUrl]); + + const onVideoLoadStart = () => { + setIsLoading(true); + }; + + const onReadyForDisplay = () => { + setIsLoading(false); + }; + + return ( + + + ); +}; + const styles = StyleSheet.create({ - // taken from example, probably needs to be nativewind stuff instead container: { flex: 1, justifyContent: "center",