_layout.tsx 2.7 KB

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