77 AppState ,
88 AppStateStatus ,
99} from 'react-native' ;
10-
1110import {
1211 CometChatIncomingCall ,
1312 CometChatThemeProvider ,
@@ -23,64 +22,87 @@ import RootStackNavigator from './src/navigation/RootStackNavigator';
2322import { AppConstants } from './src/utils/AppConstants' ;
2423import {
2524 requestAndroidPermissions ,
25+ navigateToConversation ,
2626} from './src/utils/helper' ;
27+ import { navigationRef } from './src/navigation/NavigationService' ;
2728import AsyncStorage from '@react-native-async-storage/async-storage' ;
2829import { useActiveChat } from './src/utils/ActiveChatContext' ;
2930
31+ // Listener ID for registering and removing CometChat listeners.
3032const listenerId = 'app' ;
3133
3234const App = ( ) : React . ReactElement => {
33- const { activeChat} = useActiveChat ( ) ;
3435 const [ callReceived , setCallReceived ] = useState ( false ) ;
3536 const incomingCall = useRef < CometChat . Call | CometChat . CustomMessage | null > (
3637 null ,
3738 ) ;
3839 const [ isInitializing , setIsInitializing ] = useState ( true ) ;
3940 const [ isLoggedIn , setIsLoggedIn ] = useState ( false ) ;
4041 const [ userLoggedIn , setUserLoggedIn ] = useState ( false ) ;
42+ const [ hasValidAppCredentials , setHasValidAppCredentials ] = useState ( false ) ;
4143
4244 /**
43- * 1. Initial CometChat + UIKit Initialization
45+ * Initialize CometChat UIKit and configure Google Sign-In.
46+ * Retrieves credentials from AsyncStorage and uses fallback constants if needed.
4447 */
4548 useEffect ( ( ) => {
4649 async function init ( ) {
4750 try {
51+ // Retrieve stored app credentials or default to an empty object.
4852 const AppData = ( await AsyncStorage . getItem ( 'appCredentials' ) ) || '{}' ;
53+ const storedCredentials = JSON . parse ( AppData ) ;
54+
55+ // Determine the final credentials (from AsyncStorage or AppConstants).
56+ const finalAppId = storedCredentials . appId || AppConstants . appId ;
57+ const finalAuthKey = storedCredentials . authKey || AppConstants . authKey ;
58+ const finalRegion = storedCredentials . region || AppConstants . region ;
59+
60+ // Set hasValidAppCredentials based on whether all values are available.
61+ if ( finalAppId && finalAuthKey && finalRegion ) {
62+ setHasValidAppCredentials ( true ) ;
63+ } else {
64+ setHasValidAppCredentials ( false ) ;
65+ }
66+
4967 await CometChatUIKit . init ( {
50- appId : JSON . parse ( AppData ) . appId || AppConstants . appId ,
51- authKey : JSON . parse ( AppData ) . authKey || AppConstants . authKey ,
52- region : JSON . parse ( AppData ) . region || AppConstants . region ,
68+ appId : finalAppId ,
69+ authKey : finalAuthKey ,
70+ region : finalRegion ,
5371 subscriptionType : CometChat . AppSettings
5472 . SUBSCRIPTION_TYPE_ALL_USERS as UIKitSettings [ 'subscriptionType' ] ,
5573 } ) ;
5674
75+ // If a user is already logged in, update the state.
5776 const loggedInUser = CometChatUIKit . loggedInUser ;
5877 if ( loggedInUser ) {
5978 setIsLoggedIn ( true ) ;
6079 }
80+
6181 } catch ( error ) {
62- console . log ( 'CometChat init or getLoggedinUser failed: ' , error ) ;
82+ console . log ( 'Error during initialization ' , error ) ;
6383 } finally {
84+ // Mark initialization as complete.
6485 setIsInitializing ( false ) ;
6586 }
6687 }
6788 init ( ) ;
6889 } , [ ] ) ;
6990
7091 /**
71- * 2. Re-check user & possibly re-init or re-login if app resumes
92+ * Monitor app state changes to verify the logged-in status and clear notifications.
93+ * When the app becomes active, it cancels Android notifications and checks the login status.
7294 */
7395 useEffect ( ( ) => {
96+ if ( Platform . OS === 'android' ) {
97+ // Request required Android permissions for notifications.
98+ requestAndroidPermissions ( ) ;
99+ }
74100 const handleAppStateChange = async ( nextState : AppStateStatus ) => {
75101 if ( nextState === 'active' ) {
76102 try {
77- // Check if CometChat still has a valid logged in user
103+ // Verify if there is a valid logged- in user.
78104 const chatUser = await CometChat . getLoggedinUser ( ) ;
79- if ( ! chatUser ) {
80- setIsLoggedIn ( false ) ;
81- } else {
82- setIsLoggedIn ( true ) ;
83- }
105+ setIsLoggedIn ( ! ! chatUser ) ;
84106 } catch ( error ) {
85107 console . log ( 'Error verifying CometChat user on resume:' , error ) ;
86108 }
@@ -94,19 +116,10 @@ const App = (): React.ReactElement => {
94116 } , [ ] ) ;
95117
96118 /**
97- * 3. Handle inbound FCM messages in the foreground (Android).
119+ * Attach CometChat login listener to handle login and logout events.
120+ * Updates user login status accordingly.
98121 */
99122 useEffect ( ( ) => {
100- if ( Platform . OS === 'android' ) {
101- requestAndroidPermissions ( ) ;
102- }
103- } , [ activeChat ] ) ;
104-
105- /**
106- * 4. Attach CometChatLogin Listener and Call Listener
107- */
108- useEffect ( ( ) => {
109- // Login Listener
110123 CometChat . addLoginListener (
111124 listenerId ,
112125 new CometChat . LoginListener ( {
@@ -125,55 +138,62 @@ const App = (): React.ReactElement => {
125138 } ) ,
126139 ) ;
127140
141+ // Clean up the login listener on component unmount.
128142 return ( ) => {
129- // Clean up CometChat listeners
130143 CometChat . removeLoginListener ( listenerId ) ;
131144 } ;
132145 } , [ ] ) ;
133146
147+ /**
148+ * Attach CometChat call listeners to handle incoming, outgoing, and cancelled call events.
149+ * Also handles UI events for call end.
150+ */
134151 useEffect ( ( ) => {
135- // Call Listener
152+ // Listener for call events.
136153 CometChat . addCallListener (
137154 listenerId ,
138155 new CometChat . CallListener ( {
139156 onIncomingCallReceived : ( call : CometChat . Call ) => {
140- // Close bottomsheet for incoming call overlay
157+ // Hide any bottom sheet UI before showing the incoming call screen.
141158 CometChatUIEventHandler . emitUIEvent (
142159 CometChatUIEvents . ccToggleBottomSheet ,
143160 {
144161 isBottomSheetVisible : false ,
145162 } ,
146163 ) ;
164+ // Store the incoming call and update state.
147165 incomingCall . current = call ;
148166 setCallReceived ( true ) ;
149167 } ,
150168 onOutgoingCallRejected : ( ) => {
169+ // Clear the call state if outgoing call is rejected.
151170 incomingCall . current = null ;
152171 setCallReceived ( false ) ;
153172 } ,
154173 onIncomingCallCancelled : ( ) => {
174+ // Clear the call state if the incoming call is cancelled.
155175 incomingCall . current = null ;
156176 setCallReceived ( false ) ;
157177 } ,
158178 } ) ,
159179 ) ;
160180
181+ // Additional listener to handle call end events.
161182 CometChatUIEventHandler . addCallListener ( listenerId , {
162183 ccCallEnded : ( ) => {
163184 incomingCall . current = null ;
164185 setCallReceived ( false ) ;
165186 } ,
166187 } ) ;
167188
189+ // Remove call listeners on cleanup.
168190 return ( ) => {
169- // Clean up CometChat listeners
170191 CometChatUIEventHandler . removeCallListener ( listenerId ) ;
171192 CometChat . removeCallListener ( listenerId ) ;
172193 } ;
173194 } , [ userLoggedIn ] ) ;
174195
175-
176- // Show basic splash or blank screen while initializing
196+ // Show a blank/splash screen while the app is initializing.
177197 if ( isInitializing ) {
178198 return (
179199 < View
@@ -188,21 +208,26 @@ const App = (): React.ReactElement => {
188208 ) ;
189209 }
190210
211+ // Once initialization is complete, render the main app UI.
191212 return (
192213 < SafeAreaProvider >
193214 < CometChatThemeProvider >
194- { /* Only show incoming call UI if logged in + we have a call object */ }
215+ { /* Render the incoming call UI if the user is logged in and a call is received */ }
195216 { isLoggedIn && callReceived && incomingCall . current ? (
196217 < CometChatIncomingCall
197218 call = { incomingCall . current }
198219 onDecline = { ( ) => {
220+ // Handle call decline by clearing the incoming call state.
199221 incomingCall . current = null ;
200222 setCallReceived ( false ) ;
201223 } }
202224 />
203225 ) : null }
204- { /* Pass isLoggedIn to your main stack */ }
205- < RootStackNavigator isLoggedIn = { isLoggedIn } />
226+ { /* Render the main navigation stack, passing the login status as a prop */ }
227+ < RootStackNavigator
228+ isLoggedIn = { isLoggedIn }
229+ hasValidAppCredentials = { hasValidAppCredentials }
230+ />
206231 </ CometChatThemeProvider >
207232 </ SafeAreaProvider >
208233 ) ;
0 commit comments