refactor: make video player function component

This commit is contained in:
Adrian Castro
2024-02-05 18:32:38 +01:00
parent 28126f612a
commit 61be1c37ac

View File

@@ -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 <VideoPlayer videoUrl={videoUrl} />;
}
interface VideoPlayerProps {
videoUrl: string;
}
class VideoPlayer extends Component<VideoPlayerProps, VideoPlayerState> {
private videoPlayer: React.RefObject<VideoRef>;
const VideoPlayer: React.FC<VideoPlayerProps> = ({ videoUrl }) => {
const [isLoading, setIsLoading] = useState(true);
const videoPlayer = useRef<VideoRef>(null);
constructor(props: VideoPlayerProps) {
super(props);
this.state = {
videoUrl: props.videoUrl || "",
fullscreen: true,
isLoading: true,
paused: false,
};
this.videoPlayer = React.createRef();
}
componentDidMount() {
useEffect(() => {
const lockOrientation = async () => {
await ScreenOrientation.lockAsync(
ScreenOrientation.OrientationLock.LANDSCAPE,
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,
ScreenOrientation.OrientationLock.PORTRAIT_UP
);
};
if (this.videoPlayer.current) {
this.videoPlayer.current.dismissFullscreenPlayer();
const presentFullscreenPlayer = async () => {
if (videoPlayer.current) {
videoPlayer.current.presentFullscreenPlayer();
await lockOrientation();
}
void unlockOrientation();
}
onVideoLoadStart = () => {
this.setState({ isLoading: true });
};
onReadyForDisplay = () => {
this.setState({ isLoading: false });
const dismissFullscreenPlayer = async () => {
if (videoPlayer.current) {
videoPlayer.current.dismissFullscreenPlayer();
await unlockOrientation();
}
};
// onVideoError = () => { // probably useful later
// console.log("Video playback error");
// };
setIsLoading(true);
void presentFullscreenPlayer();
return () => {
void dismissFullscreenPlayer();
};
}, [videoUrl]);
const onVideoLoadStart = () => {
setIsLoading(true);
};
const onReadyForDisplay = () => {
setIsLoading(false);
};
render() {
return (
<SafeAreaView style={styles.container}>
<Video
ref={this.videoPlayer}
source={{ uri: this.state.videoUrl }}
ref={videoPlayer}
source={{ uri: videoUrl }}
style={styles.fullScreen}
fullscreen={this.state.fullscreen}
paused={this.state.paused}
fullscreen={true}
paused={false}
controls={true}
onLoadStart={this.onVideoLoadStart}
onReadyForDisplay={this.onReadyForDisplay}
// onError={this.onVideoError}
onLoadStart={onVideoLoadStart}
onReadyForDisplay={onReadyForDisplay}
/>
{this.state.isLoading && (
<ActivityIndicator size="large" color="#0000ff" />
)}
{isLoading && <ActivityIndicator size="large" color="#0000ff" />}
</SafeAreaView>
);
}
}
};
const styles = StyleSheet.create({
// taken from example, probably needs to be nativewind stuff instead
container: {
flex: 1,
justifyContent: "center",