forked from yaylinda/scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiDayCalendarWidget.js
More file actions
564 lines (469 loc) · 16.2 KB
/
MultiDayCalendarWidget.js
File metadata and controls
564 lines (469 loc) · 16.2 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*========================================================
* CONSTANTS
*======================================================*/
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
Date.prototype.addHours = function (numHours) {
const date = new Date(this.valueOf());
date.setHours(date.getHours() + numHours);
return date;
};
Date.prototype.startOfDay = function () {
const date = new Date(this.valueOf());
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date;
};
Date.prototype.endOfDay = function () {
const date = new Date(this.valueOf());
date.setHours(23);
date.setMinutes(59);
date.setSeconds(59);
return date;
};
Date.prototype.addMinutes = function (numMinutes) {
const date = new Date(this.valueOf());
date.setMinutes(date.getMinutes() + numMinutes);
return date;
};
/*========================================================
* SCRIPT CONFIGURATIONS *** UPDATE ME!!! ***
*======================================================*/
// Example: 12 PM
const HOUR_FORMAT = new Intl.DateTimeFormat(
'en-US', {
hour: 'numeric',
});
// Example: Sat
const DAY_OF_WEEK_FORMAT = new Intl.DateTimeFormat(
'en-US', {
weekday: 'short',
});
// Example: 12
const DAY_OF_MONTH_FORMAT = new Intl.DateTimeFormat(
'en-US', {
day: 'numeric',
});
/**
* Widget confugurations.
* Edit these to customize the widget.
*/
const WIDGET_CONFIGURATIONS = {
// Number of hours to show in the widget.
// It will show a moving window of this many hours,
// across the specified number of days (numDays)
numHours: 8,
// Use this to set the starting hour (between 0-23),
// of the window of time to show in the calendar.
// This is used with "numHours" to determine which hours
// of each day to show in the widget.
// If this is set to -1, it will use the current hour as
// the starting time.
startAt: -1,
// Number of days to show in the widget
numDays: 5,
// Calendars to show events from.
// An empty array means all calendars.
// Calendar names can be found in the "Calendar" App.
// The name must be an exact string match.
calendars: [],
// Calendar callback app
// When clicking on the widget,
// which calendar app should open?
// Must be one of the supported apps:
// - calshow - Default iOS Calendar
// - googlecalendar - Google Calendar
// - x-fantastical3 - Fantastical
callbackCalendarApp: 'calshow',
// Whether or not the widget will use a backend image.
useBackgroundImage: true,
// If no background image,
// default to a grayish background color gradient.
backgroundColor: [
new Color("#29323c"),
new Color("#1c1c1c")
],
// Font to use in Widget
font: 'Menlo',
// Bold Font to use in Widget
fontBold: 'Menlo-Bold',
// Text size of the day of week label
dayOfWeekTextSize: 10,
// Text size of the event text label
eventTextSize: 10,
// Default text color in Widget
defaultTextColor: Color.white(),
// Height of Widget's Draw Context
widgetHeight: 400,
// Width of Widget's Draw Context
widgetWidth: 400,
// Vertical spacing between the day event columns
dayStackSpacing: 0,
// Left padding of events in Draw Context
eventsLeftPadding: 10,
// Padding of the events text within the event rectangle
eventsTextPadding: 5,
// Corner radius of event rectangle in Draw Context
eventsRounding: 5,
// Text color of event titles
eventsTextColor: Color.black(),
// Thickness of hour/now line
lineHeight: 2,
// Color of hour/half-hour line
lineColor: new Color('#ffffff', 0.3),
// Width of the hours label column stack
hoursLabelStackWidth: 30,
// Vertical spacing, used between the day-of-week label
// and all-day events.
verticalSpacing: 2,
};
/*========================================================
* WIDGET SET UP / PRESENTATION
*======================================================*/
const widget = new ListWidget();
await setBackground(widget, WIDGET_CONFIGURATIONS);
const events = await getEvents(WIDGET_CONFIGURATIONS);
drawWidget(widget, events, WIDGET_CONFIGURATIONS);
if (config.runsInWidget) {
Script.setWidget(widget);
Script.complete();
} else if (args.widgetParameter === 'callback') {
Script.setWidget(widget);
const timestamp = (
new Date().getTime() - new Date('2001/01/01').getTime()
) / 1000;
const callback = new CallbackURL(`${WIDGET_CONFIGURATIONS.callbackCalendarApp}:${timestamp}`);
callback.open();
Script.complete();
} else {
Script.setWidget(widget);
widget.presentLarge();
Script.complete();
}
/*========================================================
* DRAW WIDGET
*======================================================*/
/**
* Main draw widget function.
*/
function drawWidget(widget, events, WIDGET_CONFIGURATIONS) {
const mainStack = widget.addStack();
mainStack.layoutHorizontally();
mainStack.spacing = WIDGET_CONFIGURATIONS.dayStackSpacing;
// Calculate the maximum number of all-day events of the days to draw
const numAllDayEvents = Object.values(events).map(e => {
if (e['all-day']) {
return e['all-day'].length;
} else {
return 0;
}
});
const maxNumAllDayEvents = Math.max(...numAllDayEvents);
// Determine which hour of the day to start displaying events in the widget
const startingDateAtHour = new Date();
if (WIDGET_CONFIGURATIONS.startAt > -1) {
startingDateAtHour.setHours(WIDGET_CONFIGURATIONS.startAt);
}
// Create an array of hour labels for the widget
const hourLabels = [];
for (let i = 0; i < WIDGET_CONFIGURATIONS.numHours; i++) {
const dateAtHour = startingDateAtHour.addHours(i);
hourLabels.push(HOUR_FORMAT.format(dateAtHour));
}
// Draw the hour label column
const hoursLabelStack = mainStack.addStack();
hoursLabelStack.layoutVertically();
drawHoursLabelStack(hoursLabelStack, maxNumAllDayEvents, hourLabels, WIDGET_CONFIGURATIONS);
// Draw one column for each day's events
Object.keys(events).forEach(day => {
const dayEventsStack = mainStack.addStack();
dayEventsStack.layoutVertically();
const dayEvents = events[day];
drawDayEventsStack(dayEventsStack, day, dayEvents, maxNumAllDayEvents, hourLabels, WIDGET_CONFIGURATIONS);
});
}
/**
* Function to draw events of one day, as a vertical column stack.
*/
function drawDayEventsStack(stack, day, events, maxNumAllDayEvents, hourLabels, {
defaultTextColor,
font,
fontBold,
dayOfWeekTextSize,
eventsTextColor,
eventTextSize,
numDays,
numHours,
widgetHeight,
widgetWidth,
eventsLeftPadding,
eventsTextPadding,
eventsRounding,
lineHeight,
lineColor,
hoursLabelStackWidth,
verticalSpacing
}) {
const halfHourEventHeight = (widgetHeight - verticalSpacing - maxNumAllDayEvents) / (numHours * 2);
const allDayEventHeight = halfHourEventHeight / 2;
const stackWidth = (widgetWidth - hoursLabelStackWidth) / numDays;
const draw = new DrawContext();
draw.opaque = false;
draw.respectScreenScale = true;
draw.size = new Size(stackWidth, widgetHeight);
// Day day of week label
draw.setTextColor(defaultTextColor);
draw.setFont(new Font(fontBold, dayOfWeekTextSize));
draw.drawTextInRect(
`${DAY_OF_WEEK_FORMAT.format(new Date(day))} ${DAY_OF_MONTH_FORMAT.format(new Date(day))}`,
new Rect(
eventsLeftPadding,
0,
stackWidth,
allDayEventHeight + verticalSpacing,
)
);
// Draw all-day events at the top of the day events stack
if (events['all-day']) {
let numAllDayEvents = 1;
for (let i = 0; i < events['all-day'].length; i++) {
const { title, color } = events['all-day'][i];
const eventRectY = numAllDayEvents * allDayEventHeight + i;
const eventRect = new Rect(
eventsLeftPadding,
eventRectY,
(stackWidth - eventsLeftPadding),
allDayEventHeight
);
const eventPath = new Path();
eventPath.addRoundedRect(eventRect, eventsRounding, eventsRounding);
draw.addPath(eventPath);
draw.setFillColor(new Color(color));
draw.fillPath();
// Draw event name text
draw.setTextColor(eventsTextColor);
draw.setFont(new Font(font, eventTextSize));
draw.drawTextInRect(
`${title}`,
new Rect(
eventsLeftPadding + eventsTextPadding,
eventRectY,
(stackWidth - eventsLeftPadding) - eventsTextPadding,
allDayEventHeight
)
);
numAllDayEvents += 1;
}
}
// Y Offset based on number of all-day events
const offsetY = (maxNumAllDayEvents + 1) * allDayEventHeight + verticalSpacing + maxNumAllDayEvents;
// Loop through all the hours and draw the lines
for (let i = 0; i < hourLabels.length; i++) {
const topPointY = offsetY + halfHourEventHeight * 2 * i;
const midPointY = topPointY + halfHourEventHeight;
// Draw line at the hour
const topPath = new Path();
topPath.addRect(new Rect(eventsLeftPadding, topPointY, stackWidth - eventsLeftPadding, lineHeight));
draw.addPath(topPath);
draw.setFillColor(lineColor);
draw.fillPath();
// Draw line at the half hour
const middlePath = new Path();
middlePath.addRect(new Rect(eventsLeftPadding, midPointY, stackWidth - eventsLeftPadding, lineHeight / 2));
draw.addPath(middlePath);
draw.setFillColor(lineColor);
draw.fillPath();
}
// Loop through all the hours and draw the events (on top of the lines)
for (let i = 0; i < hourLabels.length; i++) {
const currentHourText = hourLabels[i];
const topPointY = offsetY + halfHourEventHeight * 2 * i;
// Draw events for this hour (if any)
const hourEvents = events[currentHourText];
if (hourEvents) {
for (let j = 0; j < hourEvents.length; j++) {
// TODO - determine width of events based on num events in hour
const { startMinute, title, color, duration } = hourEvents[j];
// Determine top Y of event
const eventRectY = topPointY + Math.floor((startMinute * halfHourEventHeight) / 30);
// Determine height of events
const eventHeight = Math.floor((duration * halfHourEventHeight) / 30);
const eventRect = new Rect(
eventsLeftPadding,
eventRectY,
stackWidth - eventsLeftPadding,
eventHeight
);
const eventPath = new Path();
eventPath.addRoundedRect(eventRect, eventsRounding, eventsRounding);
draw.addPath(eventPath);
draw.setFillColor(new Color(color));
draw.fillPath();
// Draw event name text
draw.setTextColor(eventsTextColor);
draw.setFont(new Font(font, eventTextSize));
draw.drawTextInRect(
`${title}`,
new Rect(
eventsLeftPadding + eventsTextPadding,
eventRectY,
(stackWidth - eventsLeftPadding) - eventsTextPadding,
eventHeight
)
);
}
}
}
// Draw line at the current time (if the current time is within the time window)
const currentDateTime = new Date();
const currentHourLabel = HOUR_FORMAT.format(currentDateTime);
if (hourLabels.includes(currentHourLabel)) {
const currentMinute = currentDateTime.getMinutes();
let currentMinuteY = offsetY + (currentMinute * halfHourEventHeight) / 30;
const currentMinutePath = new Path();
currentMinutePath.addRect(new Rect(eventsLeftPadding, currentMinuteY, stackWidth, lineHeight));
draw.addPath(currentMinutePath);
draw.setFillColor(Color.red());
draw.fillPath();
}
// Put the content on the widget stack
const drawn = draw.getImage();
stack.addImage(drawn);
}
/**
* Function to draw the left-most vertical column stack of the hours labels.
*/
function drawHoursLabelStack(stack, maxNumAllDayEvents, hourLabels, {
defaultTextColor,
font,
eventTextSize,
numHours,
widgetHeight,
lineHeight,
hoursLabelStackWidth,
verticalSpacing,
}) {
// Set up the Draw Context for the hours label stack
const draw = new DrawContext();
draw.opaque = false;
draw.respectScreenScale = true;
draw.size = new Size(hoursLabelStackWidth, widgetHeight);
const halfHourEventHeight = (widgetHeight - verticalSpacing - maxNumAllDayEvents) / (numHours * 2);
const allDayEventHeight = halfHourEventHeight / 2;
// Y Offset based on number of all-day events
const offsetY = (maxNumAllDayEvents + 1) * allDayEventHeight + verticalSpacing + maxNumAllDayEvents;
// Loop through all the hours and draw the lines
for (let i = 0; i < hourLabels.length; i++) {
const currentHourText = hourLabels[i];
const topPointY = offsetY + halfHourEventHeight * 2 * i - (i * lineHeight);
// Draw hour text
draw.setTextColor(defaultTextColor);
draw.setFont(new Font(font, eventTextSize));
draw.drawText(`${currentHourText}`, new Point(0, topPointY));
}
// Put the content on the widget stack
const drawn = draw.getImage();
stack.addImage(drawn);
}
/*========================================================
* FUNCTIONS
*======================================================*/
/**
* Fetch calendar events for the given number of days (from today), and from the given calendars.
*
* Returns an array, where each element is an object in the format: { hour: [events] }
*/
async function getEvents({ numDays, calendars }) {
const events = {};
const now = new Date();
for (let i = 0; i < numDays; i++) {
const day = now.addDays(i);
const startOfDay = day.startOfDay();
const endOfDay = day.endOfDay();
const dayEvents = await CalendarEvent.between(startOfDay, endOfDay);
const eventsByHour = {};
for (let i = 0; i < dayEvents.length; i++) {
const e = dayEvents[i];
if (calendars.length > 0 && !calendars.includes(e.calendar.title)) {
// If user has specified a list of calendars,
// filter out the events that are NOT from the given calendar names.
// Otherwise, include the event.
continue;
}
const start = new Date(e.startDate);
const end = new Date(e.endDate);
if (e.isAllDay) { // All day events
if (!eventsByHour['all-day']) {
eventsByHour['all-day'] = [];
}
eventsByHour['all-day'].push({
title: e.title,
color: `#${e.calendar.color.hex}`,
});
} else { // Other events
const hourKey = HOUR_FORMAT.format(start);
if (!eventsByHour[hourKey]) {
eventsByHour[hourKey] = [];
}
let eventDuration = Math.floor(((end - start) / 1000) / 60);
const eventObj = {
start,
end,
startMinute: start.getMinutes(),
title: e.title,
color: `#${e.calendar.color.hex}`,
duration: eventDuration,
};
eventsByHour[hourKey].push(eventObj);
}
}
events[startOfDay] = eventsByHour;
}
return events;
}
/**
* Set the background of the widget.
*
* If useBackgroundImage is false,
* defaults the background to the given backgroundColor.
*
* Update WIDGET_CONFIGURATIONS to configure.
*/
async function setBackground(widget, {
useBackgroundImage, backgroundColor
}) {
if (useBackgroundImage) {
// Determine if our image exists and when it was saved.
const files = FileManager.local();
const path = files.joinPath(
files.documentsDirectory(),
'multi-day-calendar-widget-background'
);
const exists = files.fileExists(path);
// If it exists and we're running in the widget, use photo from cache
// Or we're invoking the script to run FROM the widget with a widgetParameter
if (exists && config.runsInWidget || args.widgetParameter === 'callback') {
widget.backgroundImage = files.readImage(path);
// If it's missing when running in the widget, fallback to backgroundColor
} else if (!exists && config.runsInWidget) {
const bgColor = new LinearGradient();
bgColor.colors = backgroundColor;
bgColor.locations = [0.0, 1.0];
widget.backgroundGradient = bgColor;
// But if we're running in app, prompt the user for the image.
} else if (config.runsInApp) {
const img = await Photos.fromLibrary();
widget.backgroundImage = img;
files.writeImage(path, img);
}
} else {
const bgColor = new LinearGradient();
bgColor.colors = backgroundColor;
bgColor.locations = [0.0, 1.0];
widget.backgroundGradient = bgColor;
}
}