Browse Source

Merge branch 'main' of github.com:urbanbigdatacentre/WPD-MobileApp

master
GabrielTrettel 3 years ago
parent
commit
77cb225226
  1. 7
      src/App.js
  2. 8
      src/app/hooks/usePluviometricStation.js
  3. 43
      src/app/utility/cache.js

7
src/App.js

@ -43,7 +43,6 @@ export default function App() {
} }
}, [pluviometerStation]); }, [pluviometerStation]);
// console.log(pluviometerStation);
const restoreUser = async () => { const restoreUser = async () => {
const storageUser = await authStorage.getUser(); const storageUser = await authStorage.getUser();
@ -52,7 +51,7 @@ export default function App() {
global.location = await getLocation(); global.location = await getLocation();
}; };
if (!isReady && netInfo.isInternetReachable) {
if (!isReady) {
return ( return (
<AppLoading <AppLoading
startAsync={restoreUser} startAsync={restoreUser}
@ -60,7 +59,7 @@ export default function App() {
onError={(e) => console.log(e)} onError={(e) => console.log(e)}
/> />
); );
} else if (netInfo.isInternetReachable) {
} else {
if (global.formsSockets === undefined) if (global.formsSockets === undefined)
global.formsSockets = useFiltering(global.location || global.defaultLocation); global.formsSockets = useFiltering(global.location || global.defaultLocation);
@ -83,5 +82,5 @@ export default function App() {
</CurrentLocationProvider> </CurrentLocationProvider>
</AuthContext.Provider> </AuthContext.Provider>
); );
} else return <NoInternetConnectionScreen />;
}
} }

8
src/app/hooks/usePluviometricStation.js

@ -1,6 +1,7 @@
import { useState } from "react"; import { useState } from "react";
import getFieldsAnswers from "../api/RequestFieldsAnswers/getFieldsAnswers"; import getFieldsAnswers from "../api/RequestFieldsAnswers/getFieldsAnswers";
import webSocketClient from "../api/Websockets/webSocketClient"; import webSocketClient from "../api/Websockets/webSocketClient";
import cache from "../utility/cache";
async function getPluvStation_data(id) { async function getPluvStation_data(id) {
const result = await getFieldsAnswers.fieldsAnswers(null, id); const result = await getFieldsAnswers.fieldsAnswers(null, id);
@ -58,15 +59,18 @@ async function getPluviometerStation(userId, setPluviometerStation) {
pluvStation_data.data.responseData.array_to_json[0] pluvStation_data.data.responseData.array_to_json[0]
); );
setPluviometerStation(pluvObject); setPluviometerStation(pluvObject);
cache.store(socketObject, JSON.stringify(pluvObject))
} else { } else {
setPluviometerStation(false); setPluviometerStation(false);
cache.store(socketObject, false)
} }
socketObject.close(); socketObject.close();
}; };
socketObject.onerror = (e) => {
socketObject.onerror = async (e) => {
const dataCache = await cache.get(url);
console.log(e.message); console.log(e.message);
setPluviometerStation(undefined);
setPluviometerStation(dataCache);
}; };
} }

43
src/app/utility/cache.js

@ -0,0 +1,43 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import moment from "moment";
const prefix = "cache";
const expiryInMinutes = 5;
const store = async (key, value) => {
try {
const item = {
value,
timestamp: Date.now(),
};
await AsyncStorage.setItem(prefix + key, JSON.stringify(value));
} catch (error) {
console.log(error);
}
};
const isExpired = (intem) => {
const now = moment(Date.now());
const storedTime = moment(item.timestamp);
return now.diff(storedTime, "minutes") > expiryInMinutes;
};
const get = async(key) => {
try {
const value = await AsyncStorage.get(prefix + key);
if (!item) return null;
if (isExpired(isExperired)) {
await AsyncStorage.removeItem(prefix + key);
return null;
}
} catch (error) {
console.log(error);
}
};
export default {
store,
get,
};
Loading…
Cancel
Save