_layout.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 [isLoading, setIsLoading] = useState(true);
  16. const { user } = useAuth();
  17. const { expoPushToken, notification } = usePushNotifications();
  18. const data = JSON.stringify(notification, undefined, 2);
  19. useEffect(() => {
  20. const fetchVersion = async () => {
  21. const response = await authenticationService.getVersion();
  22. checkVersion(response);
  23. };
  24. // Initial version check
  25. fetchVersion();
  26. // Set up AppState listener
  27. const subscription = AppState.addEventListener('change', async (nextAppState) => {
  28. if (nextAppState === 'active') {
  29. fetchVersion();
  30. }
  31. const update = await Updates.checkForUpdateAsync();
  32. if (update.isAvailable) {
  33. await Updates.fetchUpdateAsync();
  34. await Updates.reloadAsync(); // Reload the app to apply the update
  35. }
  36. });
  37. // Cleanup subscription on unmount
  38. return () => {
  39. subscription.remove();
  40. };
  41. }, []);
  42. return (
  43. <GestureHandlerRootView style={{ flex: 1 }}>
  44. <AuthProvider>
  45. <Stack>
  46. <Stack.Screen name="(auth)/(tabs)" options={{ headerShown: false }} />
  47. <Stack.Screen name="(public)/login" options={{ headerShown: false }} />
  48. <Stack.Screen name="(public)/registerChooseVehiclesOne" options={{ headerShown: false }} />
  49. <Stack.Screen name="(public)/registerChooseVehiclesTwo" options={{ headerShown: false }} />
  50. {/* Testing Purpose */}
  51. {EXPO_PUBLIC_NODE_ENV == 'development' ? (
  52. <Stack.Screen
  53. name="(public)/test"
  54. options={{
  55. headerShown: false,
  56. title: 'Test Component Page'
  57. }}
  58. />
  59. ) : (
  60. <></>
  61. )}
  62. </Stack>
  63. </AuthProvider>
  64. <StatusBar style="dark" backgroundColor="#fff" />
  65. </GestureHandlerRootView>
  66. );
  67. }