forked from vergnet/application-amicale
Synchronised watchlist with server
This commit is contained in:
parent
8d4223333f
commit
19039f52cc
2 changed files with 45 additions and 7 deletions
|
@ -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<Object>) {
|
||||
updateNotificationState(data: Array<Object>) {
|
||||
this.setState({machinesWatched: data});
|
||||
// let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key;
|
||||
// AsyncStorageManager.getInstance().savePref(prefKey, JSON.stringify(data));
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue