-
Notifications
You must be signed in to change notification settings - Fork 244
/
app_state.go
42 lines (33 loc) · 1.06 KB
/
app_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package api
import (
"fmt"
"strings"
)
// appState represents if the app is in foreground, background or some other state
type appState string
func (a appState) String() string {
return string(a)
}
// Specific app states
// see https://facebook.github.io/react-native/docs/appstate.html
const (
appStateForeground = appState("active") // these constant values are kept in sync with React Native
appStateBackground = appState("background")
appStateInactive = appState("inactive")
appStateInvalid = appState("")
)
// validAppStates returns an immutable set of valid states.
func validAppStates() []appState {
return []appState{appStateInactive, appStateBackground, appStateForeground}
}
// 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
}
}
return appStateInvalid, fmt.Errorf("could not parse app state: %s", stateString)
}