_layout.tsx 3.3 KB

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