Skip to content

Commit 6df6352

Browse files
authored
Merge pull request #234 from flexbox/feature/netinfo
migrate to netinfo api
2 parents 742955d + a68489a commit 6df6352

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

hackathon/spacecraft/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react";
2-
import { NetworkProvider } from "react-native-offline";
32
import { Provider as PaperProvider } from "react-native-paper";
43
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
54
import Constants from "expo-constants";
65

76
import { AuthenticationProvider } from "~/context/Authentication";
7+
import { NetworkProvider } from "~/context/Network";
88
import { useAppearanceTheme } from "~/hooks/useAppearanceTheme";
99
import { Navigator } from "~/navigation/Navigator";
1010

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import React from "react";
2-
import { StyleSheet, Text, View } from "react-native";
3-
import { useIsConnected } from "react-native-offline";
4-
import { ActivityIndicator } from "react-native-paper";
2+
import { StyleSheet, View, Text } from "react-native";
3+
import { ActivityIndicator, useTheme } from "react-native-paper";
4+
5+
import { useNetwork } from "~/context/Network";
56

67
export const Offline = () => {
7-
const isConnected = useIsConnected();
8+
const { isConnected } = useNetwork();
9+
const { colors } = useTheme();
810

911
return (
1012
<>
1113
{!isConnected && (
12-
<View style={styles.container}>
13-
<Text style={styles.message}>Offline, reconnection in progress…</Text>
14-
<ActivityIndicator color="#991B1B" />
14+
<View
15+
style={[styles.container, { backgroundColor: colors.errorContainer }]}
16+
>
17+
<Text style={{ color: colors.error }}>
18+
Offline, reconnection in progress…
19+
</Text>
20+
<ActivityIndicator color={colors.error} />
1521
</View>
1622
)}
1723
</>
@@ -20,18 +26,14 @@ export const Offline = () => {
2026

2127
const styles = StyleSheet.create({
2228
container: {
23-
alignItems: "center",
24-
backgroundColor: "#FEE2E2",
25-
borderRadius: 10,
26-
flexDirection: "row",
27-
justifyContent: "space-between",
28-
marginHorizontal: 20,
2929
padding: 20,
30+
marginHorizontal: 20,
3031
position: "absolute",
3132
top: 55,
3233
width: "90%",
33-
},
34-
message: {
35-
color: "#991B1B",
34+
borderRadius: 10,
35+
flexDirection: "row",
36+
justifyContent: "space-between",
37+
alignItems: "center",
3638
},
3739
});

hackathon/spacecraft/src/context/Authentication.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const AuthenticationContext = createContext<AuthenticationContextProps>(
1212

1313
export const AuthenticationProvider = ({ children }: PropsWithChildren) => {
1414
const [user, setUser] = useState<boolean>(false);
15-
console.log("file: Authentication.tsx ~ line 19 ~ user", user);
15+
// console.log("file: Authentication.tsx ~ line 19 ~ user", user);
1616

1717
return (
1818
<AuthenticationContext.Provider value={{ user, setUser }}>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import NetInfo from "@react-native-community/netinfo";
2+
import type { PropsWithChildren } from "react";
3+
import React, { createContext, useContext, useEffect, useState } from "react";
4+
5+
interface NetworkContextProps {
6+
isConnected: boolean | null;
7+
}
8+
9+
const NetworkContext = createContext<NetworkContextProps>(
10+
{} as NetworkContextProps
11+
);
12+
13+
export const NetworkProvider = ({ children }: PropsWithChildren) => {
14+
const [isConnected, setIsConnected] = useState<boolean | null>(false);
15+
// console.log("file: Network.tsx ~ line 19 ~ isConnected", isConnected);
16+
17+
useEffect(() => {
18+
const unsubscribe = NetInfo.addEventListener((state) => {
19+
// console.log("Connection type", state.type);
20+
// console.log("Is connected?", state.isConnected);
21+
setIsConnected(state.isConnected);
22+
});
23+
24+
NetInfo.fetch().then((state) => {
25+
// console.log("Connection type", state.type);
26+
// console.log("Is connected?", state.isConnected);
27+
setIsConnected(state.isConnected);
28+
});
29+
30+
return () => {
31+
unsubscribe();
32+
};
33+
}, [isConnected]);
34+
35+
return (
36+
<NetworkContext.Provider value={{ isConnected }}>
37+
{children}
38+
</NetworkContext.Provider>
39+
);
40+
};
41+
42+
export const useNetwork = () => useContext(NetworkContext);

0 commit comments

Comments
 (0)