Skip to content

Commit 7cc3f72

Browse files
committed
migrate to netinfo api
1 parent 595e57d commit 7cc3f72

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
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 { QueryClient, QueryClientProvider } from "@tanstack/react-query";
22
import Constants from "expo-constants";
33
import React from "react";
4-
import { NetworkProvider } from "react-native-offline";
54
import { Provider as PaperProvider } from "react-native-paper";
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: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import React from "react";
22
import { StyleSheet, View, Text } from "react-native";
3-
import { ActivityIndicator } from "react-native-paper";
4-
import { useIsConnected } from "react-native-offline";
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
</>
@@ -26,12 +32,8 @@ const styles = StyleSheet.create({
2632
top: 55,
2733
width: "90%",
2834
borderRadius: 10,
29-
backgroundColor: "#FEE2E2",
3035
flexDirection: "row",
3136
justifyContent: "space-between",
3237
alignItems: "center",
3338
},
34-
message: {
35-
color: "#991B1B",
36-
},
3739
});
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)