|
@@ -6,7 +6,6 @@ import i18n from "i18n-js";
|
6
|
6
|
import WebSectionList from "../../components/Screens/WebSectionList";
|
7
|
7
|
import * as Notifications from "../../utils/Notifications";
|
8
|
8
|
import AsyncStorageManager from "../../managers/AsyncStorageManager";
|
9
|
|
-// import * as Expo from "expo";
|
10
|
9
|
import {Avatar, Banner, Button, Card, Text, withTheme} from 'react-native-paper';
|
11
|
10
|
import ProxiwashListItem from "../../components/Lists/Proxiwash/ProxiwashListItem";
|
12
|
11
|
import ProxiwashConstants from "../../constants/ProxiwashConstants";
|
|
@@ -15,6 +14,11 @@ import AprilFoolsManager from "../../managers/AprilFoolsManager";
|
15
|
14
|
import MaterialHeaderButtons, {Item} from "../../components/Overrides/CustomHeaderButton";
|
16
|
15
|
import ProxiwashSectionHeader from "../../components/Lists/Proxiwash/ProxiwashSectionHeader";
|
17
|
16
|
import {withCollapsible} from "../../utils/withCollapsible";
|
|
17
|
+import type {CustomTheme} from "../../managers/ThemeManager";
|
|
18
|
+import {Collapsible} from "react-navigation-collapsible";
|
|
19
|
+import {StackNavigationProp} from "@react-navigation/stack";
|
|
20
|
+
|
|
21
|
+const PushNotification = require("react-native-push-notification");
|
18
|
22
|
|
19
|
23
|
const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
|
20
|
24
|
|
|
@@ -23,17 +27,25 @@ let modalStateStrings = {};
|
23
|
27
|
const REFRESH_TIME = 1000 * 10; // Refresh every 10 seconds
|
24
|
28
|
const LIST_ITEM_HEIGHT = 64;
|
25
|
29
|
|
|
30
|
+type machine = {
|
|
31
|
+ number: string,
|
|
32
|
+ state: string,
|
|
33
|
+ startTime: string,
|
|
34
|
+ endTime: string,
|
|
35
|
+ donePercent: string,
|
|
36
|
+ remainingTime: string,
|
|
37
|
+}
|
|
38
|
+
|
26
|
39
|
type Props = {
|
27
|
|
- navigation: Object,
|
28
|
|
- route: Object,
|
29
|
|
- theme: Object,
|
30
|
|
- collapsibleStack: Object,
|
|
40
|
+ navigation: StackNavigationProp,
|
|
41
|
+ theme: CustomTheme,
|
|
42
|
+ collapsibleStack: Collapsible,
|
31
|
43
|
}
|
32
|
44
|
|
33
|
45
|
type State = {
|
34
|
46
|
refreshing: boolean,
|
35
|
47
|
modalCurrentDisplayItem: React.Node,
|
36
|
|
- machinesWatched: Array<string>,
|
|
48
|
+ machinesWatched: Array<machine>,
|
37
|
49
|
bannerVisible: boolean,
|
38
|
50
|
};
|
39
|
51
|
|
|
@@ -47,11 +59,12 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
47
|
59
|
modalRef: Object;
|
48
|
60
|
|
49
|
61
|
fetchedData: Object;
|
|
62
|
+ allMachines: Array<machine>;
|
50
|
63
|
|
51
|
64
|
state = {
|
52
|
65
|
refreshing: false,
|
53
|
66
|
modalCurrentDisplayItem: null,
|
54
|
|
- machinesWatched: [],
|
|
67
|
+ machinesWatched: JSON.parse(AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.current),
|
55
|
68
|
bannerVisible: AsyncStorageManager.getInstance().preferences.proxiwashShowBanner.current === '1',
|
56
|
69
|
};
|
57
|
70
|
|
|
@@ -65,6 +78,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
65
|
78
|
modalStateStrings[ProxiwashConstants.machineStates["EN COURS"]] = i18n.t('proxiwashScreen.modal.running');
|
66
|
79
|
modalStateStrings[ProxiwashConstants.machineStates.HS] = i18n.t('proxiwashScreen.modal.broken');
|
67
|
80
|
modalStateStrings[ProxiwashConstants.machineStates.ERREUR] = i18n.t('proxiwashScreen.modal.error');
|
|
81
|
+ this.allMachines = [];
|
68
|
82
|
}
|
69
|
83
|
|
70
|
84
|
/**
|
|
@@ -86,25 +100,6 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
86
|
100
|
this.props.navigation.setOptions({
|
87
|
101
|
headerRight: this.getAboutButton,
|
88
|
102
|
});
|
89
|
|
- if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
|
90
|
|
- // Get latest watchlist from server
|
91
|
|
- Notifications.getMachineNotificationWatchlist((fetchedList) => {
|
92
|
|
- this.setState({machinesWatched: fetchedList})
|
93
|
|
- });
|
94
|
|
- // Get updated watchlist after received notification
|
95
|
|
- // Expo.Notifications.addListener(() => {
|
96
|
|
- // Notifications.getMachineNotificationWatchlist((fetchedList) => {
|
97
|
|
- // this.setState({machinesWatched: fetchedList})
|
98
|
|
- // });
|
99
|
|
- // });
|
100
|
|
- // if (Platform.OS === 'android') {
|
101
|
|
- // Expo.Notifications.createChannelAndroidAsync('reminders', {
|
102
|
|
- // name: 'Reminders',
|
103
|
|
- // priority: 'max',
|
104
|
|
- // vibrate: [0, 250, 250, 250],
|
105
|
|
- // });
|
106
|
|
- // }
|
107
|
|
- }
|
108
|
103
|
}
|
109
|
104
|
|
110
|
105
|
/**
|
|
@@ -129,8 +124,16 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
129
|
124
|
* @param item The item to extract the key from
|
130
|
125
|
* @return {*} The extracted key
|
131
|
126
|
*/
|
132
|
|
- getKeyExtractor(item: Object) {
|
133
|
|
- return item !== undefined ? item.number : undefined;
|
|
127
|
+ getKeyExtractor = (item: machine) => item.number;
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+ getMachineEndDate(machine: machine) {
|
|
131
|
+ const array = machine.endTime.split(":");
|
|
132
|
+ let date = new Date();
|
|
133
|
+ date.setHours(parseInt(array[0]), parseInt(array[1]));
|
|
134
|
+ if (date < new Date())
|
|
135
|
+ date.setDate(date.getDate() + 1);
|
|
136
|
+ return date;
|
134
|
137
|
}
|
135
|
138
|
|
136
|
139
|
/**
|
|
@@ -138,18 +141,23 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
138
|
141
|
* One notification will be sent at the end of the program.
|
139
|
142
|
* Another will be send a few minutes before the end, based on the value of reminderNotifTime
|
140
|
143
|
*
|
141
|
|
- * @param machineId The machine's ID
|
|
144
|
+ * @param machine The machine to watch
|
142
|
145
|
* @returns {Promise<void>}
|
143
|
146
|
*/
|
144
|
|
- setupNotifications(machineId: string) {
|
145
|
|
- if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
|
146
|
|
- if (!this.isMachineWatched(machineId)) {
|
147
|
|
- Notifications.setupMachineNotification(machineId, true);
|
148
|
|
- this.saveNotificationToState(machineId);
|
149
|
|
- } else
|
150
|
|
- this.disableNotification(machineId);
|
|
147
|
+ setupNotifications(machine: machine) {
|
|
148
|
+ if (!this.isMachineWatched(machine)) {
|
|
149
|
+ Notifications.setupMachineNotification(machine.number, true, this.getMachineEndDate(machine))
|
|
150
|
+ .then(() => {
|
|
151
|
+ this.saveNotificationToState(machine);
|
|
152
|
+ })
|
|
153
|
+ .catch(() => {
|
|
154
|
+ this.showNotificationsDisabledWarning();
|
|
155
|
+ });
|
151
|
156
|
} else {
|
152
|
|
- this.showNotificationsDisabledWarning();
|
|
157
|
+ Notifications.setupMachineNotification(machine.number, false)
|
|
158
|
+ .then(() => {
|
|
159
|
+ this.removeNotificationFromState(machine);
|
|
160
|
+ });
|
153
|
161
|
}
|
154
|
162
|
}
|
155
|
163
|
|
|
@@ -164,61 +172,78 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
164
|
172
|
}
|
165
|
173
|
|
166
|
174
|
/**
|
167
|
|
- * Stops scheduled notifications for the machine of the given ID.
|
168
|
|
- * This will also remove the notification if it was already shown.
|
169
|
|
- *
|
170
|
|
- * @param machineId The machine's ID
|
171
|
|
- */
|
172
|
|
- disableNotification(machineId: string) {
|
173
|
|
- let data = this.state.machinesWatched;
|
174
|
|
- if (data.length > 0) {
|
175
|
|
- let arrayIndex = data.indexOf(machineId);
|
176
|
|
- if (arrayIndex !== -1) {
|
177
|
|
- Notifications.setupMachineNotification(machineId, false);
|
178
|
|
- this.removeNotificationFroState(arrayIndex);
|
179
|
|
- }
|
180
|
|
- }
|
181
|
|
- }
|
182
|
|
-
|
183
|
|
- /**
|
184
|
175
|
* Adds the given notifications associated to a machine ID to the watchlist, and saves the array to the preferences
|
185
|
176
|
*
|
186
|
|
- * @param machineId
|
|
177
|
+ * @param machine
|
187
|
178
|
*/
|
188
|
|
- saveNotificationToState(machineId: string) {
|
|
179
|
+ saveNotificationToState(machine: machine) {
|
189
|
180
|
let data = this.state.machinesWatched;
|
190
|
|
- data.push(machineId);
|
191
|
|
- this.updateNotificationState(data);
|
|
181
|
+ data.push(machine);
|
|
182
|
+ this.saveNewWatchedList(data);
|
192
|
183
|
}
|
193
|
184
|
|
194
|
185
|
/**
|
195
|
186
|
* Removes the given index from the watchlist array and saves it to preferences
|
196
|
187
|
*
|
197
|
|
- * @param index
|
|
188
|
+ * @param machine
|
198
|
189
|
*/
|
199
|
|
- removeNotificationFroState(index: number) {
|
|
190
|
+ removeNotificationFromState(machine: machine) {
|
200
|
191
|
let data = this.state.machinesWatched;
|
201
|
|
- data.splice(index, 1);
|
202
|
|
- this.updateNotificationState(data);
|
|
192
|
+ for (let i = 0; i < data.length; i++) {
|
|
193
|
+ if (data[i].number === machine.number && data[i].endTime === machine.endTime) {
|
|
194
|
+ data.splice(i, 1);
|
|
195
|
+ break;
|
|
196
|
+ }
|
|
197
|
+ }
|
|
198
|
+ this.saveNewWatchedList(data);
|
203
|
199
|
}
|
204
|
200
|
|
205
|
|
- /**
|
206
|
|
- * Sets the given fetchedData as the watchlist
|
207
|
|
- *
|
208
|
|
- * @param data
|
209
|
|
- */
|
210
|
|
- updateNotificationState(data: Array<Object>) {
|
211
|
|
- this.setState({machinesWatched: data});
|
|
201
|
+ saveNewWatchedList(list: Array<machine>) {
|
|
202
|
+ this.setState({machinesWatched: list});
|
|
203
|
+ AsyncStorageManager.getInstance().savePref(
|
|
204
|
+ AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key,
|
|
205
|
+ JSON.stringify(list),
|
|
206
|
+ );
|
212
|
207
|
}
|
213
|
208
|
|
214
|
209
|
/**
|
215
|
210
|
* Checks whether the machine of the given ID has scheduled notifications
|
216
|
211
|
*
|
217
|
|
- * @param machineID The machine's ID
|
|
212
|
+ * @param machine
|
218
|
213
|
* @returns {boolean}
|
219
|
214
|
*/
|
220
|
|
- isMachineWatched(machineID: string) {
|
221
|
|
- return this.state.machinesWatched.indexOf(machineID) !== -1;
|
|
215
|
+ isMachineWatched(machine: machine) {
|
|
216
|
+ let watched = false;
|
|
217
|
+ const list = this.state.machinesWatched;
|
|
218
|
+ for (let i = 0; i < list.length; i++) {
|
|
219
|
+ if (list[i].number === machine.number && list[i].endTime === machine.endTime) {
|
|
220
|
+ watched = true;
|
|
221
|
+ break;
|
|
222
|
+ }
|
|
223
|
+ }
|
|
224
|
+ return watched;
|
|
225
|
+ }
|
|
226
|
+
|
|
227
|
+ getMachineOfId(id: string) {
|
|
228
|
+ for (let i = 0; i < this.allMachines.length; i++) {
|
|
229
|
+ if (this.allMachines[i].number === id)
|
|
230
|
+ return this.allMachines[i];
|
|
231
|
+ }
|
|
232
|
+ return null;
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ getCleanedMachineWatched() {
|
|
236
|
+ const list = this.state.machinesWatched;
|
|
237
|
+ let newList = [];
|
|
238
|
+ for (let i = 0; i < list.length; i++) {
|
|
239
|
+ let machine = this.getMachineOfId(list[i].number);
|
|
240
|
+ if (machine !== null
|
|
241
|
+ && list[i].number === machine.number && list[i].endTime === machine.endTime
|
|
242
|
+ && ProxiwashConstants.machineStates[list[i].state] === ProxiwashConstants.machineStates["EN COURS"]) {
|
|
243
|
+ newList.push(machine);
|
|
244
|
+ }
|
|
245
|
+ }
|
|
246
|
+ return newList;
|
222
|
247
|
}
|
223
|
248
|
|
224
|
249
|
/**
|
|
@@ -234,8 +259,9 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
234
|
259
|
AprilFoolsManager.getNewProxiwashDryerOrderedList(data.dryers);
|
235
|
260
|
AprilFoolsManager.getNewProxiwashWasherOrderedList(data.washers);
|
236
|
261
|
}
|
237
|
|
- this.fetchedData = fetchedData;
|
238
|
|
-
|
|
262
|
+ this.fetchedData = data;
|
|
263
|
+ this.allMachines = [...data.dryers, ...data.washers];
|
|
264
|
+ this.state.machinesWatched = this.getCleanedMachineWatched();
|
239
|
265
|
return [
|
240
|
266
|
{
|
241
|
267
|
title: i18n.t('proxiwashScreen.dryers'),
|
|
@@ -271,13 +297,13 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
271
|
297
|
/**
|
272
|
298
|
* Callback used when the user clicks on enable notifications for a machine
|
273
|
299
|
*
|
274
|
|
- * @param machineId The machine's id to set notifications for
|
|
300
|
+ * @param machine The machine to set notifications for
|
275
|
301
|
*/
|
276
|
|
- onSetupNotificationsPress(machineId: string) {
|
|
302
|
+ onSetupNotificationsPress(machine: machine) {
|
277
|
303
|
if (this.modalRef) {
|
278
|
304
|
this.modalRef.close();
|
279
|
305
|
}
|
280
|
|
- this.setupNotifications(machineId)
|
|
306
|
+ this.setupNotifications(machine);
|
281
|
307
|
}
|
282
|
308
|
|
283
|
309
|
/**
|
|
@@ -296,7 +322,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
296
|
322
|
onPress: undefined
|
297
|
323
|
};
|
298
|
324
|
let message = modalStateStrings[ProxiwashConstants.machineStates[item.state]];
|
299
|
|
- const onPress = this.onSetupNotificationsPress.bind(this, item.number);
|
|
325
|
+ const onPress = this.onSetupNotificationsPress.bind(this, item);
|
300
|
326
|
if (ProxiwashConstants.machineStates[item.state] === ProxiwashConstants.machineStates["EN COURS"]) {
|
301
|
327
|
button =
|
302
|
328
|
{
|
|
@@ -409,8 +435,8 @@ class ProxiwashScreen extends React.Component<Props, State> {
|
409
|
435
|
return (
|
410
|
436
|
<ProxiwashListItem
|
411
|
437
|
item={item}
|
412
|
|
- onPress={this.showModal}
|
413
|
|
- isWatched={this.isMachineWatched(item.number)}
|
|
438
|
+ onPress={() => this.onSetupNotificationsPress(item)}
|
|
439
|
+ isWatched={this.isMachineWatched(item)}
|
414
|
440
|
isDryer={isDryer}
|
415
|
441
|
height={LIST_ITEM_HEIGHT}
|
416
|
442
|
/>
|