status-go/api/app_state.go

43 lines
1.1 KiB
Go
Raw Permalink Normal View History

package api
import (
"fmt"
"strings"
)
2018-06-27 08:11:45 +00:00
// appState represents if the app is in foreground, background or some other state
type appState string
2018-06-27 08:11:45 +00:00
func (a appState) String() string {
return string(a)
}
// Specific app states
// see https://facebook.github.io/react-native/docs/appstate.html
const (
2018-06-27 08:11:45 +00:00
appStateForeground = appState("active") // these constant values are kept in sync with React Native
appStateBackground = appState("background")
appStateInactive = appState("inactive")
2018-06-27 08:11:45 +00:00
appStateInvalid = appState("")
)
// validAppStates returns an immutable set of valid states.
2018-06-27 08:11:45 +00:00
func validAppStates() []appState {
return []appState{appStateInactive, appStateBackground, appStateForeground}
}
2018-06-27 08:11:45 +00:00
// parseAppState creates AppState from a string
func parseAppState(stateString string) (appState, error) {
// a bit of cleaning up
stateString = strings.ToLower(strings.TrimSpace(stateString))
for _, state := range validAppStates() {
if stateString == state.String() {
return state, nil
}
}
2018-06-27 08:11:45 +00:00
return appStateInvalid, fmt.Errorf("could not parse app state: %s", stateString)
}