Skip to content

Commit

Permalink
Ensuring the current time updates when the app is resumed from backgr…
Browse files Browse the repository at this point in the history
…ound
  • Loading branch information
JedWatson committed Mar 14, 2017
1 parent 1d9a718 commit f9298c9
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions app/scenes/Schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { Component } from 'react';
import {
Animated,
AppState,
Dimensions,
LayoutAnimation,
ListView,
Expand Down Expand Up @@ -127,6 +128,13 @@ export default class Schedule extends Component {
}
});
}
}
componentDidMount() {
this._navigatorWillFocusSubscription = this.props.navigator.navigationContext.addListener(
'willfocus',
this.handleNavigatorWillFocus
);
AppState.addEventListener('change', this.handleAppStateChange);

// Update the schedule once a second.
this.interval = setInterval(
Expand All @@ -135,12 +143,6 @@ export default class Schedule extends Component {
},
60000 // Once a minute
);
}
componentDidMount() {
this._navigatorWillFocusSubscription = this.props.navigator.navigationContext.addListener(
'willfocus',
this.handleNavigatorWillFocus
);

// This is the actual image splash screen, not the animated one.
if (Splash) {
Expand All @@ -155,19 +157,31 @@ export default class Schedule extends Component {
if (this.scrollYListener)
this.state.scrollY.removeListener(this.scrollYListener);
this._navigatorWillFocusSubscription.remove();
AppState.removeEventListener('change', this.handleAppStateChange);

if (this.interval) {
clearInterval(this.interval);
delete this.interval;
}
}

handleAppStateChange = nextAppState => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
// update the current time when the app comes into the foreground
this.setState({ now: new Date() });
}
};
handleNavigatorWillFocus = (event: any) => {
const { scene } = event.data.route;

if (scene === 'Schedule' && this.state.scrollY._value < 120) {
StatusBar.setBarStyle('light-content', true);
}

this.setState({ now: new Date() });
};
gotoEventInfo = () => {
StatusBar.setBarStyle('default', true);
Expand Down

0 comments on commit f9298c9

Please sign in to comment.