_layout.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import './global.css';
  2. import { Stack } from 'expo-router/stack';
  3. import { StatusBar } from 'expo-status-bar';
  4. import AuthProvider, { useAuth } from '../context/AuthProvider';
  5. // import { EXPO_PUBLIC_NODE_ENV } from '@env';
  6. import { GestureHandlerRootView } from 'react-native-gesture-handler';
  7. import { useEffect, useState } from 'react';
  8. import { checkVersion } from '../component/checkVersion';
  9. import { authenticationService } from '../service/authService';
  10. import { usePushNotifications } from './hooks/usePushNotifications';
  11. import Constants from 'expo-constants';
  12. import { Alert, AppState } from 'react-native';
  13. import * as Updates from 'expo-updates';
  14. export default function RootLayout() {
  15. const NODE_ENV = process.env.EXPO_PUBLIC_NODE_ENV;
  16. const [isLoading, setIsLoading] = useState(true);
  17. const { user } = useAuth();
  18. const { expoPushToken, notification } = usePushNotifications();
  19. const data = JSON.stringify(notification, undefined, 2);
  20. useEffect(() => {
  21. const fetchVersion = async () => {
  22. const response = await authenticationService.getVersion();
  23. checkVersion(response);
  24. };
  25. // Initial version check
  26. fetchVersion();
  27. // Set up AppState listener
  28. const subscription = AppState.addEventListener('change', async (nextAppState) => {
  29. if (nextAppState === 'active') {
  30. fetchVersion();
  31. }
  32. const update = await Updates.checkForUpdateAsync();
  33. if (update.isAvailable) {
  34. await Updates.fetchUpdateAsync();
  35. await Updates.reloadAsync(); // Reload the app to apply the update
  36. }
  37. });
  38. // Cleanup subscription on unmount
  39. return () => {
  40. subscription.remove();
  41. };
  42. }, []);
  43. return (
  44. <GestureHandlerRootView style={{ flex: 1 }}>
  45. <AuthProvider>
  46. <Stack>
  47. <Stack.Screen name="(auth)/(tabs)" options={{ headerShown: false }} />
  48. <Stack.Screen name="(public)/login" options={{ headerShown: false }} />
  49. <Stack.Screen name="(public)/registerChooseVehiclesOne" options={{ headerShown: false }} />
  50. <Stack.Screen name="(public)/registerChooseVehiclesTwo" options={{ headerShown: false }} />
  51. <Stack.Screen name="(public)/userTermsPage" options={{ headerShown: false }} />
  52. {/* Testing Purpose */}
  53. {NODE_ENV == 'development' ? (
  54. <Stack.Screen
  55. name="(public)/test"
  56. options={{
  57. headerShown: false,
  58. title: 'Test Component Page'
  59. }}
  60. />
  61. ) : (
  62. <></>
  63. )}
  64. </Stack>
  65. </AuthProvider>
  66. <StatusBar style="dark" backgroundColor="#fff" />
  67. </GestureHandlerRootView>
  68. );
  69. }