2021-05-14 10:55:42 +00:00
|
|
|
package connection
|
2018-02-09 20:28:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
// State represents device connection state and type,
|
2018-02-09 20:28:16 +00:00
|
|
|
// as reported by mobile framework.
|
|
|
|
//
|
2018-02-16 02:53:18 +00:00
|
|
|
// Zero value represents default assumption about network (online and unknown type).
|
2021-05-14 10:55:42 +00:00
|
|
|
type State struct {
|
2022-03-28 10:10:40 +00:00
|
|
|
Offline bool `json:"offline"`
|
|
|
|
Type Type `json:"type"`
|
|
|
|
Expensive bool `json:"expensive"`
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
// Type represents description of available
|
2018-02-09 20:28:16 +00:00
|
|
|
// 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.
|
2022-03-28 10:10:40 +00:00
|
|
|
type Type byte
|
2018-02-09 20:28:16 +00:00
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
const (
|
2021-05-14 10:55:42 +00:00
|
|
|
Offline = "offline"
|
|
|
|
Wifi = "wifi"
|
|
|
|
Cellular = "cellular"
|
|
|
|
Unknown = "unknown"
|
|
|
|
None = "none"
|
2018-06-12 16:50:25 +00:00
|
|
|
)
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
// NewType creates new Type from string.
|
|
|
|
func NewType(s string) Type {
|
2018-02-09 20:28:16 +00:00
|
|
|
switch s {
|
2021-05-14 10:55:42 +00:00
|
|
|
case Cellular:
|
2018-06-27 08:11:45 +00:00
|
|
|
return connectionCellular
|
2021-05-14 10:55:42 +00:00
|
|
|
case Wifi:
|
2018-06-27 08:11:45 +00:00
|
|
|
return connectionWifi
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
return connectionUnknown
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
// Type constants
|
2018-02-09 20:28:16 +00:00
|
|
|
const (
|
2022-03-28 10:10:40 +00:00
|
|
|
connectionUnknown Type = iota
|
|
|
|
connectionCellular // cellular, LTE, 4G, 3G, EDGE, etc.
|
|
|
|
connectionWifi // WIFI or iOS simulator
|
2018-02-09 20:28:16 +00:00
|
|
|
)
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (c State) IsExpensive() bool {
|
|
|
|
return c.Expensive || c.Type == connectionCellular
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
// string formats ConnectionState for logs. Implements Stringer.
|
2021-05-14 10:55:42 +00:00
|
|
|
func (c State) String() string {
|
2018-02-09 20:28:16 +00:00
|
|
|
if c.Offline {
|
2021-05-14 10:55:42 +00:00
|
|
|
return Offline
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var typ string
|
|
|
|
switch c.Type {
|
2018-06-27 08:11:45 +00:00
|
|
|
case connectionWifi:
|
2021-05-14 10:55:42 +00:00
|
|
|
typ = Wifi
|
2018-06-27 08:11:45 +00:00
|
|
|
case connectionCellular:
|
2021-05-14 10:55:42 +00:00
|
|
|
typ = Cellular
|
2018-02-09 20:28:16 +00:00
|
|
|
default:
|
2021-05-14 10:55:42 +00:00
|
|
|
typ = Unknown
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Expensive {
|
|
|
|
return fmt.Sprintf("%s (expensive)", typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
return typ
|
|
|
|
}
|