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() {
|
componentDidMount() {
|
||||||
super.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') {
|
if (Platform.OS === 'android') {
|
||||||
Expo.Notifications.createChannelAndroidAsync('reminders', {
|
Expo.Notifications.createChannelAndroidAsync('reminders', {
|
||||||
name: 'Reminders',
|
name: 'Reminders',
|
||||||
|
@ -137,7 +149,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
||||||
setupNotifications(machineId: string) {
|
setupNotifications(machineId: string) {
|
||||||
if (!this.isMachineWatched(machineId)) {
|
if (!this.isMachineWatched(machineId)) {
|
||||||
NotificationsManager.setupMachineNotification(machineId, true);
|
NotificationsManager.setupMachineNotification(machineId, true);
|
||||||
this.saveNotificationToPrefs(machineId);
|
this.saveNotificationToState(machineId);
|
||||||
} else
|
} else
|
||||||
this.disableNotification(machineId);
|
this.disableNotification(machineId);
|
||||||
}
|
}
|
||||||
|
@ -154,7 +166,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
||||||
let arrayIndex = data.indexOf(machineId);
|
let arrayIndex = data.indexOf(machineId);
|
||||||
if (arrayIndex !== -1) {
|
if (arrayIndex !== -1) {
|
||||||
NotificationsManager.setupMachineNotification(machineId, false);
|
NotificationsManager.setupMachineNotification(machineId, false);
|
||||||
this.removeNotificationFromPrefs(arrayIndex);
|
this.removeNotificationFroState(arrayIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -164,10 +176,10 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
||||||
*
|
*
|
||||||
* @param machineId
|
* @param machineId
|
||||||
*/
|
*/
|
||||||
saveNotificationToPrefs(machineId: string) {
|
saveNotificationToState(machineId: string) {
|
||||||
let data = this.state.machinesWatched;
|
let data = this.state.machinesWatched;
|
||||||
data.push(machineId);
|
data.push(machineId);
|
||||||
this.updateNotificationPrefs(data);
|
this.updateNotificationState(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -175,10 +187,10 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
||||||
*
|
*
|
||||||
* @param index
|
* @param index
|
||||||
*/
|
*/
|
||||||
removeNotificationFromPrefs(index: number) {
|
removeNotificationFroState(index: number) {
|
||||||
let data = this.state.machinesWatched;
|
let data = this.state.machinesWatched;
|
||||||
data.splice(index, 1);
|
data.splice(index, 1);
|
||||||
this.updateNotificationPrefs(data);
|
this.updateNotificationState(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -186,7 +198,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
|
||||||
*
|
*
|
||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
updateNotificationPrefs(data: Array<Object>) {
|
updateNotificationState(data: Array<Object>) {
|
||||||
this.setState({machinesWatched: data});
|
this.setState({machinesWatched: data});
|
||||||
// let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key;
|
// let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key;
|
||||||
// AsyncStorageManager.getInstance().savePref(prefKey, JSON.stringify(data));
|
// 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
|
* Ask the server to enable/disable notifications for the specified machine
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue