|
@@ -2,421 +2,480 @@
|
2
|
2
|
|
3
|
3
|
import * as React from 'react';
|
4
|
4
|
import {Alert, View} from 'react-native';
|
5
|
|
-import i18n from "i18n-js";
|
6
|
|
-import WebSectionList from "../../components/Screens/WebSectionList";
|
7
|
|
-import * as Notifications from "../../utils/Notifications";
|
8
|
|
-import AsyncStorageManager from "../../managers/AsyncStorageManager";
|
|
5
|
+import i18n from 'i18n-js';
|
9
|
6
|
import {Avatar, Button, Card, Text, withTheme} from 'react-native-paper';
|
10
|
|
-import ProxiwashListItem from "../../components/Lists/Proxiwash/ProxiwashListItem";
|
11
|
|
-import ProxiwashConstants from "../../constants/ProxiwashConstants";
|
12
|
|
-import CustomModal from "../../components/Overrides/CustomModal";
|
13
|
|
-import AprilFoolsManager from "../../managers/AprilFoolsManager";
|
14
|
|
-import MaterialHeaderButtons, {Item} from "../../components/Overrides/CustomHeaderButton";
|
15
|
|
-import ProxiwashSectionHeader from "../../components/Lists/Proxiwash/ProxiwashSectionHeader";
|
16
|
|
-import type {CustomTheme} from "../../managers/ThemeManager";
|
17
|
|
-import {StackNavigationProp} from "@react-navigation/stack";
|
18
|
|
-import {getCleanedMachineWatched, getMachineEndDate, isMachineWatched} from "../../utils/Proxiwash";
|
19
|
|
-import {Modalize} from "react-native-modalize";
|
20
|
|
-import {MASCOT_STYLE} from "../../components/Mascot/Mascot";
|
21
|
|
-import MascotPopup from "../../components/Mascot/MascotPopup";
|
22
|
|
-
|
23
|
|
-const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/v2/washinsa/washinsa_data.json";
|
24
|
|
-
|
25
|
|
-let modalStateStrings = {};
|
|
7
|
+import {StackNavigationProp} from '@react-navigation/stack';
|
|
8
|
+import {Modalize} from 'react-native-modalize';
|
|
9
|
+import WebSectionList from '../../components/Screens/WebSectionList';
|
|
10
|
+import * as Notifications from '../../utils/Notifications';
|
|
11
|
+import AsyncStorageManager from '../../managers/AsyncStorageManager';
|
|
12
|
+import ProxiwashListItem from '../../components/Lists/Proxiwash/ProxiwashListItem';
|
|
13
|
+import ProxiwashConstants from '../../constants/ProxiwashConstants';
|
|
14
|
+import CustomModal from '../../components/Overrides/CustomModal';
|
|
15
|
+import AprilFoolsManager from '../../managers/AprilFoolsManager';
|
|
16
|
+import MaterialHeaderButtons, {
|
|
17
|
+ Item,
|
|
18
|
+} from '../../components/Overrides/CustomHeaderButton';
|
|
19
|
+import ProxiwashSectionHeader from '../../components/Lists/Proxiwash/ProxiwashSectionHeader';
|
|
20
|
+import type {CustomTheme} from '../../managers/ThemeManager';
|
|
21
|
+import {
|
|
22
|
+ getCleanedMachineWatched,
|
|
23
|
+ getMachineEndDate,
|
|
24
|
+ isMachineWatched,
|
|
25
|
+} from '../../utils/Proxiwash';
|
|
26
|
+import {MASCOT_STYLE} from '../../components/Mascot/Mascot';
|
|
27
|
+import MascotPopup from '../../components/Mascot/MascotPopup';
|
|
28
|
+import type {SectionListDataType} from '../../components/Screens/WebSectionList';
|
|
29
|
+
|
|
30
|
+const DATA_URL =
|
|
31
|
+ 'https://etud.insa-toulouse.fr/~amicale_app/v2/washinsa/washinsa_data.json';
|
|
32
|
+
|
|
33
|
+const modalStateStrings = {};
|
26
|
34
|
|
27
|
35
|
const REFRESH_TIME = 1000 * 10; // Refresh every 10 seconds
|
28
|
36
|
const LIST_ITEM_HEIGHT = 64;
|
29
|
37
|
|
30
|
|
-export type Machine = {
|
31
|
|
- number: string,
|
32
|
|
- state: string,
|
33
|
|
- startTime: string,
|
34
|
|
- endTime: string,
|
35
|
|
- donePercent: string,
|
36
|
|
- remainingTime: string,
|
37
|
|
- program: string,
|
38
|
|
-}
|
39
|
|
-
|
40
|
|
-type Props = {
|
41
|
|
- navigation: StackNavigationProp,
|
42
|
|
- theme: CustomTheme,
|
43
|
|
-}
|
|
38
|
+export type ProxiwashMachineType = {
|
|
39
|
+ number: string,
|
|
40
|
+ state: string,
|
|
41
|
+ startTime: string,
|
|
42
|
+ endTime: string,
|
|
43
|
+ donePercent: string,
|
|
44
|
+ remainingTime: string,
|
|
45
|
+ program: string,
|
|
46
|
+};
|
44
|
47
|
|
45
|
|
-type State = {
|
46
|
|
- refreshing: boolean,
|
47
|
|
- modalCurrentDisplayItem: React.Node,
|
48
|
|
- machinesWatched: Array<Machine>,
|
|
48
|
+type PropsType = {
|
|
49
|
+ navigation: StackNavigationProp,
|
|
50
|
+ theme: CustomTheme,
|
49
|
51
|
};
|
50
|
52
|
|
|
53
|
+type StateType = {
|
|
54
|
+ modalCurrentDisplayItem: React.Node,
|
|
55
|
+ machinesWatched: Array<ProxiwashMachineType>,
|
|
56
|
+};
|
51
|
57
|
|
52
|
58
|
/**
|
53
|
59
|
* Class defining the app's proxiwash screen. This screen shows information about washing machines and
|
54
|
60
|
* dryers, taken from a scrapper reading proxiwash website
|
55
|
61
|
*/
|
56
|
|
-class ProxiwashScreen extends React.Component<Props, State> {
|
57
|
|
-
|
58
|
|
- modalRef: null | Modalize;
|
59
|
|
-
|
60
|
|
- fetchedData: {
|
61
|
|
- dryers: Array<Machine>,
|
62
|
|
- washers: Array<Machine>,
|
|
62
|
+class ProxiwashScreen extends React.Component<PropsType, StateType> {
|
|
63
|
+ /**
|
|
64
|
+ * Shows a warning telling the user notifications are disabled for the app
|
|
65
|
+ */
|
|
66
|
+ static showNotificationsDisabledWarning() {
|
|
67
|
+ Alert.alert(
|
|
68
|
+ i18n.t('screens.proxiwash.modal.notificationErrorTitle'),
|
|
69
|
+ i18n.t('screens.proxiwash.modal.notificationErrorDescription'),
|
|
70
|
+ );
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ modalRef: null | Modalize;
|
|
74
|
+
|
|
75
|
+ fetchedData: {
|
|
76
|
+ dryers: Array<ProxiwashMachineType>,
|
|
77
|
+ washers: Array<ProxiwashMachineType>,
|
|
78
|
+ };
|
|
79
|
+
|
|
80
|
+ /**
|
|
81
|
+ * Creates machine state parameters using current theme and translations
|
|
82
|
+ */
|
|
83
|
+ constructor() {
|
|
84
|
+ super();
|
|
85
|
+ this.state = {
|
|
86
|
+ modalCurrentDisplayItem: null,
|
|
87
|
+ machinesWatched: AsyncStorageManager.getObject(
|
|
88
|
+ AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key,
|
|
89
|
+ ),
|
63
|
90
|
};
|
64
|
|
-
|
65
|
|
- state = {
|
66
|
|
- refreshing: false,
|
67
|
|
- modalCurrentDisplayItem: null,
|
68
|
|
- machinesWatched: AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key),
|
|
91
|
+ modalStateStrings[ProxiwashConstants.machineStates.AVAILABLE] = i18n.t(
|
|
92
|
+ 'screens.proxiwash.modal.ready',
|
|
93
|
+ );
|
|
94
|
+ modalStateStrings[ProxiwashConstants.machineStates.RUNNING] = i18n.t(
|
|
95
|
+ 'screens.proxiwash.modal.running',
|
|
96
|
+ );
|
|
97
|
+ modalStateStrings[
|
|
98
|
+ ProxiwashConstants.machineStates.RUNNING_NOT_STARTED
|
|
99
|
+ ] = i18n.t('screens.proxiwash.modal.runningNotStarted');
|
|
100
|
+ modalStateStrings[ProxiwashConstants.machineStates.FINISHED] = i18n.t(
|
|
101
|
+ 'screens.proxiwash.modal.finished',
|
|
102
|
+ );
|
|
103
|
+ modalStateStrings[ProxiwashConstants.machineStates.UNAVAILABLE] = i18n.t(
|
|
104
|
+ 'screens.proxiwash.modal.broken',
|
|
105
|
+ );
|
|
106
|
+ modalStateStrings[ProxiwashConstants.machineStates.ERROR] = i18n.t(
|
|
107
|
+ 'screens.proxiwash.modal.error',
|
|
108
|
+ );
|
|
109
|
+ modalStateStrings[ProxiwashConstants.machineStates.UNKNOWN] = i18n.t(
|
|
110
|
+ 'screens.proxiwash.modal.unknown',
|
|
111
|
+ );
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ /**
|
|
115
|
+ * Setup notification channel for android and add listeners to detect notifications fired
|
|
116
|
+ */
|
|
117
|
+ componentDidMount() {
|
|
118
|
+ const {navigation} = this.props;
|
|
119
|
+ navigation.setOptions({
|
|
120
|
+ headerRight: (): React.Node => (
|
|
121
|
+ <MaterialHeaderButtons>
|
|
122
|
+ <Item
|
|
123
|
+ title="information"
|
|
124
|
+ iconName="information"
|
|
125
|
+ onPress={this.onAboutPress}
|
|
126
|
+ />
|
|
127
|
+ </MaterialHeaderButtons>
|
|
128
|
+ ),
|
|
129
|
+ });
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ /**
|
|
133
|
+ * Callback used when pressing the about button.
|
|
134
|
+ * This will open the ProxiwashAboutScreen.
|
|
135
|
+ */
|
|
136
|
+ onAboutPress = () => {
|
|
137
|
+ const {navigation} = this.props;
|
|
138
|
+ navigation.navigate('proxiwash-about');
|
|
139
|
+ };
|
|
140
|
+
|
|
141
|
+ /**
|
|
142
|
+ * Callback used when the user clicks on enable notifications for a machine
|
|
143
|
+ *
|
|
144
|
+ * @param machine The machine to set notifications for
|
|
145
|
+ */
|
|
146
|
+ onSetupNotificationsPress(machine: ProxiwashMachineType) {
|
|
147
|
+ if (this.modalRef) {
|
|
148
|
+ this.modalRef.close();
|
|
149
|
+ }
|
|
150
|
+ this.setupNotifications(machine);
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ /**
|
|
154
|
+ * Callback used when receiving modal ref
|
|
155
|
+ *
|
|
156
|
+ * @param ref
|
|
157
|
+ */
|
|
158
|
+ onModalRef = (ref: Modalize) => {
|
|
159
|
+ this.modalRef = ref;
|
|
160
|
+ };
|
|
161
|
+
|
|
162
|
+ /**
|
|
163
|
+ * Generates the modal content.
|
|
164
|
+ * This shows information for the given machine.
|
|
165
|
+ *
|
|
166
|
+ * @param title The title to use
|
|
167
|
+ * @param item The item to display information for in the modal
|
|
168
|
+ * @param isDryer True if the given item is a dryer
|
|
169
|
+ * @return {*}
|
|
170
|
+ */
|
|
171
|
+ getModalContent(
|
|
172
|
+ title: string,
|
|
173
|
+ item: ProxiwashMachineType,
|
|
174
|
+ isDryer: boolean,
|
|
175
|
+ ): React.Node {
|
|
176
|
+ const {props, state} = this;
|
|
177
|
+ let button = {
|
|
178
|
+ text: i18n.t('screens.proxiwash.modal.ok'),
|
|
179
|
+ icon: '',
|
|
180
|
+ onPress: undefined,
|
69
|
181
|
};
|
70
|
|
-
|
71
|
|
- /**
|
72
|
|
- * Creates machine state parameters using current theme and translations
|
73
|
|
- */
|
74
|
|
- constructor(props) {
|
75
|
|
- super(props);
|
76
|
|
- modalStateStrings[ProxiwashConstants.machineStates.AVAILABLE] = i18n.t('screens.proxiwash.modal.ready');
|
77
|
|
- modalStateStrings[ProxiwashConstants.machineStates.RUNNING] = i18n.t('screens.proxiwash.modal.running');
|
78
|
|
- modalStateStrings[ProxiwashConstants.machineStates.RUNNING_NOT_STARTED] = i18n.t('screens.proxiwash.modal.runningNotStarted');
|
79
|
|
- modalStateStrings[ProxiwashConstants.machineStates.FINISHED] = i18n.t('screens.proxiwash.modal.finished');
|
80
|
|
- modalStateStrings[ProxiwashConstants.machineStates.UNAVAILABLE] = i18n.t('screens.proxiwash.modal.broken');
|
81
|
|
- modalStateStrings[ProxiwashConstants.machineStates.ERROR] = i18n.t('screens.proxiwash.modal.error');
|
82
|
|
- modalStateStrings[ProxiwashConstants.machineStates.UNKNOWN] = i18n.t('screens.proxiwash.modal.unknown');
|
|
182
|
+ let message = modalStateStrings[item.state];
|
|
183
|
+ const onPress = this.onSetupNotificationsPress.bind(this, item);
|
|
184
|
+ if (item.state === ProxiwashConstants.machineStates.RUNNING) {
|
|
185
|
+ let remainingTime = parseInt(item.remainingTime, 10);
|
|
186
|
+ if (remainingTime < 0) remainingTime = 0;
|
|
187
|
+
|
|
188
|
+ button = {
|
|
189
|
+ text: isMachineWatched(item, state.machinesWatched)
|
|
190
|
+ ? i18n.t('screens.proxiwash.modal.disableNotifications')
|
|
191
|
+ : i18n.t('screens.proxiwash.modal.enableNotifications'),
|
|
192
|
+ icon: '',
|
|
193
|
+ onPress,
|
|
194
|
+ };
|
|
195
|
+ message = i18n.t('screens.proxiwash.modal.running', {
|
|
196
|
+ start: item.startTime,
|
|
197
|
+ end: item.endTime,
|
|
198
|
+ remaining: remainingTime,
|
|
199
|
+ program: item.program,
|
|
200
|
+ });
|
|
201
|
+ } else if (item.state === ProxiwashConstants.machineStates.AVAILABLE) {
|
|
202
|
+ if (isDryer) message += `\n${i18n.t('screens.proxiwash.dryersTariff')}`;
|
|
203
|
+ else message += `\n${i18n.t('screens.proxiwash.washersTariff')}`;
|
83
|
204
|
}
|
84
|
|
-
|
85
|
|
- /**
|
86
|
|
- * Setup notification channel for android and add listeners to detect notifications fired
|
87
|
|
- */
|
88
|
|
- componentDidMount() {
|
89
|
|
- this.props.navigation.setOptions({
|
90
|
|
- headerRight: () =>
|
91
|
|
- <MaterialHeaderButtons>
|
92
|
|
- <Item title="information" iconName="information" onPress={this.onAboutPress}/>
|
93
|
|
- </MaterialHeaderButtons>,
|
|
205
|
+ return (
|
|
206
|
+ <View
|
|
207
|
+ style={{
|
|
208
|
+ flex: 1,
|
|
209
|
+ padding: 20,
|
|
210
|
+ }}>
|
|
211
|
+ <Card.Title
|
|
212
|
+ title={title}
|
|
213
|
+ left={(): React.Node => (
|
|
214
|
+ <Avatar.Icon
|
|
215
|
+ icon={isDryer ? 'tumble-dryer' : 'washing-machine'}
|
|
216
|
+ color={props.theme.colors.text}
|
|
217
|
+ style={{backgroundColor: 'transparent'}}
|
|
218
|
+ />
|
|
219
|
+ )}
|
|
220
|
+ />
|
|
221
|
+ <Card.Content>
|
|
222
|
+ <Text>{message}</Text>
|
|
223
|
+ </Card.Content>
|
|
224
|
+
|
|
225
|
+ {button.onPress !== undefined ? (
|
|
226
|
+ <Card.Actions>
|
|
227
|
+ <Button
|
|
228
|
+ icon={button.icon}
|
|
229
|
+ mode="contained"
|
|
230
|
+ onPress={button.onPress}
|
|
231
|
+ style={{marginLeft: 'auto', marginRight: 'auto'}}>
|
|
232
|
+ {button.text}
|
|
233
|
+ </Button>
|
|
234
|
+ </Card.Actions>
|
|
235
|
+ ) : null}
|
|
236
|
+ </View>
|
|
237
|
+ );
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ /**
|
|
241
|
+ * Gets the section render item
|
|
242
|
+ *
|
|
243
|
+ * @param section The section to render
|
|
244
|
+ * @return {*}
|
|
245
|
+ */
|
|
246
|
+ getRenderSectionHeader = ({
|
|
247
|
+ section,
|
|
248
|
+ }: {
|
|
249
|
+ section: {title: string},
|
|
250
|
+ }): React.Node => {
|
|
251
|
+ const isDryer = section.title === i18n.t('screens.proxiwash.dryers');
|
|
252
|
+ const nbAvailable = this.getMachineAvailableNumber(isDryer);
|
|
253
|
+ return (
|
|
254
|
+ <ProxiwashSectionHeader
|
|
255
|
+ title={section.title}
|
|
256
|
+ nbAvailable={nbAvailable}
|
|
257
|
+ isDryer={isDryer}
|
|
258
|
+ />
|
|
259
|
+ );
|
|
260
|
+ };
|
|
261
|
+
|
|
262
|
+ /**
|
|
263
|
+ * Gets the list item to be rendered
|
|
264
|
+ *
|
|
265
|
+ * @param item The object containing the item's FetchedData
|
|
266
|
+ * @param section The object describing the current SectionList section
|
|
267
|
+ * @returns {React.Node}
|
|
268
|
+ */
|
|
269
|
+ getRenderItem = ({
|
|
270
|
+ item,
|
|
271
|
+ section,
|
|
272
|
+ }: {
|
|
273
|
+ item: ProxiwashMachineType,
|
|
274
|
+ section: {title: string},
|
|
275
|
+ }): React.Node => {
|
|
276
|
+ const {machinesWatched} = this.state;
|
|
277
|
+ const isDryer = section.title === i18n.t('screens.proxiwash.dryers');
|
|
278
|
+ return (
|
|
279
|
+ <ProxiwashListItem
|
|
280
|
+ item={item}
|
|
281
|
+ onPress={this.showModal}
|
|
282
|
+ isWatched={isMachineWatched(item, machinesWatched)}
|
|
283
|
+ isDryer={isDryer}
|
|
284
|
+ height={LIST_ITEM_HEIGHT}
|
|
285
|
+ />
|
|
286
|
+ );
|
|
287
|
+ };
|
|
288
|
+
|
|
289
|
+ /**
|
|
290
|
+ * Extracts the key for the given item
|
|
291
|
+ *
|
|
292
|
+ * @param item The item to extract the key from
|
|
293
|
+ * @return {*} The extracted key
|
|
294
|
+ */
|
|
295
|
+ getKeyExtractor = (item: ProxiwashMachineType): string => item.number;
|
|
296
|
+
|
|
297
|
+ /**
|
|
298
|
+ * Setups notifications for the machine with the given ID.
|
|
299
|
+ * One notification will be sent at the end of the program.
|
|
300
|
+ * Another will be send a few minutes before the end, based on the value of reminderNotifTime
|
|
301
|
+ *
|
|
302
|
+ * @param machine The machine to watch
|
|
303
|
+ */
|
|
304
|
+ setupNotifications(machine: ProxiwashMachineType) {
|
|
305
|
+ const {machinesWatched} = this.state;
|
|
306
|
+ if (!isMachineWatched(machine, machinesWatched)) {
|
|
307
|
+ Notifications.setupMachineNotification(
|
|
308
|
+ machine.number,
|
|
309
|
+ true,
|
|
310
|
+ getMachineEndDate(machine),
|
|
311
|
+ )
|
|
312
|
+ .then(() => {
|
|
313
|
+ this.saveNotificationToState(machine);
|
|
314
|
+ })
|
|
315
|
+ .catch(() => {
|
|
316
|
+ ProxiwashScreen.showNotificationsDisabledWarning();
|
94
|
317
|
});
|
|
318
|
+ } else {
|
|
319
|
+ Notifications.setupMachineNotification(machine.number, false, null).then(
|
|
320
|
+ () => {
|
|
321
|
+ this.removeNotificationFromState(machine);
|
|
322
|
+ },
|
|
323
|
+ );
|
95
|
324
|
}
|
96
|
|
-
|
97
|
|
- /**
|
98
|
|
- * Callback used when pressing the about button.
|
99
|
|
- * This will open the ProxiwashAboutScreen.
|
100
|
|
- */
|
101
|
|
- onAboutPress = () => this.props.navigation.navigate('proxiwash-about');
|
102
|
|
-
|
103
|
|
- /**
|
104
|
|
- * Extracts the key for the given item
|
105
|
|
- *
|
106
|
|
- * @param item The item to extract the key from
|
107
|
|
- * @return {*} The extracted key
|
108
|
|
- */
|
109
|
|
- getKeyExtractor = (item: Machine) => item.number;
|
110
|
|
-
|
111
|
|
- /**
|
112
|
|
- * Setups notifications for the machine with the given ID.
|
113
|
|
- * One notification will be sent at the end of the program.
|
114
|
|
- * Another will be send a few minutes before the end, based on the value of reminderNotifTime
|
115
|
|
- *
|
116
|
|
- * @param machine The machine to watch
|
117
|
|
- */
|
118
|
|
- setupNotifications(machine: Machine) {
|
119
|
|
- if (!isMachineWatched(machine, this.state.machinesWatched)) {
|
120
|
|
- Notifications.setupMachineNotification(machine.number, true, getMachineEndDate(machine))
|
121
|
|
- .then(() => {
|
122
|
|
- this.saveNotificationToState(machine);
|
123
|
|
- })
|
124
|
|
- .catch(() => {
|
125
|
|
- this.showNotificationsDisabledWarning();
|
126
|
|
- });
|
127
|
|
- } else {
|
128
|
|
- Notifications.setupMachineNotification(machine.number, false, null)
|
129
|
|
- .then(() => {
|
130
|
|
- this.removeNotificationFromState(machine);
|
131
|
|
- });
|
132
|
|
- }
|
133
|
|
- }
|
134
|
|
-
|
135
|
|
- /**
|
136
|
|
- * Shows a warning telling the user notifications are disabled for the app
|
137
|
|
- */
|
138
|
|
- showNotificationsDisabledWarning() {
|
139
|
|
- Alert.alert(
|
140
|
|
- i18n.t("screens.proxiwash.modal.notificationErrorTitle"),
|
141
|
|
- i18n.t("screens.proxiwash.modal.notificationErrorDescription"),
|
142
|
|
- );
|
143
|
|
- }
|
144
|
|
-
|
145
|
|
- /**
|
146
|
|
- * Adds the given notifications associated to a machine ID to the watchlist, and saves the array to the preferences
|
147
|
|
- *
|
148
|
|
- * @param machine
|
149
|
|
- */
|
150
|
|
- saveNotificationToState(machine: Machine) {
|
151
|
|
- let data = this.state.machinesWatched;
|
152
|
|
- data.push(machine);
|
153
|
|
- this.saveNewWatchedList(data);
|
|
325
|
+ }
|
|
326
|
+
|
|
327
|
+ /**
|
|
328
|
+ * Gets the number of machines available
|
|
329
|
+ *
|
|
330
|
+ * @param isDryer True if we are only checking for dryer, false for washers
|
|
331
|
+ * @return {number} The number of machines available
|
|
332
|
+ */
|
|
333
|
+ getMachineAvailableNumber(isDryer: boolean): number {
|
|
334
|
+ let data;
|
|
335
|
+ if (isDryer) data = this.fetchedData.dryers;
|
|
336
|
+ else data = this.fetchedData.washers;
|
|
337
|
+ let count = 0;
|
|
338
|
+ data.forEach((machine: ProxiwashMachineType) => {
|
|
339
|
+ if (machine.state === ProxiwashConstants.machineStates.AVAILABLE)
|
|
340
|
+ count += 1;
|
|
341
|
+ });
|
|
342
|
+ return count;
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+ /**
|
|
346
|
+ * Creates the dataset to be used by the FlatList
|
|
347
|
+ *
|
|
348
|
+ * @param fetchedData
|
|
349
|
+ * @return {*}
|
|
350
|
+ */
|
|
351
|
+ createDataset = (fetchedData: {
|
|
352
|
+ dryers: Array<ProxiwashMachineType>,
|
|
353
|
+ washers: Array<ProxiwashMachineType>,
|
|
354
|
+ }): SectionListDataType<ProxiwashMachineType> => {
|
|
355
|
+ const {state} = this;
|
|
356
|
+ let data = fetchedData;
|
|
357
|
+ if (AprilFoolsManager.getInstance().isAprilFoolsEnabled()) {
|
|
358
|
+ data = JSON.parse(JSON.stringify(fetchedData)); // Deep copy
|
|
359
|
+ AprilFoolsManager.getNewProxiwashDryerOrderedList(data.dryers);
|
|
360
|
+ AprilFoolsManager.getNewProxiwashWasherOrderedList(data.washers);
|
154
|
361
|
}
|
155
|
|
-
|
156
|
|
- /**
|
157
|
|
- * Removes the given index from the watchlist array and saves it to preferences
|
158
|
|
- *
|
159
|
|
- * @param machine
|
160
|
|
- */
|
161
|
|
- removeNotificationFromState(machine: Machine) {
|
162
|
|
- let data = this.state.machinesWatched;
|
163
|
|
- for (let i = 0; i < data.length; i++) {
|
164
|
|
- if (data[i].number === machine.number && data[i].endTime === machine.endTime) {
|
165
|
|
- data.splice(i, 1);
|
166
|
|
- break;
|
167
|
|
- }
|
168
|
|
- }
|
169
|
|
- this.saveNewWatchedList(data);
|
170
|
|
- }
|
171
|
|
-
|
172
|
|
- saveNewWatchedList(list: Array<Machine>) {
|
173
|
|
- this.setState({machinesWatched: list});
|
174
|
|
- AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key, list);
|
|
362
|
+ this.fetchedData = data;
|
|
363
|
+ this.state.machinesWatched = getCleanedMachineWatched(
|
|
364
|
+ state.machinesWatched,
|
|
365
|
+ [...data.dryers, ...data.washers],
|
|
366
|
+ );
|
|
367
|
+ return [
|
|
368
|
+ {
|
|
369
|
+ title: i18n.t('screens.proxiwash.dryers'),
|
|
370
|
+ icon: 'tumble-dryer',
|
|
371
|
+ data: data.dryers === undefined ? [] : data.dryers,
|
|
372
|
+ keyExtractor: this.getKeyExtractor,
|
|
373
|
+ },
|
|
374
|
+ {
|
|
375
|
+ title: i18n.t('screens.proxiwash.washers'),
|
|
376
|
+ icon: 'washing-machine',
|
|
377
|
+ data: data.washers === undefined ? [] : data.washers,
|
|
378
|
+ keyExtractor: this.getKeyExtractor,
|
|
379
|
+ },
|
|
380
|
+ ];
|
|
381
|
+ };
|
|
382
|
+
|
|
383
|
+ /**
|
|
384
|
+ * Shows a modal for the given item
|
|
385
|
+ *
|
|
386
|
+ * @param title The title to use
|
|
387
|
+ * @param item The item to display information for in the modal
|
|
388
|
+ * @param isDryer True if the given item is a dryer
|
|
389
|
+ */
|
|
390
|
+ showModal = (title: string, item: ProxiwashMachineType, isDryer: boolean) => {
|
|
391
|
+ this.setState({
|
|
392
|
+ modalCurrentDisplayItem: this.getModalContent(title, item, isDryer),
|
|
393
|
+ });
|
|
394
|
+ if (this.modalRef) {
|
|
395
|
+ this.modalRef.open();
|
175
|
396
|
}
|
176
|
|
-
|
177
|
|
- /**
|
178
|
|
- * Creates the dataset to be used by the flatlist
|
179
|
|
- *
|
180
|
|
- * @param fetchedData
|
181
|
|
- * @return {*}
|
182
|
|
- */
|
183
|
|
- createDataset = (fetchedData: Object) => {
|
184
|
|
- let data = fetchedData;
|
185
|
|
- if (AprilFoolsManager.getInstance().isAprilFoolsEnabled()) {
|
186
|
|
- data = JSON.parse(JSON.stringify(fetchedData)); // Deep copy
|
187
|
|
- AprilFoolsManager.getNewProxiwashDryerOrderedList(data.dryers);
|
188
|
|
- AprilFoolsManager.getNewProxiwashWasherOrderedList(data.washers);
|
189
|
|
- }
|
190
|
|
- this.fetchedData = data;
|
191
|
|
- this.state.machinesWatched =
|
192
|
|
- getCleanedMachineWatched(this.state.machinesWatched, [...data.dryers, ...data.washers]);
|
193
|
|
- return [
|
194
|
|
- {
|
195
|
|
- title: i18n.t('screens.proxiwash.dryers'),
|
196
|
|
- icon: 'tumble-dryer',
|
197
|
|
- data: data.dryers === undefined ? [] : data.dryers,
|
198
|
|
- keyExtractor: this.getKeyExtractor
|
199
|
|
- },
|
200
|
|
- {
|
201
|
|
- title: i18n.t('screens.proxiwash.washers'),
|
202
|
|
- icon: 'washing-machine',
|
203
|
|
- data: data.washers === undefined ? [] : data.washers,
|
204
|
|
- keyExtractor: this.getKeyExtractor
|
|
397
|
+ };
|
|
398
|
+
|
|
399
|
+ /**
|
|
400
|
+ * Adds the given notifications associated to a machine ID to the watchlist, and saves the array to the preferences
|
|
401
|
+ *
|
|
402
|
+ * @param machine
|
|
403
|
+ */
|
|
404
|
+ saveNotificationToState(machine: ProxiwashMachineType) {
|
|
405
|
+ const {machinesWatched} = this.state;
|
|
406
|
+ const data = machinesWatched;
|
|
407
|
+ data.push(machine);
|
|
408
|
+ this.saveNewWatchedList(data);
|
|
409
|
+ }
|
|
410
|
+
|
|
411
|
+ /**
|
|
412
|
+ * Removes the given index from the watchlist array and saves it to preferences
|
|
413
|
+ *
|
|
414
|
+ * @param selectedMachine
|
|
415
|
+ */
|
|
416
|
+ removeNotificationFromState(selectedMachine: ProxiwashMachineType) {
|
|
417
|
+ const {machinesWatched} = this.state;
|
|
418
|
+ const newList = [...machinesWatched];
|
|
419
|
+ machinesWatched.forEach((machine: ProxiwashMachineType, index: number) => {
|
|
420
|
+ if (
|
|
421
|
+ machine.number === selectedMachine.number &&
|
|
422
|
+ machine.endTime === selectedMachine.endTime
|
|
423
|
+ )
|
|
424
|
+ newList.splice(index, 1);
|
|
425
|
+ });
|
|
426
|
+ this.saveNewWatchedList(newList);
|
|
427
|
+ }
|
|
428
|
+
|
|
429
|
+ saveNewWatchedList(list: Array<ProxiwashMachineType>) {
|
|
430
|
+ this.setState({machinesWatched: list});
|
|
431
|
+ AsyncStorageManager.set(
|
|
432
|
+ AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key,
|
|
433
|
+ list,
|
|
434
|
+ );
|
|
435
|
+ }
|
|
436
|
+
|
|
437
|
+ render(): React.Node {
|
|
438
|
+ const {state} = this;
|
|
439
|
+ const {navigation} = this.props;
|
|
440
|
+ return (
|
|
441
|
+ <View style={{flex: 1}}>
|
|
442
|
+ <View
|
|
443
|
+ style={{
|
|
444
|
+ position: 'absolute',
|
|
445
|
+ width: '100%',
|
|
446
|
+ height: '100%',
|
|
447
|
+ }}>
|
|
448
|
+ <WebSectionList
|
|
449
|
+ createDataset={this.createDataset}
|
|
450
|
+ navigation={navigation}
|
|
451
|
+ fetchUrl={DATA_URL}
|
|
452
|
+ renderItem={this.getRenderItem}
|
|
453
|
+ renderSectionHeader={this.getRenderSectionHeader}
|
|
454
|
+ autoRefreshTime={REFRESH_TIME}
|
|
455
|
+ refreshOnFocus
|
|
456
|
+ updateData={state.machinesWatched.length}
|
|
457
|
+ />
|
|
458
|
+ </View>
|
|
459
|
+ <MascotPopup
|
|
460
|
+ prefKey={AsyncStorageManager.PREFERENCES.proxiwashShowBanner.key}
|
|
461
|
+ title={i18n.t('screens.proxiwash.mascotDialog.title')}
|
|
462
|
+ message={i18n.t('screens.proxiwash.mascotDialog.message')}
|
|
463
|
+ icon="information"
|
|
464
|
+ buttons={{
|
|
465
|
+ action: null,
|
|
466
|
+ cancel: {
|
|
467
|
+ message: i18n.t('screens.proxiwash.mascotDialog.ok'),
|
|
468
|
+ icon: 'check',
|
205
|
469
|
},
|
206
|
|
- ];
|
207
|
|
- };
|
208
|
|
-
|
209
|
|
- /**
|
210
|
|
- * Shows a modal for the given item
|
211
|
|
- *
|
212
|
|
- * @param title The title to use
|
213
|
|
- * @param item The item to display information for in the modal
|
214
|
|
- * @param isDryer True if the given item is a dryer
|
215
|
|
- */
|
216
|
|
- showModal = (title: string, item: Object, isDryer: boolean) => {
|
217
|
|
- this.setState({
|
218
|
|
- modalCurrentDisplayItem: this.getModalContent(title, item, isDryer)
|
219
|
|
- });
|
220
|
|
- if (this.modalRef) {
|
221
|
|
- this.modalRef.open();
|
222
|
|
- }
|
223
|
|
- };
|
224
|
|
-
|
225
|
|
- /**
|
226
|
|
- * Callback used when the user clicks on enable notifications for a machine
|
227
|
|
- *
|
228
|
|
- * @param machine The machine to set notifications for
|
229
|
|
- */
|
230
|
|
- onSetupNotificationsPress(machine: Machine) {
|
231
|
|
- if (this.modalRef) {
|
232
|
|
- this.modalRef.close();
|
233
|
|
- }
|
234
|
|
- this.setupNotifications(machine);
|
235
|
|
- }
|
236
|
|
-
|
237
|
|
- /**
|
238
|
|
- * Generates the modal content.
|
239
|
|
- * This shows information for the given machine.
|
240
|
|
- *
|
241
|
|
- * @param title The title to use
|
242
|
|
- * @param item The item to display information for in the modal
|
243
|
|
- * @param isDryer True if the given item is a dryer
|
244
|
|
- * @return {*}
|
245
|
|
- */
|
246
|
|
- getModalContent(title: string, item: Machine, isDryer: boolean) {
|
247
|
|
- let button = {
|
248
|
|
- text: i18n.t("screens.proxiwash.modal.ok"),
|
249
|
|
- icon: '',
|
250
|
|
- onPress: undefined
|
251
|
|
- };
|
252
|
|
- let message = modalStateStrings[item.state];
|
253
|
|
- const onPress = this.onSetupNotificationsPress.bind(this, item);
|
254
|
|
- if (item.state === ProxiwashConstants.machineStates.RUNNING) {
|
255
|
|
- let remainingTime = parseInt(item.remainingTime)
|
256
|
|
- if (remainingTime < 0)
|
257
|
|
- remainingTime = 0;
|
258
|
|
-
|
259
|
|
- button =
|
260
|
|
- {
|
261
|
|
- text: isMachineWatched(item, this.state.machinesWatched) ?
|
262
|
|
- i18n.t("screens.proxiwash.modal.disableNotifications") :
|
263
|
|
- i18n.t("screens.proxiwash.modal.enableNotifications"),
|
264
|
|
- icon: '',
|
265
|
|
- onPress: onPress
|
266
|
|
- }
|
267
|
|
- ;
|
268
|
|
- message = i18n.t('screens.proxiwash.modal.running',
|
269
|
|
- {
|
270
|
|
- start: item.startTime,
|
271
|
|
- end: item.endTime,
|
272
|
|
- remaining: remainingTime,
|
273
|
|
- program: item.program
|
274
|
|
- });
|
275
|
|
- } else if (item.state === ProxiwashConstants.machineStates.AVAILABLE) {
|
276
|
|
- if (isDryer)
|
277
|
|
- message += '\n' + i18n.t('screens.proxiwash.dryersTariff');
|
278
|
|
- else
|
279
|
|
- message += '\n' + i18n.t('screens.proxiwash.washersTariff');
|
280
|
|
- }
|
281
|
|
- return (
|
282
|
|
- <View style={{
|
283
|
|
- flex: 1,
|
284
|
|
- padding: 20
|
285
|
|
- }}>
|
286
|
|
- <Card.Title
|
287
|
|
- title={title}
|
288
|
|
- left={() => <Avatar.Icon
|
289
|
|
- icon={isDryer ? 'tumble-dryer' : 'washing-machine'}
|
290
|
|
- color={this.props.theme.colors.text}
|
291
|
|
- style={{backgroundColor: 'transparent'}}/>}
|
292
|
|
-
|
293
|
|
- />
|
294
|
|
- <Card.Content>
|
295
|
|
- <Text>{message}</Text>
|
296
|
|
- </Card.Content>
|
297
|
|
-
|
298
|
|
- {button.onPress !== undefined ?
|
299
|
|
- <Card.Actions>
|
300
|
|
- <Button
|
301
|
|
- icon={button.icon}
|
302
|
|
- mode="contained"
|
303
|
|
- onPress={button.onPress}
|
304
|
|
- style={{marginLeft: 'auto', marginRight: 'auto'}}
|
305
|
|
- >
|
306
|
|
- {button.text}
|
307
|
|
- </Button>
|
308
|
|
- </Card.Actions> : null}
|
309
|
|
- </View>
|
310
|
|
- );
|
311
|
|
- }
|
312
|
|
-
|
313
|
|
- /**
|
314
|
|
- * Callback used when receiving modal ref
|
315
|
|
- *
|
316
|
|
- * @param ref
|
317
|
|
- */
|
318
|
|
- onModalRef = (ref: Object) => {
|
319
|
|
- this.modalRef = ref;
|
320
|
|
- };
|
321
|
|
-
|
322
|
|
- /**
|
323
|
|
- * Gets the number of machines available
|
324
|
|
- *
|
325
|
|
- * @param isDryer True if we are only checking for dryer, false for washers
|
326
|
|
- * @return {number} The number of machines available
|
327
|
|
- */
|
328
|
|
- getMachineAvailableNumber(isDryer: boolean) {
|
329
|
|
- let data;
|
330
|
|
- if (isDryer)
|
331
|
|
- data = this.fetchedData.dryers;
|
332
|
|
- else
|
333
|
|
- data = this.fetchedData.washers;
|
334
|
|
- let count = 0;
|
335
|
|
- for (let i = 0; i < data.length; i++) {
|
336
|
|
- if (data[i].state === ProxiwashConstants.machineStates.AVAILABLE)
|
337
|
|
- count += 1;
|
338
|
|
- }
|
339
|
|
- return count;
|
340
|
|
- }
|
341
|
|
-
|
342
|
|
- /**
|
343
|
|
- * Gets the section render item
|
344
|
|
- *
|
345
|
|
- * @param section The section to render
|
346
|
|
- * @return {*}
|
347
|
|
- */
|
348
|
|
- getRenderSectionHeader = ({section}: Object) => {
|
349
|
|
- const isDryer = section.title === i18n.t('screens.proxiwash.dryers');
|
350
|
|
- const nbAvailable = this.getMachineAvailableNumber(isDryer);
|
351
|
|
- return (
|
352
|
|
- <ProxiwashSectionHeader
|
353
|
|
- title={section.title}
|
354
|
|
- nbAvailable={nbAvailable}
|
355
|
|
- isDryer={isDryer}/>
|
356
|
|
- );
|
357
|
|
- };
|
358
|
|
-
|
359
|
|
- /**
|
360
|
|
- * Gets the list item to be rendered
|
361
|
|
- *
|
362
|
|
- * @param item The object containing the item's FetchedData
|
363
|
|
- * @param section The object describing the current SectionList section
|
364
|
|
- * @returns {React.Node}
|
365
|
|
- */
|
366
|
|
- getRenderItem = ({item, section}: Object) => {
|
367
|
|
- const isDryer = section.title === i18n.t('screens.proxiwash.dryers');
|
368
|
|
- return (
|
369
|
|
- <ProxiwashListItem
|
370
|
|
- item={item}
|
371
|
|
- onPress={this.showModal}
|
372
|
|
- isWatched={isMachineWatched(item, this.state.machinesWatched)}
|
373
|
|
- isDryer={isDryer}
|
374
|
|
- height={LIST_ITEM_HEIGHT}
|
375
|
|
- />
|
376
|
|
- );
|
377
|
|
- };
|
378
|
|
-
|
379
|
|
- render() {
|
380
|
|
- const nav = this.props.navigation;
|
381
|
|
- return (
|
382
|
|
- <View
|
383
|
|
- style={{flex: 1}}
|
384
|
|
- >
|
385
|
|
- <View style={{
|
386
|
|
- position: "absolute",
|
387
|
|
- width: "100%",
|
388
|
|
- height: "100%",
|
389
|
|
- }}>
|
390
|
|
- <WebSectionList
|
391
|
|
- createDataset={this.createDataset}
|
392
|
|
- navigation={nav}
|
393
|
|
- fetchUrl={DATA_URL}
|
394
|
|
- renderItem={this.getRenderItem}
|
395
|
|
- renderSectionHeader={this.getRenderSectionHeader}
|
396
|
|
- autoRefreshTime={REFRESH_TIME}
|
397
|
|
- refreshOnFocus={true}
|
398
|
|
- updateData={this.state.machinesWatched.length}/>
|
399
|
|
- </View>
|
400
|
|
- <MascotPopup
|
401
|
|
- prefKey={AsyncStorageManager.PREFERENCES.proxiwashShowBanner.key}
|
402
|
|
- title={i18n.t("screens.proxiwash.mascotDialog.title")}
|
403
|
|
- message={i18n.t("screens.proxiwash.mascotDialog.message")}
|
404
|
|
- icon={"information"}
|
405
|
|
- buttons={{
|
406
|
|
- action: null,
|
407
|
|
- cancel: {
|
408
|
|
- message: i18n.t("screens.proxiwash.mascotDialog.ok"),
|
409
|
|
- icon: "check",
|
410
|
|
- }
|
411
|
|
- }}
|
412
|
|
- emotion={MASCOT_STYLE.NORMAL}
|
413
|
|
- />
|
414
|
|
- <CustomModal onRef={this.onModalRef}>
|
415
|
|
- {this.state.modalCurrentDisplayItem}
|
416
|
|
- </CustomModal>
|
417
|
|
- </View>
|
418
|
|
- );
|
419
|
|
- }
|
|
470
|
+ }}
|
|
471
|
+ emotion={MASCOT_STYLE.NORMAL}
|
|
472
|
+ />
|
|
473
|
+ <CustomModal onRef={this.onModalRef}>
|
|
474
|
+ {state.modalCurrentDisplayItem}
|
|
475
|
+ </CustomModal>
|
|
476
|
+ </View>
|
|
477
|
+ );
|
|
478
|
+ }
|
420
|
479
|
}
|
421
|
480
|
|
422
|
481
|
export default withTheme(ProxiwashScreen);
|