@@ -6,83 +6,87 @@ import useContactStore from "@/store/contactStore";
66import { ThemeProvider } from "@react-navigation/native" ;
77import { SplashScreen , Stack } from "expo-router" ;
88import { StatusBar } from "expo-status-bar" ;
9- import { useEffect , useState } from "react" ;
9+ import { useEffect , useRef , useState } from "react" ;
1010import { SheetProvider } from "react-native-actions-sheet" ;
1111import { GestureHandlerRootView } from "react-native-gesture-handler" ;
1212import { PaperProvider } from "react-native-paper" ;
1313
1414SplashScreen . preventAutoHideAsync ( ) ;
1515
1616export default function RootLayout ( ) {
17- const paperTheme = useTheme ( ) ;
18- const fetchContacts = useContactStore . use . fetchContacts ( ) ;
19- const [ isReady , setIsReady ] = useState ( false ) ;
17+ const paperTheme = useTheme ( ) ;
18+ const fetchContacts = useContactStore . use . fetchContacts ( ) ;
19+ const [ isReady , setIsReady ] = useState ( false ) ;
20+ const initializationRef = useRef ( false ) ;
2021
21- // Start fetching contacts during app initialization (splash screen time)
22- useEffect ( ( ) => {
23- const initializeApp = async ( ) => {
24- try {
25- await fetchContacts ( ) ;
26- } catch ( error ) {
27- console . error ( "Error during app initialization:" , error ) ;
28- } finally {
29- setIsReady ( true ) ;
30- await SplashScreen . hideAsync ( ) ;
31- }
32- } ;
22+ // Start fetching contacts during app initialization (splash screen time)
23+ useEffect ( ( ) => {
24+ if ( initializationRef . current ) return ; // Prevent re-initialization
3325
34- initializeApp ( ) ;
35- } , [ ] ) ;
26+ const initializeApp = async ( ) => {
27+ try {
28+ initializationRef . current = true ;
29+ await fetchContacts ( ) ;
30+ } catch ( error ) {
31+ console . error ( "Error during app initialization:" , error ) ;
32+ } finally {
33+ setIsReady ( true ) ;
34+ await SplashScreen . hideAsync ( ) ;
35+ }
36+ } ;
3637
37- if ( ! isReady ) {
38- return null ; // Keep showing splash screen
39- }
38+ initializeApp ( ) ;
39+ } , [ fetchContacts ] ) ;
4040
41- return (
42- < GestureHandlerRootView style = { { flex : 1 } } >
43- < ErrorBoundary >
44- < PaperProvider theme = { paperTheme } >
45- < ThemeProvider value = { paperTheme } >
46- < SheetProvider context = "global" >
47- < Stack
48- screenOptions = { {
49- header : ( props ) => < CustomNavigationBar { ...props } /> ,
50- } }
51- >
52- < Stack . Screen
53- name = "new-contact"
54- options = { {
55- title : "Add Contact" ,
56- header : ( props ) => (
57- < CustomNavigationBar { ...props } mode = "small" />
58- ) ,
59- } }
60- />
61- < Stack . Screen
62- name = "edit-contact"
63- options = { {
64- title : "Edit Contact" ,
65- header : ( props ) => (
66- < CustomNavigationBar
67- { ...props }
68- mode = "small"
69- popToTop = { true }
70- />
71- ) ,
72- } }
73- />
74- < Stack . Screen
75- name = "settings"
76- options = { {
77- title : "Settings" ,
78- } }
79- />
80- </ Stack >
81- </ SheetProvider >
82- </ ThemeProvider >
83- < StatusBar style = { paperTheme . dark ? "light" : "dark" } />
84- </ PaperProvider >
85- </ ErrorBoundary >
86- </ GestureHandlerRootView >
87- ) ;
41+ if ( ! isReady ) {
42+ return null ; // Keep showing splash screen
43+ }
44+
45+ return (
46+ < GestureHandlerRootView style = { { flex : 1 } } >
47+ < ErrorBoundary >
48+ < PaperProvider theme = { paperTheme } >
49+ < ThemeProvider value = { paperTheme } >
50+ < SheetProvider context = "global" >
51+ < Stack
52+ screenOptions = { {
53+ header : ( props ) => < CustomNavigationBar { ...props } /> ,
54+ } }
55+ >
56+ < Stack . Screen
57+ name = "new-contact"
58+ options = { {
59+ title : "Add Contact" ,
60+ header : ( props ) => (
61+ < CustomNavigationBar { ...props } mode = "small" />
62+ ) ,
63+ } }
64+ />
65+ < Stack . Screen
66+ name = "edit-contact"
67+ options = { {
68+ title : "Edit Contact" ,
69+ header : ( props ) => (
70+ < CustomNavigationBar
71+ { ...props }
72+ mode = "small"
73+ popToTop = { true }
74+ />
75+ ) ,
76+ } }
77+ />
78+ < Stack . Screen
79+ name = "settings"
80+ options = { {
81+ title : "Settings" ,
82+ } }
83+ />
84+ </ Stack >
85+ </ SheetProvider >
86+ </ ThemeProvider >
87+ < StatusBar style = { paperTheme . dark ? "light" : "dark" } />
88+ </ PaperProvider >
89+ </ ErrorBoundary >
90+ </ GestureHandlerRootView >
91+ ) ;
8892}
0 commit comments