-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNew Reminder.js
50 lines (45 loc) · 1.2 KB
/
New Reminder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// always-run-in-app: true; icon-color: deep-green;
// icon-glyph: magic;
// New Reminder
const reminder = new Reminder();
const titlePrompt = new Alert();
titlePrompt.title = 'Title';
titlePrompt.addTextField();
titlePrompt.addCancelAction('Cancel');
titlePrompt.addAction('OK');
if (-1 === (await titlePrompt.present())) {
return;
}
if (!titlePrompt.textFieldValue(0)) {
await presentError('Reminder title must be provided');
return;
}
reminder.title = titlePrompt.textFieldValue(0);
const datePicker = new DatePicker();
let dueDate;
try {
dueDate = await datePicker.pickDateAndTime();
} catch (ex) {
if (ex.message.includes('Date picker was cancelled')) {
return;
} else {
throw ex;
}
}
// Set due date rounded to the closest 5 minutes
const coeff = 1000 * 60 * 5;
reminder.dueDate = new Date(Math.round(dueDate / coeff) * coeff);
reminder.save();
/**
* Presents an alert with the supplied error message.
* @param {string} message error message
*/
async function presentError(message) {
const alert = new Alert();
alert.title = 'Error';
alert.message = message;
alert.addAction('OK');
await alert.present();
}