Improved refresh handling

This commit is contained in:
keplyx 2020-03-08 15:50:34 +01:00
parent 9ebf34fca9
commit 81b44f64fd
5 changed files with 21 additions and 10 deletions

View file

@ -10,7 +10,8 @@ import EmptyWebSectionListItem from "./EmptyWebSectionListItem";
type Props = { type Props = {
navigation: Object, navigation: Object,
fetchUrl: string, fetchUrl: string,
refreshTime: number, autoRefreshTime: number,
refreshOnFocus: boolean,
renderItem: React.Node, renderItem: React.Node,
renderSectionHeader: React.Node, renderSectionHeader: React.Node,
stickyHeader: boolean, stickyHeader: boolean,
@ -24,6 +25,8 @@ type State = {
snackbarVisible: boolean snackbarVisible: boolean
}; };
const MIN_REFRESH_TIME = 5 * 1000;
/** /**
* Custom component defining a material icon using native base * 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); const onScreenBlur = this.onScreenBlur.bind(this);
this.props.navigation.addListener('focus', onScreenFocus); this.props.navigation.addListener('focus', onScreenFocus);
this.props.navigation.addListener('blur', onScreenBlur); this.props.navigation.addListener('blur', onScreenBlur);
this.onRefresh();
} }
/** /**
* Refresh data when focusing the screen and setup a refresh interval if asked to * Refresh data when focusing the screen and setup a refresh interval if asked to
*/ */
onScreenFocus() { onScreenFocus() {
if (this.props.refreshOnFocus && this.lastRefresh !== undefined)
this.onRefresh(); this.onRefresh();
if (this.props.refreshTime > 0) if (this.props.autoRefreshTime > 0)
this.refreshInterval = setInterval(this.onRefresh, this.props.refreshTime) this.refreshInterval = setInterval(this.onRefresh, this.props.autoRefreshTime)
} }
/** /**
@ -127,7 +134,7 @@ export default class WebSectionList extends React.Component<Props, State> {
onRefresh() { onRefresh() {
let canRefresh; let canRefresh;
if (this.lastRefresh !== undefined) 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 else
canRefresh = true; canRefresh = true;
if (canRefresh) { if (canRefresh) {

View file

@ -526,7 +526,8 @@ export default class HomeScreen extends React.Component<Props> {
<WebSectionList <WebSectionList
createDataset={this.createDataset} createDataset={this.createDataset}
navigation={nav} navigation={nav}
refreshTime={REFRESH_TIME} autoRefreshTime={REFRESH_TIME}
refreshOnFocus={true}
fetchUrl={DATA_URL} fetchUrl={DATA_URL}
renderItem={this.getRenderItem}/> renderItem={this.getRenderItem}/>
); );

View file

@ -194,7 +194,8 @@ export default class ProximoMainScreen extends React.Component<Props, State> {
<WebSectionList <WebSectionList
createDataset={this.createDataset} createDataset={this.createDataset}
navigation={nav} navigation={nav}
refreshTime={0} autoRefreshTime={0}
refreshOnFocus={false}
fetchUrl={DATA_URL} fetchUrl={DATA_URL}
renderItem={this.getRenderItem}/> renderItem={this.getRenderItem}/>
); );

View file

@ -363,10 +363,11 @@ export default class ProxiwashScreen extends React.Component<Props, State> {
<WebSectionList <WebSectionList
createDataset={this.createDataset} createDataset={this.createDataset}
navigation={nav} navigation={nav}
refreshTime={REFRESH_TIME}
fetchUrl={DATA_URL} fetchUrl={DATA_URL}
renderItem={this.getRenderItem} renderItem={this.getRenderItem}
renderSectionHeader={this.getRenderSectionHeader}/> renderSectionHeader={this.getRenderSectionHeader}
autoRefreshTime={REFRESH_TIME}
refreshOnFocus={true}/>
</View> </View>
); );

View file

@ -168,7 +168,8 @@ export default class SelfMenuScreen extends React.Component<Props> {
<WebSectionList <WebSectionList
createDataset={this.createDataset} createDataset={this.createDataset}
navigation={nav} navigation={nav}
refreshTime={0} autoRefreshTime={0}
refreshOnFocus={false}
fetchUrl={DATA_URL} fetchUrl={DATA_URL}
renderItem={this.getRenderItem} renderItem={this.getRenderItem}
renderSectionHeader={this.getRenderSectionHeader} renderSectionHeader={this.getRenderSectionHeader}