diff --git a/src/app/components/ConnectionWarning.js b/src/app/components/ConnectionWarning.js new file mode 100644 index 0000000..06f3153 --- /dev/null +++ b/src/app/components/ConnectionWarning.js @@ -0,0 +1,78 @@ +import { View, Text } from "react-native"; +import React, { useEffect, useState } from "react"; +import colors from "../config/colors"; + +import NetInfo, { useNetInfo } from "@react-native-community/netinfo"; + +import { MaterialCommunityIcons } from "@expo/vector-icons"; +import { dimensions } from "../config/dimensions"; +function ConnectionWarning() { + const reconnected = { + title: "Conexão à internet restabelecida", + color: colors.greenWarning, + }; + const disconnected = { + title: "Sem conexão à internet", + color: colors.blueWarning, + }; + + const [warningObject, setWarningObject] = useState({ + title: disconnected.title, + color: disconnected.color, + }); + + const [isConnected, setIsConnected] = useState(true); + const [showWarning, setShowWarning] = useState(false); + useEffect(() => { + const unsubscribe = NetInfo.addEventListener((state) => { + if (!state.isConnected) { + setShowWarning(true); + setWarningObject({ + ...warningObject, + title: disconnected.title, + color: disconnected.color, + }); + } + else if (!isConnected && state.isConnected) { + const timeout = setTimeout(() => { + setShowWarning(false); + }, 3000); + setWarningObject({ + ...warningObject, + title: reconnected.title, + color: reconnected.color, + }); + } + setIsConnected(state.isConnected); + }); + return () => unsubscribe(); + }, [useNetInfo().isConnected]); + + + + return ( + <> + {showWarning && ( + + + {warningObject.title} + + + )} + + ); +} + +export default ConnectionWarning; diff --git a/src/app/config/colors.js b/src/app/config/colors.js index 41451f5..11f8895 100644 --- a/src/app/config/colors.js +++ b/src/app/config/colors.js @@ -19,4 +19,6 @@ export default { gold: "#ffd700", toggle: "#e5eff4", grayBG: "#EEECEC", + blueWarning: "#75BDE0", + greenWarning: "#6BC69A" };