|
@@ -13,9 +13,7 @@ import Touchable from "react-native-platform-touchable";
|
13
|
13
|
import AsyncStorageManager from "../utils/AsyncStorageManager";
|
14
|
14
|
import * as Expo from "expo";
|
15
|
15
|
|
16
|
|
-const DATA_URL = "https://srv-falcon.etud.insa-toulouse.fr/~vergnet/appli-amicale/washinsa/washinsa.json";
|
17
|
|
-
|
18
|
|
-let reminderNotifTime = 5;
|
|
16
|
+const DATA_URL = "https://srv-falcon.etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
|
19
|
17
|
|
20
|
18
|
const MACHINE_STATES = {
|
21
|
19
|
"TERMINE": "0",
|
|
@@ -43,7 +41,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
43
|
41
|
* Creates machine state parameters using current theme and translations
|
44
|
42
|
*/
|
45
|
43
|
constructor() {
|
46
|
|
- super(DATA_URL, 1000 * 60); // Refresh every minute
|
|
44
|
+ super(DATA_URL, 1000 * 30); // Refresh every half minute
|
47
|
45
|
let colors = ThemeManager.getCurrentThemeVariables();
|
48
|
46
|
stateColors[MACHINE_STATES.TERMINE] = colors.proxiwashFinishedColor;
|
49
|
47
|
stateColors[MACHINE_STATES.DISPONIBLE] = colors.proxiwashReadyColor;
|
|
@@ -69,13 +67,15 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
69
|
67
|
stateIcons[MACHINE_STATES.HS] = 'alert-octagram-outline';
|
70
|
68
|
stateIcons[MACHINE_STATES.ERREUR] = 'alert';
|
71
|
69
|
|
72
|
|
- let dataString = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.current;
|
|
70
|
+ // let dataString = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.current;
|
73
|
71
|
this.state = {
|
74
|
72
|
refreshing: false,
|
75
|
73
|
firstLoading: true,
|
76
|
74
|
fetchedData: {},
|
77
|
|
- machinesWatched: JSON.parse(dataString),
|
|
75
|
+ // machinesWatched: JSON.parse(dataString),
|
|
76
|
+ machinesWatched: [],
|
78
|
77
|
};
|
|
78
|
+ this.setMinTimeRefresh(30);
|
79
|
79
|
}
|
80
|
80
|
|
81
|
81
|
/**
|
|
@@ -90,14 +90,6 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
90
|
90
|
vibrate: [0, 250, 250, 250],
|
91
|
91
|
});
|
92
|
92
|
}
|
93
|
|
- // Remove machine from watch list when receiving last notification
|
94
|
|
- Expo.Notifications.addListener((notification) => {
|
95
|
|
- if (notification.data !== undefined) {
|
96
|
|
- if (this.isMachineWatched(notification.data.id) && notification.data.isMachineFinished === true) {
|
97
|
|
- this.removeNotificationFromPrefs(this.getMachineIndexInWatchList(notification.data.id));
|
98
|
|
- }
|
99
|
|
- }
|
100
|
|
- });
|
101
|
93
|
}
|
102
|
94
|
|
103
|
95
|
getHeaderTranslation() {
|
|
@@ -140,49 +132,16 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
140
|
132
|
* Another will be send a few minutes before the end, based on the value of reminderNotifTime
|
141
|
133
|
*
|
142
|
134
|
* @param machineId The machine's ID
|
143
|
|
- * @param remainingTime The time remaining for this machine
|
144
|
135
|
* @returns {Promise<void>}
|
145
|
136
|
*/
|
146
|
|
- async setupNotifications(machineId: string, remainingTime: number) {
|
|
137
|
+ setupNotifications(machineId: string) {
|
147
|
138
|
if (!this.isMachineWatched(machineId)) {
|
148
|
|
- let endNotificationID = await NotificationsManager.scheduleNotification(
|
149
|
|
- i18n.t('proxiwashScreen.notifications.machineFinishedTitle'),
|
150
|
|
- i18n.t('proxiwashScreen.notifications.machineFinishedBody', {number: machineId}),
|
151
|
|
- new Date().getTime() + remainingTime * (60 * 1000), // Convert back to milliseconds
|
152
|
|
- {id: machineId, isMachineFinished: true},
|
153
|
|
- 'reminders'
|
154
|
|
- );
|
155
|
|
- let reminderNotificationID = await ProxiwashScreen.setupReminderNotification(machineId, remainingTime);
|
156
|
|
- this.saveNotificationToPrefs(machineId, endNotificationID, reminderNotificationID);
|
|
139
|
+ NotificationsManager.setupMachineNotification(machineId, true);
|
|
140
|
+ this.saveNotificationToPrefs(machineId);
|
157
|
141
|
} else
|
158
|
142
|
this.disableNotification(machineId);
|
159
|
143
|
}
|
160
|
144
|
|
161
|
|
- static async setupReminderNotification(machineId: string, remainingTime: number): Promise<string | null> {
|
162
|
|
- let reminderNotificationID: string | null = null;
|
163
|
|
- let reminderNotificationTime = ProxiwashScreen.getReminderNotificationTime();
|
164
|
|
- if (remainingTime > reminderNotificationTime && reminderNotificationTime > 0) {
|
165
|
|
- reminderNotificationID = await NotificationsManager.scheduleNotification(
|
166
|
|
- i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: reminderNotificationTime}),
|
167
|
|
- i18n.t('proxiwashScreen.notifications.machineRunningBody', {number: machineId}),
|
168
|
|
- new Date().getTime() + (remainingTime - reminderNotificationTime) * (60 * 1000), // Convert back to milliseconds
|
169
|
|
- {id: machineId, isMachineFinished: false},
|
170
|
|
- 'reminders'
|
171
|
|
- );
|
172
|
|
- }
|
173
|
|
- return reminderNotificationID;
|
174
|
|
- }
|
175
|
|
-
|
176
|
|
- static getReminderNotificationTime(): number {
|
177
|
|
- let val = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current;
|
178
|
|
- if (val !== "never")
|
179
|
|
- reminderNotifTime = parseInt(val);
|
180
|
|
- else
|
181
|
|
- reminderNotifTime = -1;
|
182
|
|
- return reminderNotifTime;
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
-
|
186
|
145
|
/**
|
187
|
146
|
* Stop scheduled notifications for the machine of the given ID.
|
188
|
147
|
* This will also remove the notification if it was already shown.
|
|
@@ -192,43 +151,22 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
192
|
151
|
disableNotification(machineId: string) {
|
193
|
152
|
let data = this.state.machinesWatched;
|
194
|
153
|
if (data.length > 0) {
|
195
|
|
- let arrayIndex = this.getMachineIndexInWatchList(machineId);
|
|
154
|
+ let arrayIndex = data.indexOf(machineId);
|
196
|
155
|
if (arrayIndex !== -1) {
|
197
|
|
- NotificationsManager.cancelScheduledNotification(data[arrayIndex].endNotificationID);
|
198
|
|
- if (data[arrayIndex].reminderNotificationID !== null)
|
199
|
|
- NotificationsManager.cancelScheduledNotification(data[arrayIndex].reminderNotificationID);
|
|
156
|
+ NotificationsManager.setupMachineNotification(machineId, false);
|
200
|
157
|
this.removeNotificationFromPrefs(arrayIndex);
|
201
|
158
|
}
|
202
|
159
|
}
|
203
|
160
|
}
|
204
|
161
|
|
205
|
162
|
/**
|
206
|
|
- * Get the index of the given machine ID in the watchlist array
|
207
|
|
- *
|
208
|
|
- * @param machineId
|
209
|
|
- * @return
|
210
|
|
- */
|
211
|
|
- getMachineIndexInWatchList(machineId: string): number {
|
212
|
|
- let elem = this.state.machinesWatched.find(function (elem) {
|
213
|
|
- return elem.machineNumber === machineId
|
214
|
|
- });
|
215
|
|
- return this.state.machinesWatched.indexOf(elem);
|
216
|
|
- }
|
217
|
|
-
|
218
|
|
- /**
|
219
|
163
|
* Add the given notifications associated to a machine ID to the watchlist, and save the array to the preferences
|
220
|
164
|
*
|
221
|
165
|
* @param machineId
|
222
|
|
- * @param endNotificationID
|
223
|
|
- * @param reminderNotificationID
|
224
|
166
|
*/
|
225
|
|
- saveNotificationToPrefs(machineId: string, endNotificationID: string, reminderNotificationID: string | null) {
|
|
167
|
+ saveNotificationToPrefs(machineId: string) {
|
226
|
168
|
let data = this.state.machinesWatched;
|
227
|
|
- data.push({
|
228
|
|
- machineNumber: machineId,
|
229
|
|
- endNotificationID: endNotificationID,
|
230
|
|
- reminderNotificationID: reminderNotificationID
|
231
|
|
- });
|
|
169
|
+ data.push(machineId);
|
232
|
170
|
this.updateNotificationPrefs(data);
|
233
|
171
|
}
|
234
|
172
|
|
|
@@ -250,8 +188,8 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
250
|
188
|
*/
|
251
|
189
|
updateNotificationPrefs(data: Array<Object>) {
|
252
|
190
|
this.setState({machinesWatched: data});
|
253
|
|
- let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key;
|
254
|
|
- AsyncStorageManager.getInstance().savePref(prefKey, JSON.stringify(data));
|
|
191
|
+ // let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key;
|
|
192
|
+ // AsyncStorageManager.getInstance().savePref(prefKey, JSON.stringify(data));
|
255
|
193
|
}
|
256
|
194
|
|
257
|
195
|
/**
|
|
@@ -261,9 +199,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
261
|
199
|
* @returns {boolean}
|
262
|
200
|
*/
|
263
|
201
|
isMachineWatched(machineID: string) {
|
264
|
|
- return this.state.machinesWatched.find(function (elem) {
|
265
|
|
- return elem.machineNumber === machineID
|
266
|
|
- }) !== undefined;
|
|
202
|
+ return this.state.machinesWatched.indexOf(machineID) !== -1;
|
267
|
203
|
}
|
268
|
204
|
|
269
|
205
|
createDataset(fetchedData: Object) {
|
|
@@ -307,7 +243,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
307
|
243
|
text: this.isMachineWatched(item.number) ?
|
308
|
244
|
i18n.t("proxiwashScreen.modal.disableNotifications") :
|
309
|
245
|
i18n.t("proxiwashScreen.modal.enableNotifications"),
|
310
|
|
- onPress: () => this.setupNotifications(item.number, remainingTime)
|
|
246
|
+ onPress: () => this.setupNotifications(item.number)
|
311
|
247
|
},
|
312
|
248
|
{
|
313
|
249
|
text: i18n.t("proxiwashScreen.modal.cancel")
|