2018-03-14 15:46:21 +00:00
|
|
|
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-03-14 15:46:21 +00:00
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
func (a appState) String() string {
|
2018-03-14 15:46:21 +00:00
|
|
|
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-03-14 15:46:21 +00:00
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
appStateInvalid = appState("")
|
2018-03-14 15:46:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-03-14 15:46:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
// parseAppState creates AppState from a string
|
|
|
|
func parseAppState(stateString string) (appState, error) {
|
2018-03-14 15:46:21 +00:00
|
|
|
// 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)
|
2018-03-14 15:46:21 +00:00
|
|
|
}
|