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,24 +83,25 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
*/ */
componentDidMount() { componentDidMount() {
super.componentDidMount(); super.componentDidMount();
// Get latest watchlist from server
NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
this.setState({machinesWatched: fetchedList})
});
// Get updated watchlist after received notification if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
Expo.Notifications.addListener((notification) => { // 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
Expo.Notifications.addListener((notification) => {
if (Platform.OS === 'android') { NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
Expo.Notifications.createChannelAndroidAsync('reminders', { this.setState({machinesWatched: fetchedList})
name: 'Reminders', });
priority: 'max',
vibrate: [0, 250, 250, 250],
}); });
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<void>} * @returns {Promise<void>}
*/ */
setupNotifications(machineId: string) { setupNotifications(machineId: string) {
if (!this.isMachineWatched(machineId)) { if (AsyncStorageManager.getInstance().preferences.expoToken.current !== '') {
NotificationsManager.setupMachineNotification(machineId, true); if (!this.isMachineWatched(machineId)) {
this.saveNotificationToState(machineId); NotificationsManager.setupMachineNotification(machineId, true);
} else this.saveNotificationToState(machineId);
this.disableNotification(machineId); } else
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,29 +110,31 @@ 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 = {
} function: 'get_machine_watchlist',
let data = { password: passwords.expoNotifications,
function: 'get_machine_watchlist', token: token,
password: passwords.expoNotifications, };
token: token, fetch(EXPO_TOKEN_SERVER, {
}; method: 'POST',
fetch(EXPO_TOKEN_SERVER, { headers: new Headers({
method: 'POST', Accept: 'application/json',
headers: new Headers({ 'Content-Type': 'application/json',
Accept: 'application/json', }),
'Content-Type': 'application/json', body: JSON.stringify(data) // <-- Post parameters
}),
body: JSON.stringify(data) // <-- Post parameters
})
.then((response) => response.json())
.then((responseJson) => {
callback(responseJson);
}) })
.catch((error) => { .then((response) => response.json())
console.log(error); .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) { 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 = {
} function: 'setup_machine_notification',
let data = { password: passwords.expoNotifications,
function: 'setup_machine_notification', locale: LocaleManager.getCurrentLocale(),
password: passwords.expoNotifications, token: token,
locale: LocaleManager.getCurrentLocale(), machine_id: machineID,
token: token, enabled: isEnabled
machine_id: machineID, };
enabled: isEnabled fetch(EXPO_TOKEN_SERVER, {
}; method: 'POST',
fetch(EXPO_TOKEN_SERVER, { headers: new Headers({
method: 'POST', Accept: 'application/json',
headers: new Headers({ 'Content-Type': 'application/json',
Accept: 'application/json', }),
'Content-Type': 'application/json', body: JSON.stringify(data) // <-- Post parameters
}),
body: JSON.stringify(data) // <-- Post parameters
})
.then((response) => response.text())
.then((responseText) => {
console.log(responseText);
}) })
.catch((error) => { .then((response) => response.text())
console.log(error); .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) { 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 = {
} function: 'set_machine_reminder',
let data = { password: passwords.expoNotifications,
function: 'set_machine_reminder', token: token,
password: passwords.expoNotifications, time: time,
token: token, };
time: time, fetch(EXPO_TOKEN_SERVER, {
}; method: 'POST',
fetch(EXPO_TOKEN_SERVER, { headers: new Headers({
method: 'POST', Accept: 'application/json',
headers: new Headers({ 'Content-Type': 'application/json',
Accept: 'application/json', }),
'Content-Type': 'application/json', body: JSON.stringify(data) // <-- Post parameters
}),
body: JSON.stringify(data) // <-- Post parameters
})
.then((response) => response.text())
.then((responseText) => {
console.log(responseText);
}) })
.catch((error) => { .then((response) => response.text())
console.log(error); .then((responseText) => {
}); console.log(responseText);
})
.catch((error) => {
console.log(error);
});
} else {
console.log('Expo token not available');
}
} }
} }