|
| 1 | + |
| 2 | +import React, { useEffect, useRef, useState } from 'react'; |
| 3 | +import { PermissionsAndroid, Platform, SafeAreaView, StatusBar, Text } from 'react-native'; |
| 4 | +import { CometChat } from "@cometchat/chat-sdk-react-native"; |
| 5 | +import { COMETCHAT_CONSTANTS } from './src/CONSTS'; |
| 6 | +import { CometChatContextProvider, CometChatConversationsWithMessages } from '@cometchat/chat-uikit-react-native'; |
| 7 | +import { CometChatTheme } from '@cometchat/chat-uikit-react-native'; |
| 8 | +import { CometChatUIKit } from '@cometchat/chat-uikit-react-native'; |
| 9 | +import StackNavigator from './src/StackNavigator'; |
| 10 | +import { UserContextProvider } from './UserContext'; |
| 11 | +import { CometChatIncomingCall } from '@cometchat/chat-uikit-react-native'; |
| 12 | +import { CometChatUIEventHandler } from '@cometchat/chat-uikit-react-native'; |
| 13 | +var listnerID = "UNIQUE_LISTENER_ID"; |
| 14 | + |
| 15 | +const App = () => { |
| 16 | + |
| 17 | + const getPermissions = () => { |
| 18 | + if (Platform.OS == "android") { |
| 19 | + PermissionsAndroid.requestMultiple([ |
| 20 | + PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, |
| 21 | + PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, |
| 22 | + PermissionsAndroid.PERMISSIONS.CAMERA, |
| 23 | + PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, |
| 24 | + ]); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const [callRecevied, setCallReceived] = useState(false); |
| 29 | + const incomingCall = useRef(null); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + getPermissions(); |
| 33 | + CometChatUIKit.init({ |
| 34 | + appId: COMETCHAT_CONSTANTS.APP_ID, |
| 35 | + authKey: COMETCHAT_CONSTANTS.AUTH_KEY, |
| 36 | + region: COMETCHAT_CONSTANTS.REGION, |
| 37 | + }) |
| 38 | + .then(() => { |
| 39 | + if (CometChat.setSource) { |
| 40 | + CometChat.setSource('ui-kit', Platform.OS, 'react-native'); |
| 41 | + } |
| 42 | + }) |
| 43 | + .catch(() => { |
| 44 | + return null; |
| 45 | + }); |
| 46 | + |
| 47 | + CometChat.addCallListener( |
| 48 | + listnerID, |
| 49 | + new CometChat.CallListener({ |
| 50 | + onIncomingCallReceived: (call) => { |
| 51 | + incomingCall.current = call; |
| 52 | + setCallReceived(true); |
| 53 | + }, |
| 54 | + onOutgoingCallRejected: (call) => { |
| 55 | + incomingCall.current = null; |
| 56 | + setCallReceived(false); |
| 57 | + }, |
| 58 | + onIncomingCallCancelled: (call) => { |
| 59 | + incomingCall.current = null; |
| 60 | + setCallReceived(false); |
| 61 | + } |
| 62 | + }) |
| 63 | + ); |
| 64 | + |
| 65 | + CometChatUIEventHandler.addCallListener(listnerID, { |
| 66 | + ccCallEnded: () => { |
| 67 | + incomingCall.current = null; |
| 68 | + setCallReceived(false); |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + return () => { |
| 73 | + CometChatUIEventHandler.removeCallListener(listnerID); |
| 74 | + CometChat.removeCallListener(listnerID) |
| 75 | + } |
| 76 | + |
| 77 | + }, []); |
| 78 | + |
| 79 | + return ( |
| 80 | + <SafeAreaView style={{ flex: 1 }}> |
| 81 | + <StatusBar backgroundColor={"white"} barStyle={"dark-content"} /> |
| 82 | + { |
| 83 | + callRecevied && |
| 84 | + <CometChatIncomingCall |
| 85 | + call={incomingCall.current} |
| 86 | + onDecline={(call) => { |
| 87 | + setCallReceived(false) |
| 88 | + }} |
| 89 | + incomingCallStyle={{ |
| 90 | + backgroundColor: 'white', |
| 91 | + titleColor: 'black', |
| 92 | + subtitleColor: 'gray', |
| 93 | + titleFont: { |
| 94 | + fontSize: 20, |
| 95 | + fontWeight: 'bold' |
| 96 | + } |
| 97 | + }} |
| 98 | + /> |
| 99 | + } |
| 100 | + <UserContextProvider> |
| 101 | + <CometChatContextProvider theme={new CometChatTheme({})}> |
| 102 | + <StackNavigator /> |
| 103 | + </CometChatContextProvider> |
| 104 | + </UserContextProvider> |
| 105 | + </SafeAreaView> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +export default App; |
0 commit comments