forked from vergnet/application-amicale
Improved refresh handling
This commit is contained in:
parent
9ebf34fca9
commit
81b44f64fd
5 changed files with 21 additions and 10 deletions
|
@ -10,7 +10,8 @@ import EmptyWebSectionListItem from "./EmptyWebSectionListItem";
|
|||
type Props = {
|
||||
navigation: Object,
|
||||
fetchUrl: string,
|
||||
refreshTime: number,
|
||||
autoRefreshTime: number,
|
||||
refreshOnFocus: boolean,
|
||||
renderItem: React.Node,
|
||||
renderSectionHeader: React.Node,
|
||||
stickyHeader: boolean,
|
||||
|
@ -24,6 +25,8 @@ type State = {
|
|||
snackbarVisible: boolean
|
||||
};
|
||||
|
||||
|
||||
const MIN_REFRESH_TIME = 5 * 1000;
|
||||
/**
|
||||
* Custom component defining a material icon using native base
|
||||
*
|
||||
|
@ -82,15 +85,19 @@ export default class WebSectionList extends React.Component<Props, State> {
|
|||
const onScreenBlur = this.onScreenBlur.bind(this);
|
||||
this.props.navigation.addListener('focus', onScreenFocus);
|
||||
this.props.navigation.addListener('blur', onScreenBlur);
|
||||
this.onRefresh();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Refresh data when focusing the screen and setup a refresh interval if asked to
|
||||
*/
|
||||
onScreenFocus() {
|
||||
this.onRefresh();
|
||||
if (this.props.refreshTime > 0)
|
||||
this.refreshInterval = setInterval(this.onRefresh, this.props.refreshTime)
|
||||
if (this.props.refreshOnFocus && this.lastRefresh !== undefined)
|
||||
this.onRefresh();
|
||||
if (this.props.autoRefreshTime > 0)
|
||||
this.refreshInterval = setInterval(this.onRefresh, this.props.autoRefreshTime)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +134,7 @@ export default class WebSectionList extends React.Component<Props, State> {
|
|||
onRefresh() {
|
||||
let canRefresh;
|
||||
if (this.lastRefresh !== undefined)
|
||||
canRefresh = (new Date().getTime() - this.lastRefresh.getTime()) > this.props.refreshTime;
|
||||
canRefresh = (new Date().getTime() - this.lastRefresh.getTime()) > MIN_REFRESH_TIME;
|
||||
else
|
||||
canRefresh = true;
|
||||
if (canRefresh) {
|
||||
|
|
|
@ -526,7 +526,8 @@ export default class HomeScreen extends React.Component<Props> {
|
|||
<WebSectionList
|
||||
createDataset={this.createDataset}
|
||||
navigation={nav}
|
||||
refreshTime={REFRESH_TIME}
|
||||
autoRefreshTime={REFRESH_TIME}
|
||||
refreshOnFocus={true}
|
||||
fetchUrl={DATA_URL}
|
||||
renderItem={this.getRenderItem}/>
|
||||
);
|
||||
|
|
|
@ -194,7 +194,8 @@ export default class ProximoMainScreen extends React.Component<Props, State> {
|
|||
<WebSectionList
|
||||
createDataset={this.createDataset}
|
||||
navigation={nav}
|
||||
refreshTime={0}
|
||||
autoRefreshTime={0}
|
||||
refreshOnFocus={false}
|
||||
fetchUrl={DATA_URL}
|
||||
renderItem={this.getRenderItem}/>
|
||||
);
|
||||
|
|
|
@ -363,10 +363,11 @@ export default class ProxiwashScreen extends React.Component<Props, State> {
|
|||
<WebSectionList
|
||||
createDataset={this.createDataset}
|
||||
navigation={nav}
|
||||
refreshTime={REFRESH_TIME}
|
||||
fetchUrl={DATA_URL}
|
||||
renderItem={this.getRenderItem}
|
||||
renderSectionHeader={this.getRenderSectionHeader}/>
|
||||
renderSectionHeader={this.getRenderSectionHeader}
|
||||
autoRefreshTime={REFRESH_TIME}
|
||||
refreshOnFocus={true}/>
|
||||
</View>
|
||||
|
||||
);
|
||||
|
|
|
@ -168,7 +168,8 @@ export default class SelfMenuScreen extends React.Component<Props> {
|
|||
<WebSectionList
|
||||
createDataset={this.createDataset}
|
||||
navigation={nav}
|
||||
refreshTime={0}
|
||||
autoRefreshTime={0}
|
||||
refreshOnFocus={false}
|
||||
fetchUrl={DATA_URL}
|
||||
renderItem={this.getRenderItem}
|
||||
renderSectionHeader={this.getRenderSectionHeader}
|
||||
|
|
Loading…
Reference in a new issue