Browse Source

Improved notification activation on corner cases

Arnaud Vergnet 4 years ago
parent
commit
aa2fad344a

+ 15
- 7
__tests__/utils/Proxiwash.test.js View File

4
 test('getMachineEndDate', () => {
4
 test('getMachineEndDate', () => {
5
     jest.spyOn(Date, 'now')
5
     jest.spyOn(Date, 'now')
6
         .mockImplementation(() =>
6
         .mockImplementation(() =>
7
-            new Date('2020-01-14T00:00:00.000Z').getTime()
7
+            new Date('2020-01-14T15:00:00.000Z').getTime()
8
         );
8
         );
9
-    let expectDate = new Date('2020-01-14T00:00:00.000Z');
9
+    let expectDate = new Date('2020-01-14T15:00:00.000Z');
10
     expectDate.setHours(23);
10
     expectDate.setHours(23);
11
     expectDate.setMinutes(10);
11
     expectDate.setMinutes(10);
12
     expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime());
12
     expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime());
13
 
13
 
14
-    expectDate.setHours(15);
14
+    expectDate.setHours(16);
15
     expectDate.setMinutes(30);
15
     expectDate.setMinutes(30);
16
-    expect(getMachineEndDate({endTime: "15:30"}).getTime()).toBe(expectDate.getTime());
16
+    expect(getMachineEndDate({endTime: "16:30"}).getTime()).toBe(expectDate.getTime());
17
 
17
 
18
+    expect(getMachineEndDate({endTime: "15:30"})).toBeNull();
19
+
20
+    expect(getMachineEndDate({endTime: "13:10"})).toBeNull();
21
+
22
+    jest.spyOn(Date, 'now')
23
+        .mockImplementation(() =>
24
+            new Date('2020-01-14T23:00:00.000Z').getTime()
25
+        );
26
+    expectDate = new Date('2020-01-14T23:00:00.000Z');
18
     expectDate.setHours(0);
27
     expectDate.setHours(0);
19
-    expectDate.setMinutes(10);
20
-    expectDate.setDate(expectDate.getDate() + 1);
21
-    expect(getMachineEndDate({endTime: "00:10"}).getTime()).toBe(expectDate.getTime());
28
+    expectDate.setMinutes(30);
29
+    expect(getMachineEndDate({endTime: "00:30"}).getTime()).toBe(expectDate.getTime());
22
 });
30
 });
23
 
31
 
