Browse Source

Synchronised watchlist with server

keplyx 4 years ago
parent
commit
19039f52cc
2 changed files with 45 additions and 7 deletions
  1. 19
    7
      screens/ProxiwashScreen.js
  2. 26
    0
      utils/NotificationsManager.js

+ 19
- 7
screens/ProxiwashScreen.js View File

@@ -83,6 +83,18 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
83 83
      */
84 84
     componentDidMount() {
85 85
         super.componentDidMount();
86
+        // Get latest watchlist from server
87
+        NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
88
+            this.setState({machinesWatched: fetchedList})
89
+        });
90
+
91
+        // Get updated watchlist after received notification
92
+        Expo.Notifications.addListener((notification) => {
93
+            NotificationsManager.getMachineNotificationWatchlist((fetchedList) => {
94
+                this.setState({machinesWatched: fetchedList})
95
+            });
96
+        });
97
+
86 98
         if (Platform.OS === 'android') {
87 99
             Expo.Notifications.createChannelAndroidAsync('reminders', {
88 100
                 name: 'Reminders',
@@ -137,7 +149,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
137 149
     setupNotifications(machineId: string) {
138 150
         if (!this.isMachineWatched(machineId)) {
139 151
             NotificationsManager.setupMachineNotification(machineId, true);
140
-            this.saveNotificationToPrefs(machineId);
152
+            this.saveNotificationToState(machineId);
141 153
         } else
142 154
             this.disableNotification(machineId);
143 155
     }
@@ -154,7 +166,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
154 166
             let arrayIndex = data.indexOf(machineId);
155 167
             if (arrayIndex !== -1) {
156 168
                 NotificationsManager.setupMachineNotification(machineId, false);
157
-                this.removeNotificationFromPrefs(arrayIndex);
169
+                this.removeNotificationFroState(arrayIndex);
158 170
             }
159 171
         }
160 172
     }
@@ -164,10 +176,10 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
164 176
      *
165 177
      * @param machineId
166 178
      */
167
-    saveNotificationToPrefs(machineId: string) {
179
+    saveNotificationToState(machineId: string) {
168 180
         let data = this.state.machinesWatched;
169 181
         data.push(machineId);
170
-        this.updateNotificationPrefs(data);
182
+        this.updateNotificationState(data);
171 183
     }
172 184
 
173 185
     /**
@@ -175,10 +187,10 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
175 187
      *
176 188
      * @param index
177 189
      */
178
-    removeNotificationFromPrefs(index: number) {
190
+    removeNotificationFroState(index: number) {
179 191
         let data = this.state.machinesWatched;
180 192
         data.splice(index, 1);
181
-        this.updateNotificationPrefs(data);
193
+        this.updateNotificationState(data);
182 194
     }
183 195
 
184 196
     /**
@@ -186,7 +198,7 @@ export default class ProxiwashScreen extends FetchedDataSectionList {
186 198
      *
187 199
      * @param data
188 200
      */
189
-    updateNotificationPrefs(data: Array<Object>) {
201
+    updateNotificationState(data: Array<Object>) {
190 202
         this.setState({machinesWatched: data});
191 203
         // let prefKey = AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key;
192 204
         // AsyncStorageManager.getInstance().savePref(prefKey, JSON.stringify(data));

+ 26
- 0
utils/NotificationsManager.js View File

@@ -100,6 +100,32 @@ export default class NotificationsManager {
100 100
         }
101 101
     }
102 102
 
103
+    static getMachineNotificationWatchlist(callback: Function) {
104
+        let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
105
+        if (token === '') {
106
+            throw Error('Expo token not available');
107
+        }
108
+        let data = {
109
+            function: 'get_machine_watchlist',
110
+            token: token,
111
+        };
112
+        fetch(EXPO_TOKEN_SERVER, {
113
+            method: 'POST',
114
+            headers: new Headers({
115
+                Accept: 'application/json',
116
+                'Content-Type': 'application/json',
117
+            }),
118
+            body: JSON.stringify(data) // <-- Post parameters
119
+        })
120
+            .then((response) => response.json())
121
+            .then((responseJson) => {
122
+                callback(responseJson);
123
+            })
124
+            .catch((error) => {
125
+                console.log(error);
126
+            });
127
+    }
128
+
103 129
     /**
104 130
      * Ask the server to enable/disable notifications for the specified machine
105 131
      *

Loading…
Cancel
Save