From e30ed959d788d0a66a959c8135540878aabbf3fe Mon Sep 17 00:00:00 2001 From: Lee-Sunho Date: Sun, 1 Dec 2024 15:50:16 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/App.tsx | 92 +++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/app/App.tsx b/app/App.tsx index ce31120..ab8edf0 100644 --- a/app/App.tsx +++ b/app/App.tsx @@ -7,10 +7,11 @@ import {useEffect, useRef, useState} from 'react'; import SplashScreen from 'react-native-splash-screen'; import AsyncStorage from '@react-native-async-storage/async-storage'; import analytics from '@react-native-firebase/analytics'; +import {userStore} from 'store/Store'; function App(): React.JSX.Element { const [isInitializing, setIsInitializing] = useState(true); - const [isLoggedIn, setIsLoggedIn] = useState(false); + const {isLoggedIn, setIsLoggedIn} = userStore(); const appStateRef = useRef(AppState.currentState); const sessionStartTimeRef = useRef(Date.now()); @@ -20,61 +21,56 @@ function App(): React.JSX.Element { setIsInitializing(false); }; - useEffect(() => { - const logInitialSession = async () => { - try { - await analytics().logEvent('bv_session_start', { + const logInitialSession = async () => { + try { + await analytics().logEvent('bv_session_start', { + timestamp: new Date().toISOString(), + is_logged_in: isLoggedIn, + }); + } catch (error) { + console.error('Analytics error:', error); + } + }; + + const subscription = AppState.addEventListener('change', async nextAppState => { + try { + const currentState = appStateRef.current; + appStateRef.current = nextAppState; + + if ((currentState === 'inactive' || currentState === 'background') && nextAppState === 'active') { + // 앱이 foreground로 돌아올 때 + sessionStartTimeRef.current = Date.now(); + await analytics().logEvent('bv_foreground', { timestamp: new Date().toISOString(), is_logged_in: isLoggedIn, }); - } catch (error) { - console.error('Analytics error:', error); } - }; - - const subscription = AppState.addEventListener('change', async (nextAppState) => { - try { - const currentState = appStateRef.current; - appStateRef.current = nextAppState; - if ( - (currentState === 'inactive' || currentState === 'background') && - nextAppState === 'active' - ) { - // 앱이 foreground로 돌아올 때 - sessionStartTimeRef.current = Date.now(); - await analytics().logEvent('bv_foreground', { - timestamp: new Date().toISOString(), - is_logged_in: isLoggedIn, - }); - } - - if (nextAppState === 'background') { - // 앱이 background로 갈 때 사용 시간 계산 - const sessionDuration = Math.floor( - (Date.now() - sessionStartTimeRef.current) / 1000 - ); // 초 단위 + if (nextAppState === 'background') { + // 앱이 background로 갈 때 사용 시간 계산 + const sessionDuration = Math.floor((Date.now() - sessionStartTimeRef.current) / 1000); // 초 단위 - await analytics().logEvent('bv_background', { - timestamp: new Date().toISOString(), - session_duration_seconds: sessionDuration, - is_logged_in: isLoggedIn, - }); - } - } catch (error) { - console.error('Analytics error:', error); + await analytics().logEvent('bv_background', { + timestamp: new Date().toISOString(), + session_duration_seconds: sessionDuration, + is_logged_in: isLoggedIn, + }); } - }); + } catch (error) { + console.error('Analytics error:', error); + } + }); + + useEffect(() => { + const init = async () => { + await checkLoginStatus(); // 로그인 상태 확인 후 + await logInitialSession(); // 세션 시작 로그 + + setIsInitializing(false); // 초기화 완료 + SplashScreen.hide(); // 스플래시 화면 숨기기 + }; - // 3초 대기 시간과 로그인 상태 확인을 병렬로 처리 - Promise.all([ - checkLoginStatus(), - new Promise(resolve => setTimeout(resolve, 3000)), - logInitialSession(), - ]).finally(() => { - setIsInitializing(false); - SplashScreen.hide(); - }); + init(); return () => { subscription.remove();