mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-01-15 15:48:05 +00:00
Co-authored-by: Alex Kim <alexkim@Alexs-MacBook-Pro.local> Co-authored-by: Alex <111128610+Alexk2309@users.noreply.github.com> Co-authored-by: Simon-Eklundh <simon.eklundh@proton.me>
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import { Feather } from "@expo/vector-icons";
|
|
import { useCallback, useEffect } from "react";
|
|
import { Platform } from "react-native";
|
|
import { Pressable } from "react-native-gesture-handler";
|
|
import GoogleCast, {
|
|
CastButton,
|
|
CastContext,
|
|
useCastDevice,
|
|
useDevices,
|
|
useMediaStatus,
|
|
useRemoteMediaClient,
|
|
} from "react-native-google-cast";
|
|
import { RoundButton } from "./RoundButton";
|
|
|
|
export function Chromecast({
|
|
width = 48,
|
|
height = 48,
|
|
background = "transparent",
|
|
...props
|
|
}) {
|
|
const client = useRemoteMediaClient();
|
|
const castDevice = useCastDevice();
|
|
const devices = useDevices();
|
|
const sessionManager = GoogleCast.getSessionManager();
|
|
const discoveryManager = GoogleCast.getDiscoveryManager();
|
|
const mediaStatus = useMediaStatus();
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
if (!discoveryManager) {
|
|
console.warn("DiscoveryManager is not initialized");
|
|
return;
|
|
}
|
|
|
|
await discoveryManager.startDiscovery();
|
|
})();
|
|
}, [client, devices, castDevice, sessionManager, discoveryManager]);
|
|
|
|
// Android requires the cast button to be present for startDiscovery to work
|
|
const AndroidCastButton = useCallback(
|
|
() =>
|
|
Platform.OS === "android" ? <CastButton tintColor='transparent' /> : null,
|
|
[Platform.OS],
|
|
);
|
|
|
|
if (Platform.OS === "ios") {
|
|
return (
|
|
<Pressable
|
|
className='mr-4'
|
|
onPress={() => {
|
|
if (mediaStatus?.currentItemId) CastContext.showExpandedControls();
|
|
else CastContext.showCastDialog();
|
|
}}
|
|
{...props}
|
|
>
|
|
<AndroidCastButton />
|
|
<Feather name='cast' size={22} color={"white"} />
|
|
</Pressable>
|
|
);
|
|
}
|
|
|
|
if (background === "transparent")
|
|
return (
|
|
<RoundButton
|
|
size='large'
|
|
className='mr-2'
|
|
background={false}
|
|
onPress={() => {
|
|
if (mediaStatus?.currentItemId) CastContext.showExpandedControls();
|
|
else CastContext.showCastDialog();
|
|
}}
|
|
{...props}
|
|
>
|
|
<AndroidCastButton />
|
|
<Feather name='cast' size={22} color={"white"} />
|
|
</RoundButton>
|
|
);
|
|
|
|
return (
|
|
<RoundButton
|
|
size='large'
|
|
onPress={() => {
|
|
if (mediaStatus?.currentItemId) CastContext.showExpandedControls();
|
|
else CastContext.showCastDialog();
|
|
}}
|
|
{...props}
|
|
>
|
|
<AndroidCastButton />
|
|
<Feather name='cast' size={22} color={"white"} />
|
|
</RoundButton>
|
|
);
|
|
}
|