Fixed crash when expo token not available

This commit is contained in:
keplyx 2019-09-12 23:23:36 +02:00
parent 540d94d877
commit 575b900268
4 changed files with 111 additions and 90 deletions

View file

@ -83,18 +83,18 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
*/ */
componentDidMount() { componentDidMount() {
super.componentDidMount(); super.componentDidMount();
if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
// Get latest watchlist from server // Get latest watchlist from server
NotificationsManager.getMachineNotificationWatchlist((fetchedList) => { NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
this.setState({machinesWatched: fetchedList}) this.setState({machinesWatched: fetchedList})
}); });
// Get updated watchlist after received notification // Get updated watchlist after received notification
Expo.Notifications.addListener((notification) => { Expo.Notifications.addListener((notification) => {
NotificationsManager.getMachineNotificationWatchlist((fetchedList) => { NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
this.setState({machinesWatched: fetchedList}) this.setState({machinesWatched: fetchedList})
}); });
}); });
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
Expo.Notifications.createChannelAndroidAsync('reminders', { Expo.Notifications.createChannelAndroidAsync('reminders', {
name: 'Reminders', name: 'Reminders',
@ -103,6 +103,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
}); });
} }
} }
}
getHeaderTranslation() { getHeaderTranslation() {
return i18n.t("screens.proxiwash"); return i18n.t("screens.proxiwash");
@ -129,11 +130,22 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
setupNotifications(machineId: string) { setupNotifications(machineId: string) {
if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
if (!this.isMachineWatched(machineId)) { if (!this.isMachineWatched(machineId)) {
NotificationsManager.setupMachineNotification(machineId, true); NotificationsManager.setupMachineNotification(machineId, true);
this.saveNotificationToState(machineId); this.saveNotificationToState(machineId);
} else } else
this.disableNotification(machineId); this.disableNotification(machineId);
} else {
this.showNotificationsDisabledWarning();
}
}
showNotificationsDisabledWarning() {
Alert.alert(
i18n.t("proxiwashScreen.modal.notificationErrorTitle"),
i18n.t("proxiwashScreen.modal.notificationErrorDescription"),
);
} }
/** /**

View file

@ -126,7 +126,9 @@
"ready": "This machine is empty and ready to use.", "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", "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.", "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": { "states": {
"finished": "FINISHED", "finished": "FINISHED",

View file

@ -126,7 +126,10 @@
"ready": "Cette machine est vide et prête à être utilisée.", "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", "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.", "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": { "states": {
"finished": "TERMINE", "finished": "TERMINE",

View file

@ -110,9 +110,7 @@ export default class NotificationsManager {
static getMachineNotificationWatchlist(callback: Function) { static getMachineNotificationWatchlist(callback: Function) {
let token = AsyncStorageManager.getInstance().preferences.expoToken.current; let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
if (token === '') { if (token !== '') {
throw Error('Expo token not available');
}
let data = { let data = {
function: 'get_machine_watchlist', function: 'get_machine_watchlist',
password: passwords.expoNotifications, password: passwords.expoNotifications,
@ -133,6 +131,10 @@ export default class NotificationsManager {
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);
}); });
} else {
console.log('Expo token not available');
}
} }
/** /**
@ -143,9 +145,7 @@ export default class NotificationsManager {
*/ */
static setupMachineNotification(machineID: string, isEnabled: boolean) { static setupMachineNotification(machineID: string, isEnabled: boolean) {
let token = AsyncStorageManager.getInstance().preferences.expoToken.current; let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
if (token === '') { if (token !== '') {
throw Error('Expo token not available');
}
let data = { let data = {
function: 'setup_machine_notification', function: 'setup_machine_notification',
password: passwords.expoNotifications, password: passwords.expoNotifications,
@ -169,6 +169,9 @@ export default class NotificationsManager {
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);
}); });
} else {
console.log('Expo token not available');
}
} }
/** /**
@ -177,9 +180,7 @@ export default class NotificationsManager {
*/ */
static setMachineReminderNotificationTime(time: number) { static setMachineReminderNotificationTime(time: number) {
let token = AsyncStorageManager.getInstance().preferences.expoToken.current; let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
if (token === '') { if (token !== '') {
throw Error('Expo token not available');
}
let data = { let data = {
function: 'set_machine_reminder', function: 'set_machine_reminder',
password: passwords.expoNotifications, password: passwords.expoNotifications,
@ -201,5 +202,8 @@ export default class NotificationsManager {
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);
}); });
} else {
console.log('Expo token not available');
}
} }
} }