Browse Source

Block refresh to once every minute

keplyx 4 years ago
parent
commit
8e03b6b8c2
2 changed files with 33 additions and 10 deletions
  1. 32
    10
      components/FetchedDataSectionList.js
  2. 1
    0
      utils/WebDataManager.js

+ 32
- 10
components/FetchedDataSectionList.js View File

@@ -20,6 +20,8 @@ type State = {
20 20
     machinesWatched: Array<Object>,
21 21
 };
22 22
 
23
+const minTimeBetweenRefresh = 60;
24
+
23 25
 /**
24 26
  * Class used to create a basic list view using online json data.
25 27
  * Used by inheriting from it and redefining getters.
@@ -32,6 +34,7 @@ export default class FetchedDataSectionList extends React.Component<Props, State
32 34
     willBlurSubscription: function;
33 35
     refreshInterval: IntervalID;
34 36
     refreshTime: number;
37
+    lastRefresh: Date;
35 38
 
36 39
     constructor(fetchUrl: string, refreshTime: number) {
37 40
         super();
@@ -112,15 +115,33 @@ export default class FetchedDataSectionList extends React.Component<Props, State
112 115
      * @private
113 116
      */
114 117
     _onRefresh = () => {
115
-        this.setState({refreshing: true});
116
-        this.webDataManager.readData().then((fetchedData) => {
117
-            this.setState({
118
-                fetchedData: fetchedData,
119
-                refreshing: false,
120
-                firstLoading: false
121
-            });
122
-            this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
123
-        });
118
+        let canRefresh;
119
+        if (this.lastRefresh !== undefined)
120
+            canRefresh = (new Date().getTime() - this.lastRefresh.getTime())/1000 > minTimeBetweenRefresh;
121
+        else
122
+            canRefresh = true;
123
+
124
+        if (canRefresh) {
125
+            this.setState({refreshing: true});
126
+            this.webDataManager.readData()
127
+                .then((fetchedData) => {
128
+                    this.setState({
129
+                        fetchedData: fetchedData,
130
+                        refreshing: false,
131
+                        firstLoading: false
132
+                    });
133
+                    this.lastRefresh = new Date();
134
+                })
135
+                .catch((err) => {
136
+                    this.setState({
137
+                        fetchedData: {},
138
+                        refreshing: false,
139
+                        firstLoading: false
140
+                    });
141
+                    this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
142
+                });
143
+        }
144
+
124 145
     };
125 146
 
126 147
     /**
@@ -316,7 +337,8 @@ export default class FetchedDataSectionList extends React.Component<Props, State
316 337
         const nav = this.props.navigation;
317 338
         const dataset = this.createDataset(this.state.fetchedData);
318 339
         return (
319
-            <BaseContainer navigation={nav} headerTitle={this.getHeaderTranslation()} headerRightButton={this.getRightButton()}>
340
+            <BaseContainer navigation={nav} headerTitle={this.getHeaderTranslation()}
341
+                           headerRightButton={this.getRightButton()}>
320 342
                 {this.hasTabs() ?
321 343
                     <Tabs>
322 344
                         {this.getTabbedView(dataset)}

+ 1
- 0
utils/WebDataManager.js View File

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

Loading…
Cancel
Save