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,21 +4,29 @@ import {getCleanedMachineWatched, getMachineEndDate, getMachineOfId, isMachineWa
4 4
 test('getMachineEndDate', () => {
5 5
     jest.spyOn(Date, 'now')
6 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 10
     expectDate.setHours(23);
11 11
     expectDate.setMinutes(10);
12 12
     expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime());
13 13
 
14
-    expectDate.setHours(15);
14
+    expectDate.setHours(16);
15 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 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 32
 test('isMachineWatched', () => {

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

@@ -18,6 +18,7 @@ import type {CustomTheme} from "../../managers/ThemeManager";
18 18
 import {Collapsible} from "react-navigation-collapsible";
19 19
 import {StackNavigationProp} from "@react-navigation/stack";
20 20
 import {getCleanedMachineWatched, getMachineEndDate, isMachineWatched} from "../../utils/Proxiwash";
21
+import {Modalize} from "react-native-modalize";
21 22
 
22 23
 const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/washinsa/washinsa.json";
23 24
 
@@ -55,9 +56,12 @@ type State = {
55 56
  */
56 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 66
     state = {
63 67
         refreshing: false,
@@ -95,7 +99,10 @@ class ProxiwashScreen extends React.Component<Props, State> {
95 99
      */
96 100
     componentDidMount() {
97 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,16 +113,6 @@ class ProxiwashScreen extends React.Component<Props, State> {
106 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 116
      * Extracts the key for the given item
120 117
      *
121 118
      * @param item The item to extract the key from
@@ -123,7 +120,6 @@ class ProxiwashScreen extends React.Component<Props, State> {
123 120
      */
124 121
     getKeyExtractor = (item: Machine) => item.number;
125 122
 
126
-
127 123
     /**
128 124
      * Setups notifications for the machine with the given ID.
129 125
      * One notification will be sent at the end of the program.
@@ -141,7 +137,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
141 137
                     this.showNotificationsDisabledWarning();
142 138
                 });
143 139
         } else {
144
-            Notifications.setupMachineNotification(machine.number, false)
140
+            Notifications.setupMachineNotification(machine.number, false, null)
145 141
                 .then(() => {
146 142
                     this.removeNotificationFromState(machine);
147 143
                 });

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

@@ -62,7 +62,7 @@ function createNotifications(machineID: string, date: Date) {
62 62
  * @param isEnabled True to enable notifications, false to disable
63 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 66
     return new Promise((resolve, reject) => {
67 67
         if (isEnabled && endDate != null) {
68 68
             askPermissions()

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

@@ -4,18 +4,31 @@ import type {Machine} from "../screens/Proxiwash/ProxiwashScreen";
4 4
 
5 5
 /**
6 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 11
  * @param machine The machine to get the date from
10 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 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