_layout.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. {/* Testing Purpose */}
  52. {NODE_ENV == 'development' ? (
  53. <Stack.Screen
  54. name="(public)/test"
  55. options={{
  56. headerShown: false,
  57. title: 'Test Component Page'
  58. }}
  59. />
  60. ) : (
  61. <></>
  62. )}
  63. </Stack>
  64. </AuthProvider>
  65. <StatusBar style="dark" backgroundColor="#fff" />
  66. </GestureHandlerRootView>
  67. );
  68. }