2018-03-14 15:46:21 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-11-19 09:31:29 +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
|
|
|
|
2024-11-19 09:31:29 +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 (
|
2024-11-19 09:31:29 +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
|
|
|
|
2024-11-19 09:31:29 +00:00
|
|
|
AppStateInvalid = AppState("")
|
2018-03-14 15:46:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// validAppStates returns an immutable set of valid states.
|
2024-11-19 09:31:29 +00:00
|
|
|
func validAppStates() []AppState {
|
|
|
|
return []AppState{AppStateInactive, AppStateBackground, AppStateForeground}
|
2018-03-14 15:46:21 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 09:31:29 +00:00
|
|
|
func (a AppState) IsValid() bool {
|
|
|
|
for _, state := range validAppStates() {
|
|
|
|
if a == state {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 09:31:29 +00:00
|
|
|
return AppStateInvalid, fmt.Errorf("could not parse app state: %s", stateString)
|
2018-03-14 15:46:21 +00:00
|
|
|
}
|