Block refresh to once every minute

This commit is contained in:
keplyx 2019-08-08 18:40:42 +02:00
parent aba52782d0
commit 8e03b6b8c2
2 changed files with 33 additions and 10 deletions

View file

@ -20,6 +20,8 @@ type State = {
machinesWatched: Array<Object>, machinesWatched: Array<Object>,
}; };
const minTimeBetweenRefresh = 60;
/** /**
* Class used to create a basic list view using online json data. * Class used to create a basic list view using online json data.
* Used by inheriting from it and redefining getters. * Used by inheriting from it and redefining getters.
@ -32,6 +34,7 @@ export default class FetchedDataSectionList extends React.Component<Props, State
willBlurSubscription: function; willBlurSubscription: function;
refreshInterval: IntervalID; refreshInterval: IntervalID;
refreshTime: number; refreshTime: number;
lastRefresh: Date;
constructor(fetchUrl: string, refreshTime: number) { constructor(fetchUrl: string, refreshTime: number) {
super(); super();
@ -112,15 +115,33 @@ export default class FetchedDataSectionList extends React.Component<Props, State
* @private * @private
*/ */
_onRefresh = () => { _onRefresh = () => {
let canRefresh;
if (this.lastRefresh !== undefined)
canRefresh = (new Date().getTime() - this.lastRefresh.getTime())/1000 > minTimeBetweenRefresh;
else
canRefresh = true;
if (canRefresh) {
this.setState({refreshing: true}); this.setState({refreshing: true});
this.webDataManager.readData().then((fetchedData) => { this.webDataManager.readData()
.then((fetchedData) => {
this.setState({ this.setState({
fetchedData: fetchedData, fetchedData: fetchedData,
refreshing: false, refreshing: false,
firstLoading: false firstLoading: false
}); });
this.lastRefresh = new Date();
})
.catch((err) => {
this.setState({
fetchedData: {},
refreshing: false,
firstLoading: false
});
this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]); this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
}); });
}
}; };
/** /**
@ -316,7 +337,8 @@ export default class FetchedDataSectionList extends React.Component<Props, State
const nav = this.props.navigation; const nav = this.props.navigation;
const dataset = this.createDataset(this.state.fetchedData); const dataset = this.createDataset(this.state.fetchedData);
return ( return (
<BaseContainer navigation={nav} headerTitle={this.getHeaderTranslation()} headerRightButton={this.getRightButton()}> <BaseContainer navigation={nav} headerTitle={this.getHeaderTranslation()}
headerRightButton={this.getRightButton()}>
{this.hasTabs() ? {this.hasTabs() ?
<Tabs> <Tabs>
{this.getTabbedView(dataset)} {this.getTabbedView(dataset)}

View file

@ -27,6 +27,7 @@ export default class WebDataManager {
} catch (error) { } catch (error) {
console.log('Could not read FetchedData from server'); console.log('Could not read FetchedData from server');
console.log(error); console.log(error);
throw new Error('Could not read FetchedData from server');
} }
this.lastDataFetched = fetchedData; this.lastDataFetched = fetchedData;
return fetchedData; return fetchedData;