import { PayloadAction, createAsyncThunk, createSlice } from '@reduxjs/toolkit'; import { Logger } from '../../utils/Log'; import { IP2PCall, IWebRTCState } from './types'; import { currentCallService } from '../../services/calls'; import { webRTCService } from '../../services/calls/webrtc'; import { permissionsService } from '../../services/Permissions'; import { PERMISSIONS, RESULTS } from 'react-native-permissions'; import { Strings } from '../../resources/localization/Strings'; import { Platform } from 'react-native'; const logger = new Logger('webrtcSlice'); const microphonePermission = Platform.OS === 'ios' ? PERMISSIONS.IOS.MICROPHONE : PERMISSIONS.ANDROID.RECORD_AUDIO; const cameraPermission = Platform.OS === 'ios' ? PERMISSIONS.IOS.CAMERA : PERMISSIONS.ANDROID.CAMERA; const initialState: IWebRTCState = { startingCall: false, p2pCall: undefined } export const startCall = createAsyncThunk('webrtc/startCall', async(callDate: { contactId: string; video: boolean; subject: string}) =>{ const { contactId, video, subject } = callDate; try { permissionsService.checkMultiPermissionRequest(permissionsService.appPermissions ) .then((result) => { console.log("permission",result); if (result === RESULTS.GRANTED) { logger.info("Permissions granted"); webRTCService.startCall(contactId, video, subject); } else { logger.info("Permissions not granted"); permissionsService.openSettingAlert( Strings.CallPermissionsTittle, Strings.CallPermissionsMsg ); } }) .catch((error) => { logger.error(`An error occurred during permissions check or call start:${error}`); }); } catch (error) { logger.error(`Synchronous error during call start process:${error}`); } }); export const takeCall = createAsyncThunk('webrtc/takeCall', async(withVideo: boolean)=>{ permissionsService.checkMultiPermissionRequest([microphonePermission, cameraPermission]).then((result) => { if (result === RESULTS.GRANTED) { webRTCService.takeCall(withVideo); } else { permissionsService.openSettingAlert( Strings.CallPermissionsTittle, Strings.CallPermissionsMsg ); } }); }); const webrtcSlice = createSlice({ name: 'webrtc', initialState, reducers: { getP2PCall(){ currentCallService.getCurrentCall(); }, setP2PCall(state, action: PayloadAction){ state.p2pCall = action.payload; }, rejectCall(){ webRTCService.rejectCall(); }, hangupCall(){ webRTCService.hangupCall(); }, switchCamera(){ webRTCService.switchCamera(); } }, extraReducers: builder =>{ builder.addCase(startCall.fulfilled, (state)=>{ state.startingCall = true; }), builder.addCase(takeCall.fulfilled, (state) =>{ logger.info("take an webrtc call") }) } }); // Export action creators and reducer export const { getP2PCall, setP2PCall, rejectCall, hangupCall, switchCamera } = webrtcSlice.actions; export default webrtcSlice.reducer;