24
 test('isMachineWatched', () => {
32
 test('isMachineWatched', () => {

+ 11
- 15
src/screens/Proxiwash/ProxiwashScreen.js View File

18
 import {Collapsible} from "react-navigation-collapsible";
18
 import {Collapsible} from "react-navigation-collapsible";
19
 import {StackNavigationProp} from "@react-navigation/stack";
19
 import {StackNavigationProp} from "@react-navigation/stack";
20
 import {getCleanedMachineWatched, getMachineEndDate, isMachineWatched} from "../../utils/Proxiwash";
20
 import {getCleanedMachineWatched, getMachineEndDate, isMachineWatched} from "../../utils/Proxiwash";
21
+import {Modalize} from "react-native-modalize";
21
 
22
 
22
 const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
23
 const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
23
 
24
 
55
  */
56
  */
56
 class ProxiwashScreen extends React.Component<Props, State> {
57
 class ProxiwashScreen extends React.Component<Props, State> {
57
 
58
 
58
-    modalRef: Object;
59
+    modalRef: null | Modalize;
59
 
60
 
60
-    fetchedData: Object;
61
+    fetchedData: {
62
+        dryers: Array<Machine>,
63
+        washers: Array<Machine>,
64
+    };
61
 
65
 
62
     state = {
66
     state = {
63
         refreshing: false,
67
         refreshing: false,
95
      */
99
      */
96
     componentDidMount() {
100
     componentDidMount() {
97
         this.props.navigation.setOptions({
101
         this.props.navigation.setOptions({
98
-            headerRight: this.getAboutButton,
102
+            headerRight:
103
+                <MaterialHeaderButtons>
104
+                    <Item title="information" iconName="information" onPress={this.onAboutPress}/>
105
+                </MaterialHeaderButtons>,
99
         });
106
         });
100
     }
107
     }
101
 
108
 
106
     onAboutPress = () => this.props.navigation.navigate('proxiwash-about');
113
     onAboutPress = () => this.props.navigation.navigate('proxiwash-about');
107
 
114
 
108
     /**
115
     /**
109
-     * Gets the about header button
110
-     *
111
-     * @return {*}
112
-     */
113
-    getAboutButton = () =>
114
-        <MaterialHeaderButtons>
115
-            <Item title="information" iconName="information" onPress={this.onAboutPress}/>
116
-        </MaterialHeaderButtons>;
117
-
118
-    /**
119
      * Extracts the key for the given item
116
      * Extracts the key for the given item
120
      *
117
      *
121
      * @param item The item to extract the key from
118
      * @param item The item to extract the key from
123
      */
120
      */
124
     getKeyExtractor = (item: Machine) => item.number;
121
     getKeyExtractor = (item: Machine) => item.number;
125
 
122
 
126
-
127
     /**
123
     /**
128
      * Setups notifications for the machine with the given ID.
124
      * Setups notifications for the machine with the given ID.
129
      * One notification will be sent at the end of the program.
125
      * One notification will be sent at the end of the program.
141
                     this.showNotificationsDisabledWarning();
137
                     this.showNotificationsDisabledWarning();
142
                 });
138
                 });
143
         } else {
139
         } else {
144
-            Notifications.setupMachineNotification(machine.number, false)
140
+            Notifications.setupMachineNotification(machine.number, false, null)
145
                 .then(() => {
141
                 .then(() => {
146
                     this.removeNotificationFromState(machine);
142
                     this.removeNotificationFromState(machine);
147
                 });
143
                 });

+ 1
- 1
src/utils/Notifications.js View File

62
  * @param isEnabled True to enable notifications, false to disable
62
  * @param isEnabled True to enable notifications, false to disable
63
  * @param endDate
63
  * @param endDate
64
  */
64
  */
65
-export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate?: Date) {
65
+export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) {
66
     return new Promise((resolve, reject) => {
66
     return new Promise((resolve, reject) => {
67
         if (isEnabled && endDate != null) {
67
         if (isEnabled && endDate != null) {
68
             askPermissions()
68
             askPermissions()

+ 20
- 7
src/utils/Proxiwash.js View File

4
 
4
 
5
 /**
5
 /**
6
  * Gets the machine end Date object.
6
  * Gets the machine end Date object.
7
- * If the time is before the current time, it will be considered as tomorrow
7
+ * If the end time is at least 12 hours before the current time,
8
+ * it will be considered as happening the day after.
9
+ * If it is before but less than 12 hours, it will be considered invalid (to fix proxiwash delay)
8
  *
10
  *
9
  * @param machine The machine to get the date from
11
  * @param machine The machine to get the date from
10
  * @returns {Date} The date object representing the end time.
12
  * @returns {Date} The date object representing the end time.
11
  */
13
  */
12
-export function getMachineEndDate(machine: Machine) {
14
+export function getMachineEndDate(machine: Machine): Date | null {
13
     const array = machine.endTime.split(":");
15
     const array = machine.endTime.split(":");
14
-    let date = new Date(Date.now());
15
-    date.setHours(parseInt(array[0]), parseInt(array[1]));
16
-    if (date < new Date(Date.now()))
17
-        date.setDate(date.getDate() + 1);
18
-    return date;
16
+    let endDate = new Date(Date.now());
17
+    endDate.setHours(parseInt(array[0]), parseInt(array[1]));
18
+
19
+    let limit = new Date(Date.now());
20
+    if (endDate < limit) {
21
+        if (limit.getHours() > 12) {
22
+            limit.setHours(limit.getHours() - 12);
23
+            if (endDate < limit)
24
+                endDate.setDate(endDate.getDate() + 1);
25
+            else
26
+                endDate = null;
27
+        } else
28
+            endDate = null;
29
+    }
30
+
31
+    return endDate;
19
 }
32
 }
20
 
33
 
21
 /**
34
 /**

Loading…
Cancel
Save