From 19039f52cce73e5f4e6cbc34049d534458a42ca2 Mon Sep 17 00:00:00 2001 From: keplyx Date: Tue, 13 Aug 2019 12:53:21 +0200 Subject: [PATCH] Synchronised watchlist with server --- screens/ProxiwashScreen.js | 26 +++++++++++++++++++------- utils/NotificationsManager.js | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/screens/ProxiwashScreen.js b/screens/ProxiwashScreen.js index d630203..2d77e3e 100644 --- a/screens/ProxiwashScreen.js +++ b/screens/ProxiwashScreen.js @@ -83,6 +83,18 @@ 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) => { + NotificationsManager.getMachineNotificationWatchlist((fetchedList) => { + this.setState({machinesWatched: fetchedList}) + }); + }); + if (Platform.OS === 'android') { Expo.Notifications.createChannelAndroidAsync('reminders', { name: 'Reminders', @@ -137,7 +149,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList { setupNotifications(machineId: string) { if (!this.isMachineWatched(machineId)) { NotificationsManager.setupMachineNotification(machineId, true); - this.saveNotificationToPrefs(machineId); + this.saveNotificationToState(machineId); } else this.disableNotification(machineId); } @@ -154,7 +166,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList { let arrayIndex = data.indexOf(machineId); if (arrayIndex !== -1) { NotificationsManager.setupMachineNotification(machineId, false); - this.removeNotificationFromPrefs(arrayIndex); + this.removeNotificationFroState(arrayIndex); } } } @@ -164,10 +176,10 @@ export default class ProxiwashScreen extends FetchedDataSectionList { * * @param machineId */ - saveNotificationToPrefs(machineId: string) { + saveNotificationToState(machineId: string) { let data = this.state.machinesWatched; data.push(machineId); - this.updateNotificationPrefs(data); + this.updateNotificationState(data); } /** @@ -175,10 +187,10 @@ export default class ProxiwashScreen extends FetchedDataSectionList { * * @param index */ - removeNotificationFromPrefs(index: number) { + removeNotificationFroState(index: number) { let data = this.state.machinesWatched; data.splice(index, 1); - this.updateNotificationPrefs(data); + this.updateNotificationState(data); } /** @@ -186,7 +198,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList { * * @param data */ - updateNotificationPrefs(data: Array) { + updateNotificationState(data: Array) { this.setState({machinesWatched: data}); // let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key; // AsyncStorageManager.getInstance().savePref(prefKey, JSON.stringify(data)); diff --git a/utils/NotificationsManager.js b/utils/NotificationsManager.js index 7ae112f..b281641 100644 --- a/utils/NotificationsManager.js +++ b/utils/NotificationsManager.js @@ -100,6 +100,32 @@ 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', + 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); + }) + .catch((error) => { + console.log(error); + }); + } + /** * Ask the server to enable/disable notifications for the specified machine *