-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathApp.tsx
132 lines (120 loc) · 3.99 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { useEffect, useRef, useState } from 'react';
import { ActivityIndicator, PermissionsAndroid, Platform, SafeAreaView, StatusBar, StyleSheet, View } from 'react-native';
import { CometChat } from "@cometchat/chat-sdk-react-native";
import { AppConstants } from './AppConstants';
import { CometChatContextProvider, CometChatLocalize } from '@cometchat/chat-uikit-react-native';
import { CometChatTheme } from '@cometchat/chat-uikit-react-native';
import { CometChatUIKit } from '@cometchat/chat-uikit-react-native';
import StackNavigator from './src/StackNavigator';
import { UserContextProvider } from './UserContext';
import { CometChatIncomingCall } from '@cometchat/chat-uikit-react-native';
import { CometChatUIEventHandler } from '@cometchat/chat-uikit-react-native';
import { metaInfo } from './src/metaInfo';
var listnerID = "UNIQUE_LISTENER_ID";
const App = () => {
const getPermissions = () => {
if (Platform.OS == "android") {
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
]);
}
}
const [callReceived, setCallReceived] = useState(false);
const [isInitialized, setIsInitialized] = useState(false);
const incomingCall = useRef(null);
useEffect(() => {
getPermissions();
CometChatUIKit.init({
appId: AppConstants.APP_ID,
authKey: AppConstants.AUTH_KEY,
region: AppConstants.REGION,
})
.then(() => {
CometChatLocalize.setLocale("en");
try{CometChat.setDemoMetaInfo(metaInfo)}catch(err){}
if (CometChat.setSource) {
CometChat.setSource('ui-kit', Platform.OS, 'react-native');
}
setIsInitialized(true);
})
.catch(() => {
return null;
});
CometChat.addCallListener(
listnerID,
new CometChat.CallListener({
onIncomingCallReceived: (call) => {
incomingCall.current = call;
setCallReceived(true);
},
onOutgoingCallRejected: (call) => {
incomingCall.current = null;
setCallReceived(false);
},
onIncomingCallCancelled: (call) => {
incomingCall.current = null;
setCallReceived(false);
}
})
);
CometChatUIEventHandler.addCallListener(listnerID, {
ccCallEnded: () => {
incomingCall.current = null;
setCallReceived(false);
},
});
return () => {
CometChatUIEventHandler.removeCallListener(listnerID);
CometChat.removeCallListener(listnerID)
}
}, []);
return (
<View style={styles.container}>
{isInitialized ? (
<SafeAreaView style={{ flex: 1 }}>
<StatusBar backgroundColor={"white"} barStyle={"dark-content"} />
{callReceived && (
<CometChatIncomingCall
call={incomingCall.current}
onDecline={(call) => {
setCallReceived(false);
}}
onError={(error) => {
setCallReceived(false);
}}
incomingCallStyle={{
backgroundColor: "white",
titleColor: "black",
subtitleColor: "gray",
titleFont: {
fontSize: 20,
fontWeight: "bold",
},
}}
/>
)}
<UserContextProvider>
<CometChatContextProvider theme={new CometChatTheme({})}>
<StackNavigator />
</CometChatContextProvider>
</UserContextProvider>
</SafeAreaView>
) : (
<View style={[styles.container, {justifyContent: "center"}]}>
<ActivityIndicator />
</View>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff"
}
})
export default App;