import React, { useState, useEffect } from 'react';
import { View, TouchableOpacity, Text, StyleSheet, AppState, Platform } from 'react-native';
import { CometChatCalls } from '@cometchat/calls-sdk-react-native';
function PictureInPictureControls() {
const [isPiPEnabled, setIsPiPEnabled] = useState(false);
const [appState, setAppState] = useState(AppState.currentState);
useEffect(() => {
const unsubscribeEnabled = CometChatCalls.addEventListener(
'onPictureInPictureLayoutEnabled',
() => {
setIsPiPEnabled(true);
}
);
const unsubscribeDisabled = CometChatCalls.addEventListener(
'onPictureInPictureLayoutDisabled',
() => {
setIsPiPEnabled(false);
}
);
// Listen for app state changes
const subscription = AppState.addEventListener('change', (nextAppState) => {
if (appState.match(/active/) && nextAppState === 'background') {
// App going to background - enable PiP
CometChatCalls.enablePictureInPictureLayout();
}
setAppState(nextAppState);
});
return () => {
unsubscribeEnabled();
unsubscribeDisabled();
subscription.remove();
};
}, [appState]);
const togglePiP = () => {
if (isPiPEnabled) {
CometChatCalls.disablePictureInPictureLayout();
} else {
CometChatCalls.enablePictureInPictureLayout();
}
};
return (
<TouchableOpacity
style={[styles.button, isPiPEnabled && styles.activeButton]}
onPress={togglePiP}
>
<Text style={styles.buttonText}>
{isPiPEnabled ? 'Exit PiP' : 'Enter PiP'}
</Text>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
button: {
backgroundColor: '#333',
paddingHorizontal: 16,
paddingVertical: 12,
borderRadius: 8,
},
activeButton: {
backgroundColor: '#6851D6',
},
buttonText: {
color: '#fff',
fontSize: 14,
fontWeight: '600',
},
});
export default PictureInPictureControls;