From 30ae6b55d2fb4b40c1b22fdbca8a5b34c55c9146 Mon Sep 17 00:00:00 2001 From: analuizaff Date: Tue, 15 Mar 2022 19:05:47 -0300 Subject: [PATCH 1/2] removing internet connection verificatrion --- src/App.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 6600f32..2a81ae2 100644 --- a/src/App.js +++ b/src/App.js @@ -43,7 +43,6 @@ export default function App() { } }, [pluviometerStation]); - // console.log(pluviometerStation); const restoreUser = async () => { const storageUser = await authStorage.getUser(); @@ -52,7 +51,7 @@ export default function App() { global.location = await getLocation(); }; - if (!isReady && netInfo.isInternetReachable) { + if (!isReady) { return ( console.log(e)} /> ); - } else if (netInfo.isInternetReachable) { + } else { if (global.formsSockets === undefined) global.formsSockets = useFiltering(global.location || global.defaultLocation); @@ -83,5 +82,5 @@ export default function App() { ); - } else return ; + } } From 453c83bcfe98f5377e14cfd9e64f6ea3e1244994 Mon Sep 17 00:00:00 2001 From: analuizaff Date: Tue, 15 Mar 2022 22:30:48 -0300 Subject: [PATCH 2/2] caching data from user's pluviometer --- src/app/hooks/usePluviometricStation.js | 8 +++-- src/app/utility/cache.js | 43 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/app/utility/cache.js diff --git a/src/app/hooks/usePluviometricStation.js b/src/app/hooks/usePluviometricStation.js index 2593802..3df4ae3 100644 --- a/src/app/hooks/usePluviometricStation.js +++ b/src/app/hooks/usePluviometricStation.js @@ -1,6 +1,7 @@ import { useState } from "react"; import getFieldsAnswers from "../api/RequestFieldsAnswers/getFieldsAnswers"; import webSocketClient from "../api/Websockets/webSocketClient"; +import cache from "../utility/cache"; async function getPluvStation_data(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] ); setPluviometerStation(pluvObject); + cache.store(socketObject, JSON.stringify(pluvObject)) } else { setPluviometerStation(false); + cache.store(socketObject, false) } socketObject.close(); }; - socketObject.onerror = (e) => { + socketObject.onerror = async (e) => { + const dataCache = await cache.get(url); console.log(e.message); - setPluviometerStation(undefined); + setPluviometerStation(dataCache); }; } diff --git a/src/app/utility/cache.js b/src/app/utility/cache.js new file mode 100644 index 0000000..5945b6e --- /dev/null +++ b/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, +};