|
@@ -1,24 +1,240 @@
|
1
|
1
|
import React from 'react';
|
2
|
|
-import {StyleSheet, View} from 'react-native';
|
3
|
|
-import {Container, Text} from 'native-base';
|
|
2
|
+import {SectionList, RefreshControl, StyleSheet, View, ScrollView} from 'react-native';
|
|
3
|
+import {Badge, Body, Container, Content, Icon, Left, ListItem, Right, Text, Toast, H2, Button} from 'native-base';
|
4
|
4
|
import CustomHeader from "../components/CustomHeader";
|
|
5
|
+import NotificationsManager from '../utils/NotificationsManager';
|
|
6
|
+import i18n from "i18n-js";
|
|
7
|
+import {AsyncStorage} from 'react-native'
|
|
8
|
+
|
|
9
|
+const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProxiwash.json";
|
|
10
|
+const WATCHED_MACHINES_PREFKEY = "proxiwash.watchedMachines";
|
|
11
|
+
|
|
12
|
+const remainderNotifTime = 5;
|
|
13
|
+
|
|
14
|
+const MACHINE_STATES = {
|
|
15
|
+ TERMINE: "0",
|
|
16
|
+ DISPONIBLE: "1",
|
|
17
|
+ FONCTIONNE: "2",
|
|
18
|
+ HS: "3",
|
|
19
|
+ ERREUR: "4"
|
|
20
|
+};
|
|
21
|
+
|
|
22
|
+let stateStrings = {};
|
|
23
|
+
|
|
24
|
+let stateColors = {};
|
|
25
|
+
|
5
|
26
|
|
6
|
27
|
export default class ProxiwashScreen extends React.Component {
|
|
28
|
+
|
|
29
|
+ constructor(props) {
|
|
30
|
+ super(props);
|
|
31
|
+ stateColors[MACHINE_STATES.TERMINE] = 'rgba(54,165,22,0.4)';
|
|
32
|
+ stateColors[MACHINE_STATES.DISPONIBLE] = '#ffffff';
|
|
33
|
+ stateColors[MACHINE_STATES.FONCTIONNE] = 'rgba(241,237,41,0.4)';
|
|
34
|
+ stateColors[MACHINE_STATES.HS] = '#a2a2a2';
|
|
35
|
+ stateColors[MACHINE_STATES.ERREUR] = 'rgba(204,7,0,0.4)';
|
|
36
|
+
|
|
37
|
+ stateStrings[MACHINE_STATES.TERMINE] = i18n.t('proxiwashScreen.states.finished');
|
|
38
|
+ stateStrings[MACHINE_STATES.DISPONIBLE] = i18n.t('proxiwashScreen.states.ready');
|
|
39
|
+ stateStrings[MACHINE_STATES.FONCTIONNE] = i18n.t('proxiwashScreen.states.running');
|
|
40
|
+ stateStrings[MACHINE_STATES.HS] = i18n.t('proxiwashScreen.states.broken');
|
|
41
|
+ stateStrings[MACHINE_STATES.ERREUR] = i18n.t('proxiwashScreen.states.error');
|
|
42
|
+ this.state = {
|
|
43
|
+ refreshing: false,
|
|
44
|
+ data: {},
|
|
45
|
+ machinesWatched: [],
|
|
46
|
+ };
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ async readData() {
|
|
50
|
+ try {
|
|
51
|
+ let response = await fetch(DATA_URL);
|
|
52
|
+ let responseJson = await response.json();
|
|
53
|
+ // This prevents end notifications from showing
|
|
54
|
+ // let watchList = this.state.machinesWatched;
|
|
55
|
+ // for (let i = 0; i < watchList.length; i++) {
|
|
56
|
+ // if (responseJson[MACHINE_STATES[watchList[i].machineNumber.state]] !== MACHINE_STATES.FONCTIONNE)
|
|
57
|
+ // this.disableNotification(watchList[i].machineNumber);
|
|
58
|
+ // }
|
|
59
|
+ this.setState({
|
|
60
|
+ data: responseJson
|
|
61
|
+ });
|
|
62
|
+ } catch (error) {
|
|
63
|
+ console.error(error);
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ async componentWillMount() {
|
|
68
|
+ let dataString = await AsyncStorage.getItem(WATCHED_MACHINES_PREFKEY);
|
|
69
|
+ if (dataString === null)
|
|
70
|
+ dataString = '[]';
|
|
71
|
+ this.setState({machinesWatched: JSON.parse(dataString)});
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ componentDidMount() {
|
|
75
|
+ this._onRefresh();
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ _onRefresh = () => {
|
|
79
|
+ this.setState({refreshing: true});
|
|
80
|
+ this.readData().then(() => {
|
|
81
|
+ this.setState({refreshing: false});
|
|
82
|
+ Toast.show({
|
|
83
|
+ text: i18n.t('proxiwashScreen.listUpdated'),
|
|
84
|
+ buttonText: 'OK',
|
|
85
|
+ type: "success",
|
|
86
|
+ duration: 2000
|
|
87
|
+ })
|
|
88
|
+ });
|
|
89
|
+ };
|
|
90
|
+
|
|
91
|
+ static getRemainingTime(startString, endString, percentDone) {
|
|
92
|
+ let startArray = startString.split(':');
|
|
93
|
+ let endArray = endString.split(':');
|
|
94
|
+ let startDate = new Date();
|
|
95
|
+ startDate.setHours(parseInt(startArray[0]), parseInt(startArray[1]), 0, 0);
|
|
96
|
+ let endDate = new Date();
|
|
97
|
+ endDate.setHours(parseInt(endArray[0]), parseInt(endArray[1]), 0, 0);
|
|
98
|
+ return (((100 - percentDone) / 100) * (endDate - startDate) / (60 * 1000)).toFixed(0); // Convert milliseconds into minutes
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ async setupNotifications(number, remainingTime) {
|
|
102
|
+ if (!this.isMachineWatched(number)) {
|
|
103
|
+ let endNotifID = await NotificationsManager.scheduleNotification(
|
|
104
|
+ i18n.t('proxiwashScreen.notifications.machineFinishedTitle'),
|
|
105
|
+ i18n.t('proxiwashScreen.notifications.machineFinishedBody', {number: number}),
|
|
106
|
+ new Date().getTime() + remainingTime * (60 * 1000) // Convert back to milliseconds
|
|
107
|
+ );
|
|
108
|
+ let remainderNotifID = undefined;
|
|
109
|
+ if (remainingTime > remainderNotifTime) {
|
|
110
|
+ remainderNotifID = await NotificationsManager.scheduleNotification(
|
|
111
|
+ i18n.t('proxiwashScreen.notifications.machineRunningTitle', {time: remainderNotifTime}),
|
|
112
|
+ i18n.t('proxiwashScreen.notifications.machineRunningBody', {number: number}),
|
|
113
|
+ new Date().getTime() + (remainingTime - remainderNotifTime) * (60 * 1000) // Convert back to milliseconds
|
|
114
|
+ );
|
|
115
|
+ }
|
|
116
|
+ let data = this.state.machinesWatched;
|
|
117
|
+ data.push({machineNumber: number, endNotifID: endNotifID, remainderNotifID: remainderNotifID});
|
|
118
|
+ this.setState({machinesWatched: data});
|
|
119
|
+ AsyncStorage.setItem(WATCHED_MACHINES_PREFKEY, JSON.stringify(data));
|
|
120
|
+ } else
|
|
121
|
+ this.disableNotification(number);
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ disableNotification(number) {
|
|
125
|
+ let data = this.state.machinesWatched;
|
|
126
|
+ if (data.length > 0) {
|
|
127
|
+ let elem = this.state.machinesWatched.find(function (elem) {
|
|
128
|
+ return elem.machineNumber === number
|
|
129
|
+ });
|
|
130
|
+ let arrayIndex = data.indexOf(elem);
|
|
131
|
+ NotificationsManager.cancelScheduledNoification(data[arrayIndex].endNotifID);
|
|
132
|
+ if (data[arrayIndex].remainderNotifID !== undefined)
|
|
133
|
+ NotificationsManager.cancelScheduledNoification(data[arrayIndex].remainderNotifID);
|
|
134
|
+ data.splice(arrayIndex, 1);
|
|
135
|
+ this.setState({machinesWatched: data});
|
|
136
|
+ AsyncStorage.setItem(WATCHED_MACHINES_PREFKEY, JSON.stringify(data));
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ isMachineWatched(number) {
|
|
141
|
+ return this.state.machinesWatched.find(function (elem) {
|
|
142
|
+ return elem.machineNumber === number
|
|
143
|
+ }) !== undefined;
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ renderItem(item, section, data) {
|
|
147
|
+ return (
|
|
148
|
+ <ListItem
|
|
149
|
+ thumbnail
|
|
150
|
+ style={{
|
|
151
|
+ marginLeft: 0,
|
|
152
|
+ backgroundColor: stateColors[MACHINE_STATES[item.state]]
|
|
153
|
+ }}
|
|
154
|
+ >
|
|
155
|
+ <View style={{
|
|
156
|
+ height: '100%',
|
|
157
|
+ position: 'absolute',
|
|
158
|
+ alignSelf: 'flex-end',
|
|
159
|
+ right: 0,
|
|
160
|
+ width: item.donePercent !== '' ? (100 - parseInt(item.donePercent)).toString() + '%' : 0,
|
|
161
|
+ backgroundColor: '#fff'
|
|
162
|
+ }}>
|
|
163
|
+ </View>
|
|
164
|
+ <Left>
|
|
165
|
+ <Icon name={section.title === data[0].title ? 'tumble-dryer' : 'washing-machine'}
|
|
166
|
+ type={'MaterialCommunityIcons'}
|
|
167
|
+ style={{fontSize: 30, width: 30}}
|
|
168
|
+ />
|
|
169
|
+ </Left>
|
|
170
|
+ <Body>
|
|
171
|
+ <Text>
|
|
172
|
+ {section.title === data[0].title ? i18n.t('proxiwashScreen.dryer') : i18n.t('proxiwashScreen.washer')} n°{item.number}
|
|
173
|
+ </Text>
|
|
174
|
+ <Text note>
|
|
175
|
+ {item.startTime !== '' ? item.startTime + '/' + item.endTime : ''}
|
|
176
|
+ </Text>
|
|
177
|
+ </Body>
|
|
178
|
+ <Right>
|
|
179
|
+ {item.startTime !== '' ?
|
|
180
|
+ <Button
|
|
181
|
+ style={this.isMachineWatched(item.number) ?
|
|
182
|
+ {backgroundColor: '#ba7c1f'} : {}}
|
|
183
|
+ onPress={() => {
|
|
184
|
+ this.setupNotifications(item.number, ProxiwashScreen.getRemainingTime(item.startTime, item.endTime, item.donePercent))
|
|
185
|
+ }}>
|
|
186
|
+ <Text>
|
|
187
|
+ {ProxiwashScreen.getRemainingTime(item.startTime, item.endTime, item.donePercent) + ' ' + i18n.t('proxiwashScreen.min')}
|
|
188
|
+ </Text>
|
|
189
|
+ <Icon name={this.isMachineWatched(item.number) ? 'bell-ring' : 'bell'}
|
|
190
|
+ type={'MaterialCommunityIcons'}
|
|
191
|
+ style={{fontSize: 30, width: 30}}
|
|
192
|
+ />
|
|
193
|
+ </Button>
|
|
194
|
+ : <Text style={MACHINE_STATES[item.state] === MACHINE_STATES.TERMINE ?
|
|
195
|
+ {fontWeight: 'bold'} : {}}
|
|
196
|
+ >{stateStrings[MACHINE_STATES[item.state]]}</Text>
|
|
197
|
+
|
|
198
|
+ }
|
|
199
|
+ </Right>
|
|
200
|
+ </ListItem>);
|
|
201
|
+ }
|
|
202
|
+
|
7
|
203
|
render() {
|
8
|
204
|
const nav = this.props.navigation;
|
|
205
|
+ const data = [
|
|
206
|
+ {
|
|
207
|
+ title: i18n.t('proxiwashScreen.dryers'),
|
|
208
|
+ data: this.state.data.dryers === undefined ? [] : this.state.data.dryers,
|
|
209
|
+ extraData: this.state
|
|
210
|
+ },
|
|
211
|
+ {
|
|
212
|
+ title: i18n.t('proxiwashScreen.washers'),
|
|
213
|
+ data: this.state.data.washers === undefined ? [] : this.state.data.washers,
|
|
214
|
+ extraData: this.state
|
|
215
|
+ },
|
|
216
|
+ ];
|
|
217
|
+ console.log(this.state.machinesWatched);
|
9
|
218
|
return (
|
10
|
219
|
<Container>
|
11
|
220
|
<CustomHeader navigation={nav} title={'Proxiwash'}/>
|
|
221
|
+ <SectionList
|
|
222
|
+ sections={data}
|
|
223
|
+ keyExtractor={(item) => item.number}
|
|
224
|
+ refreshControl={
|
|
225
|
+ <RefreshControl
|
|
226
|
+ refreshing={this.state.refreshing}
|
|
227
|
+ onRefresh={this._onRefresh}
|
|
228
|
+ />
|
|
229
|
+ }
|
|
230
|
+ renderSectionHeader={({section: {title}}) => (
|
|
231
|
+ <H2 style={{textAlign: 'center', paddingVertical: 10}}>{title}</H2>
|
|
232
|
+ )}
|
|
233
|
+ renderItem={({item, section}) =>
|
|
234
|
+ this.renderItem(item, section, data)
|
|
235
|
+ }
|
|
236
|
+ />
|
12
|
237
|
</Container>
|
13
|
238
|
);
|
14
|
239
|
}
|
15
|
240
|
}
|
16
|
|
-
|
17
|
|
-const styles = StyleSheet.create({
|
18
|
|
- container: {
|
19
|
|
- flex: 1,
|
20
|
|
- backgroundColor: '#fff',
|
21
|
|
- alignItems: 'center',
|
22
|
|
- justifyContent: 'center',
|
23
|
|
- },
|
24
|
|
-});
|