-
Notifications
You must be signed in to change notification settings - Fork 246
/
state.go
76 lines (64 loc) · 1.63 KB
/
state.go
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
package connection
import (
"fmt"
)
// State represents device connection state and type,
// as reported by mobile framework.
//
// Zero value represents default assumption about network (online and unknown type).
type State struct {
Offline bool `json:"offline"`
Type connectionType `json:"type"`
Expensive bool `json:"expensive"`
}
// connectionType represents description of available
// connection types as reported by React Native (see
// https://facebook.github.io/react-native/docs/netinfo.html)
// We're interested mainly in 'wifi' and 'cellular', but
// other types are also may be used.
type connectionType byte
const (
Offline = "offline"
Wifi = "wifi"
Cellular = "cellular"
Unknown = "unknown"
None = "none"
)
// NewConnectionType creates new connectionType from string.
func NewConnectionType(s string) connectionType {
switch s {
case Cellular:
return connectionCellular
case Wifi:
return connectionWifi
}
return connectionUnknown
}
// ConnectionType constants
const (
connectionUnknown connectionType = iota
connectionCellular // cellular, LTE, 4G, 3G, EDGE, etc.
connectionWifi // WIFI or iOS simulator
)
func (c State) IsExpensive() bool {
return c.Expensive || c.Type == connectionCellular
}
// string formats ConnectionState for logs. Implements Stringer.
func (c State) String() string {
if c.Offline {
return Offline
}
var typ string
switch c.Type {
case connectionWifi:
typ = Wifi
case connectionCellular:
typ = Cellular
default:
typ = Unknown
}
if c.Expensive {
return fmt.Sprintf("%s (expensive)", typ)
}
return typ
}