github.com/status-im/status-go@v1.1.0/api/app_state.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // appState represents if the app is in foreground, background or some other state
     9  type appState string
    10  
    11  func (a appState) String() string {
    12  	return string(a)
    13  }
    14  
    15  // Specific app states
    16  // see https://facebook.github.io/react-native/docs/appstate.html
    17  const (
    18  	appStateForeground = appState("active") // these constant values are kept in sync with React Native
    19  	appStateBackground = appState("background")
    20  	appStateInactive   = appState("inactive")
    21  
    22  	appStateInvalid = appState("")
    23  )
    24  
    25  // validAppStates returns an immutable set of valid states.
    26  func validAppStates() []appState {
    27  	return []appState{appStateInactive, appStateBackground, appStateForeground}
    28  }
    29  
    30  // parseAppState creates AppState from a string
    31  func parseAppState(stateString string) (appState, error) {
    32  	// a bit of cleaning up
    33  	stateString = strings.ToLower(strings.TrimSpace(stateString))
    34  
    35  	for _, state := range validAppStates() {
    36  		if stateString == state.String() {
    37  			return state, nil
    38  		}
    39  	}
    40  
    41  	return appStateInvalid, fmt.Errorf("could not parse app state: %s", stateString)
    42  }