|
@@ -1,10 +1,14 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
|
-import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions';
|
4
|
|
-import AsyncStorageManager from "../managers/AsyncStorageManager";
|
5
|
|
-import i18n from "i18n-js";
|
|
3
|
+import {
|
|
4
|
+ checkNotifications,
|
|
5
|
+ requestNotifications,
|
|
6
|
+ RESULTS,
|
|
7
|
+} from 'react-native-permissions';
|
|
8
|
+import i18n from 'i18n-js';
|
|
9
|
+import AsyncStorageManager from '../managers/AsyncStorageManager';
|
6
|
10
|
|
7
|
|
-const PushNotification = require("react-native-push-notification");
|
|
11
|
+const PushNotification = require('react-native-push-notification');
|
8
|
12
|
|
9
|
13
|
// Used to multiply the normal notification id to create the reminder one. It allows to find it back easily
|
10
|
14
|
const reminderIdFactor = 100;
|
|
@@ -13,25 +17,21 @@ const reminderIdFactor = 100;
|
13
|
17
|
* Async function asking permission to send notifications to the user.
|
14
|
18
|
* Used on ios.
|
15
|
19
|
*
|
16
|
|
- * @returns {Promise}
|
|
20
|
+ * @returns {Promise<void>}
|
17
|
21
|
*/
|
18
|
|
-export async function askPermissions() {
|
19
|
|
- return new Promise(((resolve, reject) => {
|
20
|
|
- checkNotifications().then(({status}) => {
|
21
|
|
- if (status === RESULTS.GRANTED)
|
22
|
|
- resolve();
|
23
|
|
- else if (status === RESULTS.BLOCKED)
|
24
|
|
- reject()
|
25
|
|
- else {
|
26
|
|
- requestNotifications().then(({status}) => {
|
27
|
|
- if (status === RESULTS.GRANTED)
|
28
|
|
- resolve();
|
29
|
|
- else
|
30
|
|
- reject();
|
31
|
|
- });
|
32
|
|
- }
|
|
22
|
+export async function askPermissions(): Promise<void> {
|
|
23
|
+ return new Promise((resolve: () => void, reject: () => void) => {
|
|
24
|
+ checkNotifications().then(({status}: {status: string}) => {
|
|
25
|
+ if (status === RESULTS.GRANTED) resolve();
|
|
26
|
+ else if (status === RESULTS.BLOCKED) reject();
|
|
27
|
+ else {
|
|
28
|
+ requestNotifications().then((result: {status: string}) => {
|
|
29
|
+ if (result.status === RESULTS.GRANTED) resolve();
|
|
30
|
+ else reject();
|
33
|
31
|
});
|
34
|
|
- }));
|
|
32
|
+ }
|
|
33
|
+ });
|
|
34
|
+ });
|
35
|
35
|
}
|
36
|
36
|
|
37
|
37
|
/**
|
|
@@ -43,27 +43,33 @@ export async function askPermissions() {
|
43
|
43
|
* @param date The date to trigger the notification at
|
44
|
44
|
*/
|
45
|
45
|
function createNotifications(machineID: string, date: Date) {
|
46
|
|
- let reminder = AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key);
|
47
|
|
- if (!isNaN(reminder) && reminder > 0) {
|
48
|
|
- let id = reminderIdFactor * parseInt(machineID);
|
49
|
|
- let reminderDate = new Date(date);
|
50
|
|
- reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
|
51
|
|
- PushNotification.localNotificationSchedule({
|
52
|
|
- title: i18n.t("screens.proxiwash.notifications.machineRunningTitle", {time: reminder}),
|
53
|
|
- message: i18n.t("screens.proxiwash.notifications.machineRunningBody", {number: machineID}),
|
54
|
|
- id: id.toString(),
|
55
|
|
- date: reminderDate,
|
56
|
|
- });
|
57
|
|
- console.log("Setting up notifications for ", date, " and reminder for ", reminderDate);
|
58
|
|
- } else
|
59
|
|
- console.log("Setting up notifications for ", date);
|
60
|
|
-
|
|
46
|
+ const reminder = AsyncStorageManager.getNumber(
|
|
47
|
+ AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
|
|
48
|
+ );
|
|
49
|
+ if (!Number.isNaN(reminder) && reminder > 0) {
|
|
50
|
+ const id = reminderIdFactor * parseInt(machineID, 10);
|
|
51
|
+ const reminderDate = new Date(date);
|
|
52
|
+ reminderDate.setMinutes(reminderDate.getMinutes() - reminder);
|
61
|
53
|
PushNotification.localNotificationSchedule({
|
62
|
|
- title: i18n.t("screens.proxiwash.notifications.machineFinishedTitle"),
|
63
|
|
- message: i18n.t("screens.proxiwash.notifications.machineFinishedBody", {number: machineID}),
|
64
|
|
- id: machineID,
|
65
|
|
- date: date,
|
|
54
|
+ title: i18n.t('screens.proxiwash.notifications.machineRunningTitle', {
|
|
55
|
+ time: reminder,
|
|
56
|
+ }),
|
|
57
|
+ message: i18n.t('screens.proxiwash.notifications.machineRunningBody', {
|
|
58
|
+ number: machineID,
|
|
59
|
+ }),
|
|
60
|
+ id: id.toString(),
|
|
61
|
+ date: reminderDate,
|
66
|
62
|
});
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ PushNotification.localNotificationSchedule({
|
|
66
|
+ title: i18n.t('screens.proxiwash.notifications.machineFinishedTitle'),
|
|
67
|
+ message: i18n.t('screens.proxiwash.notifications.machineFinishedBody', {
|
|
68
|
+ number: machineID,
|
|
69
|
+ }),
|
|
70
|
+ id: machineID,
|
|
71
|
+ date,
|
|
72
|
+ });
|
67
|
73
|
}
|
68
|
74
|
|
69
|
75
|
/**
|
|
@@ -76,22 +82,26 @@ function createNotifications(machineID: string, date: Date) {
|
76
|
82
|
* @param isEnabled True to enable notifications, false to disable
|
77
|
83
|
* @param endDate The trigger date, or null if disabling notifications
|
78
|
84
|
*/
|
79
|
|
-export async function setupMachineNotification(machineID: string, isEnabled: boolean, endDate: Date | null) {
|
80
|
|
- return new Promise((resolve, reject) => {
|
81
|
|
- if (isEnabled && endDate != null) {
|
82
|
|
- askPermissions()
|
83
|
|
- .then(() => {
|
84
|
|
- createNotifications(machineID, endDate);
|
85
|
|
- resolve();
|
86
|
|
- })
|
87
|
|
- .catch(() => {
|
88
|
|
- reject();
|
89
|
|
- });
|
90
|
|
- } else {
|
91
|
|
- PushNotification.cancelLocalNotifications({id: machineID});
|
92
|
|
- let reminderId = reminderIdFactor * parseInt(machineID);
|
93
|
|
- PushNotification.cancelLocalNotifications({id: reminderId.toString()});
|
94
|
|
- resolve();
|
95
|
|
- }
|
96
|
|
- });
|
|
85
|
+export async function setupMachineNotification(
|
|
86
|
+ machineID: string,
|
|
87
|
+ isEnabled: boolean,
|
|
88
|
+ endDate: Date | null,
|
|
89
|
+): Promise<void> {
|
|
90
|
+ return new Promise((resolve: () => void, reject: () => void) => {
|
|
91
|
+ if (isEnabled && endDate != null) {
|
|
92
|
+ askPermissions()
|
|
93
|
+ .then(() => {
|
|
94
|
+ createNotifications(machineID, endDate);
|
|
95
|
+ resolve();
|
|
96
|
+ })
|
|
97
|
+ .catch(() => {
|
|
98
|
+ reject();
|
|
99
|
+ });
|
|
100
|
+ } else {
|
|
101
|
+ PushNotification.cancelLocalNotifications({id: machineID});
|
|
102
|
+ const reminderId = reminderIdFactor * parseInt(machineID, 10);
|
|
103
|
+ PushNotification.cancelLocalNotifications({id: reminderId.toString()});
|
|
104
|
+ resolve();
|
|
105
|
+ }
|
|
106
|
+ });
|
97
|
107
|
}
|