refactor: playing state

This commit is contained in:
Fredrik Burmester
2024-08-20 08:24:05 +02:00
parent 1c31458dd4
commit 469e8b3f01
5 changed files with 392 additions and 264 deletions

View File

@@ -4,24 +4,27 @@ import { Feather, Ionicons } from "@expo/vector-icons";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { View } from "react-native";
import { Button } from "./Button";
import CastContext, {
PlayServicesState,
useRemoteMediaClient,
} from "react-native-google-cast";
import { usePlayback } from "@/providers/PlaybackProvider";
interface Props extends React.ComponentProps<typeof Button> {
item: BaseItemDto;
onPress: (type?: "cast" | "device") => void;
chromecastReady: boolean;
item?: BaseItemDto | null;
url?: string | null;
}
export const PlayButton: React.FC<Props> = ({
item,
onPress,
chromecastReady,
...props
}) => {
export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
const { showActionSheetWithOptions } = useActionSheet();
const client = useRemoteMediaClient();
const { currentlyPlaying, setCurrentlyPlayingState } = usePlayback();
const _onPress = () => {
if (!chromecastReady) {
onPress("device");
const onPress = async () => {
if (!url || !item) return;
if (!client) {
setCurrentlyPlayingState({ item, url });
return;
}
@@ -33,28 +36,45 @@ export const PlayButton: React.FC<Props> = ({
options,
cancelButtonIndex,
},
(selectedIndex: number | undefined) => {
async (selectedIndex: number | undefined) => {
switch (selectedIndex) {
case 0:
onPress("cast");
await CastContext.getPlayServicesState().then((state) => {
if (state && state !== PlayServicesState.SUCCESS)
CastContext.showPlayServicesErrorDialog(state);
else {
client.loadMedia({
mediaInfo: {
contentUrl: url,
contentType: "video/mp4",
metadata: {
type: item.Type === "Episode" ? "tvShow" : "movie",
title: item.Name || "",
subtitle: item.Overview || "",
},
},
startTime: 0,
});
}
});
break;
case 1:
onPress("device");
setCurrentlyPlayingState({ item, url });
break;
case cancelButtonIndex:
break;
}
},
}
);
};
return (
<Button
onPress={_onPress}
onPress={onPress}
iconRight={
<View className="flex flex-row items-center space-x-2">
<Ionicons name="play-circle" size={24} color="white" />
{chromecastReady && <Feather name="cast" size={22} color="white" />}
{client && <Feather name="cast" size={22} color="white" />}
</View>
}
{...props}