Browse Source

Fixed crash when expo token not available

keplyx 4 years ago
parent
commit
575b900268
4 changed files with 111 additions and 90 deletions
  1. 30
    18
      screens/ProxiwashScreen.js
  2. 3
    1
      translations/en.json
  3. 4
    1
      translations/fr.json
  4. 74
    70
      utils/NotificationsManager.js

+ 30
- 18
screens/ProxiwashScreen.js View File

@@ -83,24 +83,25 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
83 83
      */
84 84
     componentDidMount() {
85 85
         super.componentDidMount();
86
-        // Get latest watchlist from server
87
-        NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
88
-            this.setState({machinesWatched: fetchedList})
89
-        });
90 86
 
91
-        // Get updated watchlist after received notification
92
-        Expo.Notifications.addListener((notification) => {
87
+        if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
88
+            // Get latest watchlist from server
93 89
             NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
94 90
                 this.setState({machinesWatched: fetchedList})
95 91
             });
96
-        });
97
-
98
-        if (Platform.OS === 'android') {
99
-            Expo.Notifications.createChannelAndroidAsync('reminders', {
100
-                name: 'Reminders',
101
-                priority: 'max',
102
-                vibrate: [0, 250, 250, 250],
92
+            // Get updated watchlist after received notification
93
+            Expo.Notifications.addListener((notification) => {
94
+                NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
95
+                    this.setState({machinesWatched: fetchedList})
96
+                });
103 97
             });
98
+            if (Platform.OS === 'android') {
99
+                Expo.Notifications.createChannelAndroidAsync('reminders', {
100
+                    name: 'Reminders',
101
+                    priority: 'max',
102
+                    vibrate: [0, 250, 250, 250],
103
+                });
104
+            }
104 105
         }
105 106
     }
106 107
 
@@ -129,11 +130,22 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
129 130
      * @returns {Promise<void>}
130 131
      */
131 132
     setupNotifications(machineId: string) {
132
-        if (!this.isMachineWatched(machineId)) {
133
-            NotificationsManager.setupMachineNotification(machineId, true);
134
-            this.saveNotificationToState(machineId);
135
-        } else
136
-            this.disableNotification(machineId);
133
+        if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
134
+            if (!this.isMachineWatched(machineId)) {
135
+                NotificationsManager.setupMachineNotification(machineId, true);
136
+                this.saveNotificationToState(machineId);
137
+            } else
138
+                this.disableNotification(machineId);
139
+        } else {
140
+            this.showNotificationsDisabledWarning();
141
+        }
142
+    }
143
+
144
+    showNotificationsDisabledWarning() {
145
+        Alert.alert(
146
+            i18n.t("proxiwashScreen.modal.notificationErrorTitle"),
147
+            i18n.t("proxiwashScreen.modal.notificationErrorDescription"),
148
+        );
137 149
     }
138 150
 
139 151
     /**

+ 3
- 1
translations/en.json View File

@@ -126,7 +126,9 @@
126 126
       "ready": "This machine is empty and ready to use.",
127 127
       "running": "This machine has been started at %{start} and will end at %{end}.\nRemaining time: %{remaining} min",
128 128
       "broken": "This machine is broken and cannot be used. Thank you for your comprehension.",
129
-      "error": "There has been an error and we are unable to get information from this machine. Sorry for the inconvenience."
129
+      "error": "There has been an error and we are unable to get information from this machine. Sorry for the inconvenience.",
130
+      "notificationErrorTitle": "Error",
131
+      "notificationErrorDescription": "Impossible to create notifications. Please make sure you enabled notifications then restart the app."
130 132
     },
131 133
     "states": {
132 134
       "finished": "FINISHED",

+ 4
- 1
translations/fr.json View File

@@ -126,7 +126,10 @@
126 126
       "ready": "Cette machine est vide et prête à être utilisée.",
127 127
       "running": "Cette machine a démarré à %{start} et terminera à %{end}.\nTemps restant : %{remaining} min",
128 128
       "broken": "Cette machine est hors service. Merci pour votre compréhension.",
129
-      "error": "Il y a eu une erreur et il est impossible de récupérer les informations de cette machine. Veuillez nous excuser pour le gène occasionnée."
129
+      "error": "Il y a eu une erreur et il est impossible de récupérer les informations de cette machine. Veuillez nous excuser pour le gène occasionnée.",
130
+      "notificationErrorTitle": "Erreur",
131
+      "notificationErrorDescription": "Impossible de créer les notifications. Merci de vérifier que vous avez activé les notifications puis redémarrez l'appli."
132
+
130 133
     },
131 134
     "states": {
132 135
       "finished": "TERMINE",

+ 74
- 70
utils/NotificationsManager.js View File

@@ -110,29 +110,31 @@ export default class NotificationsManager {
110 110
 
111 111
     static getMachineNotificationWatchlist(callback: Function) {
112 112
         let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
113
-        if (token === '') {
114
-            throw Error('Expo token not available');
115
-        }
116
-        let data = {
117
-            function: 'get_machine_watchlist',
118
-            password: passwords.expoNotifications,
119
-            token: token,
120
-        };
121
-        fetch(EXPO_TOKEN_SERVER, {
122
-            method: 'POST',
123
-            headers: new Headers({
124
-                Accept: 'application/json',
125
-                'Content-Type': 'application/json',
126
-            }),
127
-            body: JSON.stringify(data) // <-- Post parameters
128
-        })
129
-            .then((response) => response.json())
130
-            .then((responseJson) => {
131
-                callback(responseJson);
113
+        if (token !== '') {
114
+            let data = {
115
+                function: 'get_machine_watchlist',
116
+                password: passwords.expoNotifications,
117
+                token: token,
118
+            };
119
+            fetch(EXPO_TOKEN_SERVER, {
120
+                method: 'POST',
121
+                headers: new Headers({
122
+                    Accept: 'application/json',
123
+                    'Content-Type': 'application/json',
124
+                }),
125
+                body: JSON.stringify(data) // <-- Post parameters
132 126
             })
133
-            .catch((error) => {
134
-                console.log(error);
135
-            });
127
+                .then((response) => response.json())
128
+                .then((responseJson) => {
129
+                    callback(responseJson);
130
+                })
131
+                .catch((error) => {
132
+                    console.log(error);
133
+                });
134
+        } else {
135
+            console.log('Expo token not available');
136
+        }
137
+
136 138
     }
137 139
 
138 140
     /**
@@ -143,32 +145,33 @@ export default class NotificationsManager {
143 145
      */
