forked from vergnet/application-amicale
Fixed crash when expo token not available
This commit is contained in:
parent
540d94d877
commit
575b900268
4 changed files with 111 additions and 90 deletions
|
@ -83,18 +83,18 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
|||
*/
|
||||
componentDidMount() {
|
||||
super.componentDidMount();
|
||||
|
||||
if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
|
||||
// Get latest watchlist from server
|
||||
NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
|
||||
this.setState({machinesWatched: fetchedList})
|
||||
});
|
||||
|
||||
// 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',
|
||||
|
@ -103,6 +103,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
|||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getHeaderTranslation() {
|
||||
return i18n.t("screens.proxiwash");
|
||||
|
@ -129,11 +130,22 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
|||
* @returns {Promise<void>}
|
||||
*/
|
||||
setupNotifications(machineId: string) {
|
||||
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"),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -110,9 +110,7 @@ export default class NotificationsManager {
|
|||
|
||||
static getMachineNotificationWatchlist(callback: Function) {
|
||||
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
||||
if (token === '') {
|
||||
throw Error('Expo token not available');
|
||||
}
|
||||
if (token !== '') {
|
||||
let data = {
|
||||
function: 'get_machine_watchlist',
|
||||
password: passwords.expoNotifications,
|
||||
|
@ -133,6 +131,10 @@ export default class NotificationsManager {
|
|||
.catch((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) {
|
||||
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
||||
if (token === '') {
|
||||
throw Error('Expo token not available');
|
||||
}
|
||||
if (token !== '') {
|
||||
let data = {
|
||||
function: 'setup_machine_notification',
|
||||
password: passwords.expoNotifications,
|
||||
|
@ -169,6 +169,9 @@ export default class NotificationsManager {
|
|||
.catch((error) => {
|
||||
console.log(error);
|
||||
});
|
||||
} else {
|
||||
console.log('Expo token not available');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -177,9 +180,7 @@ export default class NotificationsManager {
|
|||
*/
|
||||
static setMachineReminderNotificationTime(time: number) {
|
||||
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
||||
if (token === '') {
|
||||
throw Error('Expo token not available');
|
||||
}
|
||||
if (token !== '') {
|
||||
let data = {
|
||||
function: 'set_machine_reminder',
|
||||
password: passwords.expoNotifications,
|
||||
|
@ -201,5 +202,8 @@ export default class NotificationsManager {
|
|||
.catch((error) => {
|
||||
console.log(error);
|
||||
});
|
||||
} else {
|
||||
console.log('Expo token not available');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue