You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.8 KiB

  1. import React, { useState, useEffect } from "react";
  2. import { NavigationContainer } from "@react-navigation/native";
  3. import navigationTheme from "./app/navigation/navigationTheme";
  4. import "./app/config/globals.js";
  5. import AppLoading from "expo-app-loading";
  6. import FlashMessage from "react-native-flash-message";
  7. import AppNavigator from "./app/navigation/AppNavigator";
  8. import EventLocationProvider from "./app/context/EventLocationContext";
  9. import CurrentLocationProvider from "./app/context/CurrentLocationContext";
  10. import AuthNavigator from "./app/navigation/AuthNavigator";
  11. import { AuthContext } from "./app/auth/context";
  12. import authStorage from "./app/auth/storage";
  13. import MapDataProvider from "./app/context/MapDataContext";
  14. import { getLocation } from "./app/hooks/useLocation";
  15. import { useFiltering } from "./app/hooks/useFiltering";
  16. import NoInternetConnectionScreen from "./app/screens/NoInternetConnectionScreen";
  17. import NetInfo, { useNetInfo } from "@react-native-community/netinfo";
  18. import getPluviometerStation from "./app/hooks/usePluviometricStation";
  19. export default function App() {
  20. const [user, setUser] = useState();
  21. const [pluviometerStation, setPluviometerStation] = useState(undefined);
  22. const [isReady, setIsReady] = useState();
  23. const netInfo = useNetInfo();
  24. useEffect(() => {
  25. if (user?.username != null) {
  26. if (pluviometerStation == undefined) {
  27. getPluviometerStation(user.id, setPluviometerStation);
  28. }
  29. authStorage.setUser(user);
  30. }
  31. else{
  32. setPluviometerStation(undefined);
  33. }
  34. }, [user]);
  35. useEffect(() => {
  36. if (user?.username != null) {
  37. setUser({ ...user, pluviometer: pluviometerStation });
  38. }
  39. }, [pluviometerStation]);
  40. console.log(pluviometerStation);
  41. const restoreUser = async () => {
  42. const storageUser = await authStorage.getUser();
  43. if (storageUser) setUser(storageUser);
  44. global.location = await getLocation();
  45. };
  46. if (!isReady && netInfo.isInternetReachable) {
  47. return (
  48. <AppLoading
  49. startAsync={restoreUser}
  50. onFinish={() => setIsReady(true)}
  51. onError={(e) => console.log(e)}
  52. />
  53. );
  54. } else if (netInfo.isInternetReachable) {
  55. global.formsSockets = useFiltering(global.location || global.defaultLocation);
  56. return (
  57. <AuthContext.Provider
  58. value={{
  59. user,
  60. setUser,
  61. }}
  62. >
  63. <CurrentLocationProvider>
  64. <EventLocationProvider>
  65. <MapDataProvider>
  66. <NavigationContainer theme={navigationTheme}>
  67. {user ? <AppNavigator /> : <AuthNavigator />}
  68. <FlashMessage position="top" />
  69. </NavigationContainer>
  70. </MapDataProvider>
  71. </EventLocationProvider>
  72. </CurrentLocationProvider>
  73. </AuthContext.Provider>
  74. );
  75. } else return <NoInternetConnectionScreen />;
  76. }