import React, { useEffect, useRef, useCallback } from 'react';
import { View, StyleSheet, AppState, AppStateStatus } from 'react-native';
import { CometChatCalls } from '@cometchat/calls-sdk-react-native';
interface CallScreenProps {
callToken: string;
callSettings: any;
onCallEnd: () => void;
}
function CallScreen({ callToken, callSettings, onCallEnd }: CallScreenProps) {
const appState = useRef(AppState.currentState);
const isInCall = useRef(true);
// Handle app state changes
useEffect(() => {
const subscription = AppState.addEventListener(
'change',
(nextAppState: AppStateStatus) => {
if (!isInCall.current) return;
if (
appState.current.match(/active/) &&
nextAppState === 'background'
) {
// Going to background - enable PiP for video calls
CometChatCalls.enablePictureInPictureLayout();
} else if (
appState.current === 'background' &&
nextAppState === 'active'
) {
// Coming to foreground - disable PiP
CometChatCalls.disablePictureInPictureLayout();
}
appState.current = nextAppState;
}
);
return () => {
subscription.remove();
};
}, []);
// Handle connection events
useEffect(() => {
const unsubscribeLost = CometChatCalls.addEventListener(
'onConnectionLost',
() => {
console.log('Connection lost');
// Show reconnecting UI
}
);
const unsubscribeRestored = CometChatCalls.addEventListener(
'onConnectionRestored',
() => {
console.log('Connection restored');
// Hide reconnecting UI
}
);
const unsubscribeClosed = CometChatCalls.addEventListener(
'onConnectionClosed',
() => {
console.log('Connection closed');
isInCall.current = false;
onCallEnd();
}
);
return () => {
unsubscribeLost();
unsubscribeRestored();
unsubscribeClosed();
};
}, [onCallEnd]);
// Cleanup on unmount
useEffect(() => {
return () => {
isInCall.current = false;
};
}, []);
return (
<View style={styles.container}>
<CometChatCalls.Component
callToken={callToken}
callSettings={callSettings}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
},
});
export default CallScreen;