diff --git a/app/screens/channel/channel.js b/app/screens/channel/channel.js index b7c908070..1a7b7329c 100644 --- a/app/screens/channel/channel.js +++ b/app/screens/channel/channel.js @@ -5,6 +5,7 @@ import React, {PureComponent} from 'react'; import PropTypes from 'prop-types'; import {injectIntl, intlShape} from 'react-intl'; import { + AppState, Platform, View, } from 'react-native'; @@ -66,6 +67,10 @@ class Channel extends PureComponent { } componentDidMount() { + if (Platform.OS === 'android') { + AppState.addEventListener('change', this.handleAppStateChange); + } + if (tracker.initialLoad) { this.props.actions.recordLoadTime('Start time', 'initialLoad'); } @@ -95,6 +100,10 @@ class Channel extends PureComponent { EventEmitter.off('leave_team', this.handleLeaveTeam); this.networkListener.removeEventListener(); + if (Platform.OS === 'android') { + AppState.removeEventListener('change', this.handleAppStateChange); + } + closeWebSocket(); stopPeriodicStatusUpdates(); } @@ -141,6 +150,25 @@ class Channel extends PureComponent { } }); + handleAppStateChange = async (appState) => { + const {actions} = this.props; + const { + closeWebSocket, + initWebSocket, + startPeriodicStatusUpdates, + stopPeriodicStatusUpdates, + } = actions; + const isActive = appState === 'active'; + + if (isActive) { + initWebSocket(Platform.OS); + startPeriodicStatusUpdates(); + } else { + closeWebSocket(true); + stopPeriodicStatusUpdates(); + } + }; + handleConnectionChange = (isConnected) => { const {actions} = this.props; const {