Add API to listen to app state changes.
This commit is contained in:
parent
81c8669518
commit
3b92b61ef1
|
@ -234,3 +234,14 @@ func (api *StatusAPI) ConnectionChange(typ string, expensive bool) {
|
|||
}
|
||||
api.b.ConnectionChange(state)
|
||||
}
|
||||
|
||||
// AppStateChange handles app state changes (background/foreground).
|
||||
// state values: see https://facebook.github.io/react-native/docs/appstate.html
|
||||
func (api *StatusAPI) AppStateChange(state string) {
|
||||
appState, err := ParseAppState(state)
|
||||
if err != nil {
|
||||
log.Error("AppStateChange failed, ignoring it:", err)
|
||||
return // and do nothing
|
||||
}
|
||||
api.b.AppStateChange(appState)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,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("foreground")
|
||||
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)
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseAppType(t *testing.T) {
|
||||
check := func(input string, expectedState AppState, expectError bool) {
|
||||
actualState, err := ParseAppState(input)
|
||||
assert.Equalf(t, expectedState, actualState, "unexpected result from ParseAppState")
|
||||
if expectError {
|
||||
assert.NotNil(t, err, "error should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
check("foreground", AppStateForeground, false)
|
||||
check("background", AppStateBackground, false)
|
||||
check("inactive", AppStateInactive, false)
|
||||
check(" forEGROUnd ", AppStateForeground, false)
|
||||
check(" backGROUND ", AppStateBackground, false)
|
||||
check(" INACTIVE ", AppStateInactive, false)
|
||||
check("", AppStateInvalid, true)
|
||||
check("back ground", AppStateInvalid, true)
|
||||
check(" back ground ", AppStateInvalid, true)
|
||||
check(" ", AppStateInvalid, true)
|
||||
}
|
|
@ -236,3 +236,11 @@ func (b *StatusBackend) ConnectionChange(state ConnectionState) {
|
|||
// logic of handling state changes here
|
||||
// restart node? force peers reconnect? etc
|
||||
}
|
||||
|
||||
// AppStateChange handles app state changes (background/foreground).
|
||||
func (b *StatusBackend) AppStateChange(state AppState) {
|
||||
log.Info("App State changed: %s", state)
|
||||
|
||||
// TODO: put node in low-power mode if the app is in background (or inactive)
|
||||
// and normal mode if the app is in foreground.
|
||||
}
|
||||
|
|
|
@ -456,3 +456,9 @@ func AddPeer(enode *C.char) *C.char {
|
|||
func ConnectionChange(typ *C.char, expensive C.int) {
|
||||
statusAPI.ConnectionChange(C.GoString(typ), expensive == 1)
|
||||
}
|
||||
|
||||
// AppStateChange handles app state changes (background/foreground).
|
||||
//export AppStateChange
|
||||
func AppStateChange(state *C.char) {
|
||||
statusAPI.AppStateChange(C.GoString(state))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue