-
Notifications
You must be signed in to change notification settings - Fork 246
/
connection.go
72 lines (61 loc) · 1.56 KB
/
connection.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
package api
import (
"fmt"
)
// connectionState represents device connection state and type,
// as reported by mobile framework.
//
// Zero value represents default assumption about network (online and unknown type).
type connectionState 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
)
// string formats ConnectionState for logs. Implements Stringer.
func (c connectionState) 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
}