_layout.tsx 2.8 KB

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