github.com/status-im/status-go@v1.1.0/connection/state.go (about)

     1  package connection
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // State represents device connection state and type,
     8  // as reported by mobile framework.
     9  //
    10  // Zero value represents default assumption about network (online and unknown type).
    11  type State struct {
    12  	Offline   bool `json:"offline"`
    13  	Type      Type `json:"type"`
    14  	Expensive bool `json:"expensive"`
    15  }
    16  
    17  // Type represents description of available
    18  // connection types as reported by React Native (see
    19  // https://facebook.github.io/react-native/docs/netinfo.html)
    20  // We're interested mainly in 'wifi' and 'cellular', but
    21  // other types are also may be used.
    22  type Type byte
    23  
    24  const (
    25  	Offline  = "offline"
    26  	Wifi     = "wifi"
    27  	Cellular = "cellular"
    28  	Unknown  = "unknown"
    29  	None     = "none"
    30  )
    31  
    32  // NewType creates new Type from string.
    33  func NewType(s string) Type {
    34  	switch s {
    35  	case Cellular:
    36  		return connectionCellular
    37  	case Wifi:
    38  		return connectionWifi
    39  	}
    40  
    41  	return connectionUnknown
    42  }
    43  
    44  // Type constants
    45  const (
    46  	connectionUnknown  Type = iota
    47  	connectionCellular      // cellular, LTE, 4G, 3G, EDGE, etc.
    48  	connectionWifi          // WIFI or iOS simulator
    49  )
    50  
    51  func (c State) IsExpensive() bool {
    52  	return c.Expensive || c.Type == connectionCellular
    53  }
    54  
    55  // string formats ConnectionState for logs. Implements Stringer.
    56  func (c State) String() string {
    57  	if c.Offline {
    58  		return Offline
    59  	}
    60  
    61  	var typ string
    62  	switch c.Type {
    63  	case connectionWifi:
    64  		typ = Wifi
    65  	case connectionCellular:
    66  		typ = Cellular
    67  	default:
    68  		typ = Unknown
    69  	}
    70  
    71  	if c.Expensive {
    72  		return fmt.Sprintf("%s (expensive)", typ)
    73  	}
    74  
    75  	return typ
    76  }