diff --git a/screens/ProxiwashScreen.js b/screens/ProxiwashScreen.js index 6bb9ed5..24ef7bc 100644 --- a/screens/ProxiwashScreen.js +++ b/screens/ProxiwashScreen.js @@ -83,24 +83,25 @@ export default class ProxiwashScreen extends FetchedDataSectionList { */ componentDidMount() { super.componentDidMount(); - // Get latest watchlist from server - NotificationsManager.getMachineNotificationWatchlist((fetchedList) => { - this.setState({machinesWatched: fetchedList}) - }); - // Get updated watchlist after received notification - Expo.Notifications.addListener((notification) => { + if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') { + // Get latest watchlist from server NotificationsManager.getMachineNotificationWatchlist((fetchedList) => { this.setState({machinesWatched: fetchedList}) }); - }); - - if (Platform.OS === 'android') { - Expo.Notifications.createChannelAndroidAsync('reminders', { - name: 'Reminders', - priority: 'max', - vibrate: [0, 250, 250, 250], + // Get updated watchlist after received notification + Expo.Notifications.addListener((notification) => { + NotificationsManager.getMachineNotificationWatchlist((fetchedList) => { + this.setState({machinesWatched: fetchedList}) + }); }); + if (Platform.OS === 'android') { + Expo.Notifications.createChannelAndroidAsync('reminders', { + name: 'Reminders', + priority: 'max', + vibrate: [0, 250, 250, 250], + }); + } } } @@ -129,11 +130,22 @@ export default class ProxiwashScreen extends FetchedDataSectionList { * @returns {Promise} */ setupNotifications(machineId: string) { - if (!this.isMachineWatched(machineId)) { - NotificationsManager.setupMachineNotification(machineId, true); - this.saveNotificationToState(machineId); - } else - this.disableNotification(machineId); + if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') { + if (!this.isMachineWatched(machineId)) { + NotificationsManager.setupMachineNotification(machineId, true); + this.saveNotificationToState(machineId); + } else + this.disableNotification(machineId); + } else { + this.showNotificationsDisabledWarning(); + } + } + + showNotificationsDisabledWarning() { + Alert.alert( + i18n.t("proxiwashScreen.modal.notificationErrorTitle"), + i18n.t("proxiwashScreen.modal.notificationErrorDescription"), + ); } /** diff --git a/translations/en.json b/translations/en.json index e464310..d2b3fc5 100644 --- a/translations/en.json +++ b/translations/en.json @@ -126,7 +126,9 @@ "ready": "This machine is empty and ready to use.", "running": "This machine has been started at %{start} and will end at %{end}.\nRemaining time: %{remaining} min", "broken": "This machine is broken and cannot be used. Thank you for your comprehension.", - "error": "There has been an error and we are unable to get information from this machine. Sorry for the inconvenience." + "error": "There has been an error and we are unable to get information from this machine. Sorry for the inconvenience.", + "notificationErrorTitle": "Error", + "notificationErrorDescription": "Impossible to create notifications. Please make sure you enabled notifications then restart the app." }, "states": { "finished": "FINISHED", diff --git a/translations/fr.json b/translations/fr.json index b7ab6af..864317c 100644 --- a/translations/fr.json +++ b/translations/fr.json @@ -126,7 +126,10 @@ "ready": "Cette machine est vide et prête à être utilisée.", "running": "Cette machine a démarré à %{start} et terminera à %{end}.\nTemps restant : %{remaining} min", "broken": "Cette machine est hors service. Merci pour votre compréhension.", - "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." + "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.", + "notificationErrorTitle": "Erreur", + "notificationErrorDescription": "Impossible de créer les notifications. Merci de vérifier que vous avez activé les notifications puis redémarrez l'appli." + }, "states": { "finished": "TERMINE", diff --git a/utils/NotificationsManager.js b/utils/NotificationsManager.js index 90849a8..b2c10b2 100644 --- a/utils/NotificationsManager.js +++ b/utils/NotificationsManager.js @@ -110,29 +110,31 @@ export default class NotificationsManager { static getMachineNotificationWatchlist(callback: Function) { let token = AsyncStorageManager.getInstance().preferences.expoToken.current; - if (token === '') { - throw Error('Expo token not available'); - } - let data = { - function: 'get_machine_watchlist', - password: passwords.expoNotifications, - token: token, - }; - fetch(EXPO_TOKEN_SERVER, { - method: 'POST', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - }), - body: JSON.stringify(data) // <-- Post parameters - }) - .then((response) => response.json()) - .then((responseJson) => { - callback(responseJson); + if (token !== '') { + let data = { + function: 'get_machine_watchlist', + password: passwords.expoNotifications, + token: token, + }; + fetch(EXPO_TOKEN_SERVER, { + method: 'POST', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + }), + body: JSON.stringify(data) // <-- Post parameters }) - .catch((error) => { - console.log(error); - }); + .then((response) => response.json()) + .then((responseJson) => { + callback(responseJson); + }) + .catch((error) => { + console.log(error); + }); + } else { + console.log('Expo token not available'); + } + } /** @@ -143,32 +145,33 @@ export default class NotificationsManager { */ static setupMachineNotification(machineID: string, isEnabled: boolean) { let token = AsyncStorageManager.getInstance().preferences.expoToken.current; - if (token === '') { - throw Error('Expo token not available'); - } - let data = { - function: 'setup_machine_notification', - password: passwords.expoNotifications, - locale: LocaleManager.getCurrentLocale(), - token: token, - machine_id: machineID, - enabled: isEnabled - }; - fetch(EXPO_TOKEN_SERVER, { - method: 'POST', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - }), - body: JSON.stringify(data) // <-- Post parameters - }) - .then((response) => response.text()) - .then((responseText) => { - console.log(responseText); + if (token !== '') { + let data = { + function: 'setup_machine_notification', + password: passwords.expoNotifications, + locale: LocaleManager.getCurrentLocale(), + token: token, + machine_id: machineID, + enabled: isEnabled + }; + fetch(EXPO_TOKEN_SERVER, { + method: 'POST', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + }), + body: JSON.stringify(data) // <-- Post parameters }) - .catch((error) => { - console.log(error); - }); + .then((response) => response.text()) + .then((responseText) => { + console.log(responseText); + }) + .catch((error) => { + console.log(error); + }); + } else { + console.log('Expo token not available'); + } } /** @@ -177,29 +180,30 @@ export default class NotificationsManager { */ static setMachineReminderNotificationTime(time: number) { let token = AsyncStorageManager.getInstance().preferences.expoToken.current; - if (token === '') { - throw Error('Expo token not available'); - } - let data = { - function: 'set_machine_reminder', - password: passwords.expoNotifications, - token: token, - time: time, - }; - fetch(EXPO_TOKEN_SERVER, { - method: 'POST', - headers: new Headers({ - Accept: 'application/json', - 'Content-Type': 'application/json', - }), - body: JSON.stringify(data) // <-- Post parameters - }) - .then((response) => response.text()) - .then((responseText) => { - console.log(responseText); + if (token !== '') { + let data = { + function: 'set_machine_reminder', + password: passwords.expoNotifications, + token: token, + time: time, + }; + fetch(EXPO_TOKEN_SERVER, { + method: 'POST', + headers: new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + }), + body: JSON.stringify(data) // <-- Post parameters }) - .catch((error) => { - console.log(error); - }); + .then((response) => response.text()) + .then((responseText) => { + console.log(responseText); + }) + .catch((error) => { + console.log(error); + }); + } else { + console.log('Expo token not available'); + } } }