forked from rescript-react-native/rescript-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetInfoExample.re
More file actions
113 lines (106 loc) · 3.16 KB
/
NetInfoExample.re
File metadata and controls
113 lines (106 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
open BsReactNative;
open Utils;
module NetInfoIsConnectedExample = {
type action =
| UpdateIsConnected(bool);
type state = {isConnected: bool};
let component = ReasonReact.reducerComponent("NetInfoIsConnectedExample");
let make = _children => {
...component,
initialState: () => {isConnected: false},
reducer: (action, _state) =>
switch (action) {
| UpdateIsConnected(isConnected) => Update({isConnected: isConnected})
},
subscriptions: self => [
Sub(
() => {
let handleConnectionChange = isConnected =>
self.send(UpdateIsConnected(isConnected));
NetInfo.IsConnected.addEventListener(handleConnectionChange);
handleConnectionChange;
},
listener => NetInfo.IsConnected.removeEventListener(listener),
),
],
render: ({state}) =>
Style.(
<View>
<View style={style([padding(Pt(10.))])}>
<Text>
{
ReasonReact.string(
state.isConnected ? "Connected" : "Not connected",
)
}
</Text>
</View>
</View>
),
};
};
module NetInfoConnectionTypeExample = {
type action =
| UpdateConnectionType(NetInfo.infoType);
type state = {connectionType: NetInfo.connectionType};
let component =
ReasonReact.reducerComponent("NetInfoConnectionTypeExample");
let checkConnectionType = (_event, {ReasonReact.send}) =>
NetInfo.getConnectionInfo()
|> Js.Promise.then_(info => {
send(UpdateConnectionType(info##_type));
Js.Promise.resolve();
})
|> ignore;
let make = _children => {
...component,
initialState: () => {connectionType: NetInfo.Unknown},
reducer: (action, _state) =>
switch (action) {
| UpdateConnectionType(infoType) =>
let connectionType = NetInfo.connectionType(infoType);
Update({connectionType: connectionType});
},
render: ({state, handle}) =>
Style.(
<View>
<View style={style([padding(Pt(10.))])}>
<TouchableOpacity onPress={handle(checkConnectionType)}>
<Text> {ReasonReact.string("Check connection type")} </Text>
</TouchableOpacity>
<View>
<Text>
{
ReasonReact.string(
switch (state.connectionType) {
| NetInfo.None => "none"
| NetInfo.Unknown => "unknown"
| NetInfo.WiFi => "wifi"
| NetInfo.Cellular => "cellular"
| _ => "something else"
},
)
}
</Text>
</View>
</View>
</View>
),
};
};
let title = "<NetInfo>";
let exampleType = Multiple;
let description = "NetInfo";
let displayName = "NetInfoExample";
let examples: array(Example.t) = [|
{
title: "NetInfo.IsConnected.addEventListener()",
description: None,
render: () => <NetInfoIsConnectedExample />,
},
{
title: "NetInfo.getConnectionInfo()",
description: None,
render: () => <NetInfoConnectionTypeExample />,
},
|];