POWR/app/_layout.tsx

92 lines
2.9 KiB
TypeScript
Raw Normal View History

// app/_layout.tsx
2025-02-09 20:38:38 -05:00
import '@/global.css';
2025-02-15 14:03:42 -05:00
import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native';
import { Stack } from 'expo-router';
import { StatusBar } from 'expo-status-bar';
import * as React from 'react';
2025-02-16 22:47:47 -05:00
import { View, Text, Platform } from 'react-native';
2025-02-09 20:38:38 -05:00
import { NAV_THEME } from '@/lib/constants';
import { useColorScheme } from '@/lib/useColorScheme';
import { PortalHost } from '@rn-primitives/portal';
2025-02-09 20:38:38 -05:00
import { setAndroidNavigationBar } from '@/lib/android-navigation-bar';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
2025-02-16 22:47:47 -05:00
import { DatabaseProvider } from '@/components/DatabaseProvider';
import { ErrorBoundary } from '@/components/ErrorBoundary';
import { SettingsDrawerProvider } from '@/lib/contexts/SettingsDrawerContext';
import SettingsDrawer from '@/components/SettingsDrawer';
import { useNDKStore } from '@/lib/stores/ndk';
import { useWorkoutStore } from '@/stores/workoutStore';
2025-02-16 22:47:47 -05:00
const LIGHT_THEME = {
...DefaultTheme,
colors: NAV_THEME.light,
};
2025-02-15 14:03:42 -05:00
2025-02-16 22:47:47 -05:00
const DARK_THEME = {
...DarkTheme,
colors: NAV_THEME.dark,
};
export default function RootLayout() {
2025-02-16 22:47:47 -05:00
const [isInitialized, setIsInitialized] = React.useState(false);
const { colorScheme, isDarkColorScheme } = useColorScheme();
const { init } = useNDKStore();
2025-02-09 20:38:38 -05:00
React.useEffect(() => {
async function initApp() {
2025-02-16 22:47:47 -05:00
try {
if (Platform.OS === 'web') {
document.documentElement.classList.add('bg-background');
}
setAndroidNavigationBar(colorScheme);
// Initialize NDK
await init();
// Load favorites from SQLite
await useWorkoutStore.getState().loadFavorites();
2025-02-16 22:47:47 -05:00
setIsInitialized(true);
} catch (error) {
console.error('Failed to initialize:', error);
}
}
initApp();
}, []);
2025-02-16 22:47:47 -05:00
if (!isInitialized) {
return (
<View className="flex-1 items-center justify-center bg-background">
<Text className="text-foreground">Initializing...</Text>
</View>
);
}
return (
2025-02-16 22:47:47 -05:00
<ErrorBoundary>
<GestureHandlerRootView style={{ flex: 1 }}>
<DatabaseProvider>
<ThemeProvider value={isDarkColorScheme ? DARK_THEME : LIGHT_THEME}>
<SettingsDrawerProvider>
<StatusBar style={isDarkColorScheme ? 'light' : 'dark'} />
<Stack screenOptions={{ headerShown: false }}>
<Stack.Screen
name="(tabs)"
options={{
headerShown: false,
}}
/>
</Stack>
{/* Settings drawer needs to be outside the navigation stack */}
<SettingsDrawer />
<PortalHost />
</SettingsDrawerProvider>
2025-02-16 22:47:47 -05:00
</ThemeProvider>
</DatabaseProvider>
2025-02-09 20:38:38 -05:00
</GestureHandlerRootView>
2025-02-16 22:47:47 -05:00
</ErrorBoundary>
);
2025-02-09 20:38:38 -05:00
}