144 146
     static setupMachineNotification(machineID: string, isEnabled: boolean) {
145 147
         let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
146
-        if (token === '') {
147
-            throw Error('Expo token not available');
148
-        }
149
-        let data = {
150
-            function: 'setup_machine_notification',
151
-            password: passwords.expoNotifications,
152
-            locale: LocaleManager.getCurrentLocale(),
153
-            token: token,
154
-            machine_id: machineID,
155
-            enabled: isEnabled
156
-        };
157
-        fetch(EXPO_TOKEN_SERVER, {
158
-            method: 'POST',
159
-            headers: new Headers({
160
-                Accept: 'application/json',
161
-                'Content-Type': 'application/json',
162
-            }),
163
-            body: JSON.stringify(data) // <-- Post parameters
164
-        })
165
-            .then((response) => response.text())
166
-            .then((responseText) => {
167
-                console.log(responseText);
148
+        if (token !== '') {
149
+            let data = {
150
+                function: 'setup_machine_notification',
151
+                password: passwords.expoNotifications,
152
+                locale: LocaleManager.getCurrentLocale(),
153
+                token: token,
154
+                machine_id: machineID,
155
+                enabled: isEnabled
156
+            };
157
+            fetch(EXPO_TOKEN_SERVER, {
158
+                method: 'POST',
159
+                headers: new Headers({
160
+                    Accept: 'application/json',
161
+                    'Content-Type': 'application/json',
162
+                }),
163
+                body: JSON.stringify(data) // <-- Post parameters
168 164
             })
169
-            .catch((error) => {
170
-                console.log(error);
171
-            });
165
+                .then((response) => response.text())
166
+                .then((responseText) => {
167
+                    console.log(responseText);
168
+                })
169
+                .catch((error) => {
170
+                    console.log(error);
171
+                });
172
+        } else {
173
+            console.log('Expo token not available');
174
+        }
172 175
     }
173 176
 
174 177
     /**
@@ -177,29 +180,30 @@ export default class NotificationsManager {
177 180
      */
178 181
     static setMachineReminderNotificationTime(time: number) {
179 182
         let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
180
-        if (token === '') {
181
-            throw Error('Expo token not available');
182
-        }
183
-        let data = {
184
-            function: 'set_machine_reminder',
185
-            password: passwords.expoNotifications,
186
-            token: token,
187
-            time: time,
188
-        };
189
-        fetch(EXPO_TOKEN_SERVER, {
190
-            method: 'POST',
191
-            headers: new Headers({
192
-                Accept: 'application/json',
193
-                'Content-Type': 'application/json',
194
-            }),
195
-            body: JSON.stringify(data) // <-- Post parameters
196
-        })
197
-            .then((response) => response.text())
198
-            .then((responseText) => {
199
-                console.log(responseText);
183
+        if (token !== '') {
184
+            let data = {
185
+                function: 'set_machine_reminder',
186
+                password: passwords.expoNotifications,
187
+                token: token,
188
+                time: time,
189
+            };
190
+            fetch(EXPO_TOKEN_SERVER, {
191
+                method: 'POST',
192
+                headers: new Headers({
193
+                    Accept: 'application/json',
194
+                    'Content-Type': 'application/json',
195
+                }),
196
+                body: JSON.stringify(data) // <-- Post parameters
200 197
             })
201
-            .catch((error) => {
202
-                console.log(error);
203
-            });
198
+                .then((response) => response.text())
199
+                .then((responseText) => {
200
+                    console.log(responseText);
201
+                })
202
+                .catch((error) => {
203
+                    console.log(error);
204
+                });
205
+        } else {
206
+            console.log('Expo token not available');
207
+        }
204 208
     }
205 209
 }

Loading…
Cancel
Save