Guest User

Untitled

a guest
Oct 1st, 2020
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Variables used by Scriptable.
  2. // These must be at the very top of the file. Do not edit.
  3. // icon-color: orange; icon-glyph: calendar-alt;
  4. const debug = true;
  5. const imageName = "image.jpg";
  6. const backgroundColor = "#000000";
  7. const currentDayColor = "#000000";
  8. const textColor = "#ffffff";
  9. const textRed = "#ec534b";
  10.  
  11. if (config.runsInWidget) {
  12.   let widget = await createWidget();
  13.   Script.setWidget(widget);
  14.   Script.complete();
  15.   await widget.presentLarge();
  16. } else if (debug) {
  17.   Script.complete();
  18.   let widget = await createWidget();
  19.   await widget.presentLarge();
  20. } else {
  21.   const appleDate = new Date("2001/01/01");
  22.   const timestamp = (new Date().getTime() - appleDate.getTime()) / 1000;
  23.   console.log(timestamp);
  24.   const callback = new CallbackURL("calshow:" + timestamp);
  25.   callback.open();
  26.   Script.complete();
  27. }
  28.  
  29. async function createWidget() {
  30.   let widget = new ListWidget();
  31.   widget.backgroundColor = new Color(backgroundColor);
  32.   setWidgetBackground(widget, imageName);
  33.   widget.setPadding(16, 16, 16, 16);
  34.   const globalStack = widget.addStack();
  35.   const leftStack = globalStack.addStack();
  36.   const upperStack = globalStack.addStack();
  37.  
  38.   // opacity value for weekends and times
  39.   const opacity = 0.6;
  40.  
  41.   // space between the two halves
  42.   // Espacio enmedio
  43.   globalStack.addSpacer(null);
  44.   leftStack.layoutVertically();
  45.  
  46.   const date = new Date();
  47.   const dateFormatter = new DateFormatter();
  48.   dateFormatter.dateFormat = "EEEE";
  49.  
  50.   // Find future events that aren't all day and aren't canceled
  51.   const events = await CalendarEvent.today([]);
  52.   const futureEvents = [];
  53.   for (const event of events) {
  54.     if (
  55.       event.startDate.getTime() > date.getTime() &&
  56.       !event.isAllDay &&
  57.       !event.title.startsWith("Canceled:")
  58.     ) {
  59.       futureEvents.push(event);
  60.     }
  61.   }
  62.  
  63.   // center the whole left part of the widget
  64.   // Centrar izq
  65.   leftStack.addSpacer(185);
  66.  
  67.   //leftStack.layoutVertically();
  68.  
  69.   // if we have events today; else if we don't
  70.   if (futureEvents.length !== 0) {
  71.     // show the next 3 events at most
  72.     const numEvents = futureEvents.length > 3 ? 3 : futureEvents.length;
  73.     for (let i = 0; i < numEvents; i += 1) {
  74.       formatEvent(leftStack, futureEvents[i], textColor, opacity);
  75.       // don't add a spacer after the last event
  76.       if (i < numEvents - 1) {
  77.         leftStack.addSpacer(8);
  78.       }
  79.     }
  80.   } else {
  81.     addWidgetTextLine(leftStack, "No more events today", {
  82.       color: textColor,
  83.       opacity,
  84.       font: Font.regularSystemFont(15),
  85.       align: "left",
  86.     });
  87.   }
  88.   // for centering
  89.   // Espacio Arriba
  90.   //leftStack.addSpacer(130);
  91.  
  92.   // right half
  93.   const rightStack = globalStack.addStack();
  94.   rightStack.addSpacer(165);
  95.   rightStack.layoutVertically();
  96.  
  97.   dateFormatter.dateFormat = "MMMM";
  98.  
  99.   // Current month line
  100.   const monthLine = rightStack.addStack();
  101.   monthLine.addSpacer(4);
  102.   addWidgetTextLine(monthLine, dateFormatter.string(date).toUpperCase(), {
  103.     color: textRed,
  104.     textSize: 12,
  105.     font: Font.boldSystemFont(12),
  106.   });
  107.  
  108.   // between the month name and the week calendar
  109.   rightStack.addSpacer(2);
  110.  
  111.   const calendarStack = rightStack.addStack();
  112.   calendarStack.spacing = 2;
  113.  
  114.   const month = buildMonthVertical();
  115.  
  116.   for (let i = 0; i < month.length; i += 1) {
  117.     let weekdayStack = calendarStack.addStack();
  118.     weekdayStack.layoutVertically();
  119.  
  120.     for (let j = 0; j < month[i].length; j += 1) {
  121.       let dayStack = weekdayStack.addStack();
  122.       dayStack.size = new Size(20, 20);
  123.       dayStack.centerAlignContent();
  124.  
  125.       if (month[i][j] === date.getDate().toString()) {
  126.         const highlightedDate = getHighlightedDate(
  127.           date.getDate().toString(),
  128.           currentDayColor
  129.         );
  130.         dayStack.addImage(highlightedDate);
  131.       } else {
  132.         addWidgetTextLine(dayStack, `${month[i][j]}`, {
  133.           color: textColor,
  134.           opacity: i > 4 ? opacity : 1,
  135.           font: Font.boldSystemFont(10),
  136.           align: "center",
  137.         });
  138.       }
  139.     }
  140.   }
  141.  
  142.   return widget;
  143. }
  144.  
  145. /**
  146.  * Creates an array of arrays, where the inner arrays include the same weekdays
  147.  * along with an identifier in 0 position
  148.  * [
  149.  *   [ 'M', ' ', '7', '14', '21', '28' ],
  150.  *   [ 'T', '1', '8', '15', '22', '29' ],
  151.  *   [ 'W', '2', '9', '16', '23', '30' ],
  152.  *   ...
  153.  * ]
  154.  *
  155.  * @returns {Array<Array<string>>}
  156.  */
  157. function buildMonthVertical() {
  158.   const date = new Date();
  159.   const firstDayStack = new Date(date.getFullYear(), date.getMonth(), 1);
  160.   const lastDayStack = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  161.  
  162.   const month = [["M"], ["T"], ["W"], ["T"], ["F"], ["S"], ["S"]];
  163.  
  164.   let dayStackCounter = 0;
  165.  
  166.   for (let i = 1; i < firstDayStack.getDay(); i += 1) {
  167.     month[i - 1].push(" ");
  168.     dayStackCounter = (dayStackCounter + 1) % 7;
  169.   }
  170.  
  171.   for (let date = 1; date <= lastDayStack.getDate(); date += 1) {
  172.     month[dayStackCounter].push(`${date}`);
  173.     dayStackCounter = (dayStackCounter + 1) % 7;
  174.   }
  175.  
  176.   const length = month.reduce(
  177.     (acc, dayStacks) => (dayStacks.length > acc ? dayStacks.length : acc),
  178.     0
  179.   );
  180.   month.forEach((dayStacks, index) => {
  181.     while (dayStacks.length < length) {
  182.       month[index].push(" ");
  183.     }
  184.   });
  185.  
  186.   return month;
  187. }
  188.  
  189. /**
  190.  * Draws a circle with a date on it for highlighting in calendar view
  191.  *
  192.  * @param  {string} date to draw into the circle
  193.  *
  194.  * @returns {Image} a circle with the date
  195.  */
  196. function getHighlightedDate(date) {
  197.   const drawing = new DrawContext();
  198.   drawing.respectScreenScale = true;
  199.   const size = 50;
  200.   drawing.size = new Size(size, size);
  201.   drawing.opaque = false;
  202.   drawing.setFillColor(new Color(textRed));
  203.   drawing.fillEllipse(new Rect(1, 1, size - 2, size - 2));
  204.   drawing.setFont(Font.boldSystemFont(25));
  205.   drawing.setTextAlignedCenter();
  206.   drawing.setTextColor(new Color("#ffffff"));
  207.   drawing.drawTextInRect(date, new Rect(0, 10, size, size));
  208.   const currentDayImg = drawing.getImage();
  209.   return currentDayImg;
  210. }
  211.  
  212. /**
  213.  * formats the event times into just hours
  214.  *
  215.  * @param  {Date} date
  216.  *
  217.  * @returns {string} time
  218.  */
  219. function formatTime(date) {
  220.   let dateFormatter = new DateFormatter();
  221.   dateFormatter.useNoDateStyle();
  222.   dateFormatter.useShortTimeStyle();
  223.   return dateFormatter.string(date);
  224. }
  225.  
  226. /**
  227.  * Adds a event name along with start and end times to widget stack
  228.  *
  229.  * @param  {WidgetStack} stack - onto which the event is added
  230.  * @param  {CalendarEvent} event - an event to add on the stack
  231.  * @param  {number} opacity - text opacity
  232.  */
  233.  
  234.  
  235.  
  236.  
  237. function formatEvent(stack, event, color, opacity) {
  238.     //let prefix = `▐`,
  239.     //first.textColor = getItemColor(event.calendarTitle),
  240.    
  241.   const CALENDAR_COLORS = {
  242.      "Algebra": Color.blue(),
  243.      "Cal 2": Color.green(),
  244.      "Cal 3": new Color("#3f51b5"),
  245.   }
  246.  
  247.   //function getItemColor(event) {
  248.     console.log("=-=-=-=-=-=-=-=")
  249.     /*
  250.     let prefix = stack.addText(`▐ `)
  251.     prefix.textColor = `#${event.calendar.color.hex}`
  252.    
  253.     let eventTitle = stack.addText(event.title)
  254.     console.log(text)
  255.     console.log("=-=-=-=-=-=-=-=")
  256.    //  return CALENDAR_COLORS[event.calendarTitle]
  257.   //}
  258.   eventC = new Color(`#${event.calendar.color.hex}`)
  259.   addWidgetTextLine(stack, `▐`, {
  260.     color: eventC,
  261.     font: Font.mediumSystemFont(12),
  262.     //lineLimit: 2,
  263.   });*/
  264.  
  265. addWidgetTextLine(stack, `${event.title}`, {
  266.     color,
  267.     font: Font.mediumSystemFont(12),
  268.     lineLimit: 1,
  269.   });
  270.   //const prefix1 = `▐`,
  271.  
  272.   // create line for event start and end times
  273.   let timeStack = stack.addStack();
  274.   const start = `${formatTime(event.startDate)}`;
  275.   const end = `${formatTime(event.endDate)}`;
  276.  
  277.   if(start!=end){
  278.   const time = `┃${formatTime(event.startDate)} - ${formatTime(event.endDate)}`;
  279.   addWidgetTextLine(timeStack, time, {
  280.     color: (`#${event.calendar.color.hex}`),
  281.     opacity,
  282.     font: Font.regularSystemFont(12),
  283.   });} else {
  284.     const time = `┃${formatTime(event.startDate)}`;
  285.   addWidgetTextLine(timeStack, time, {
  286.     color: (`#${event.calendar.color.hex}`),
  287.     opacity,
  288.     font: Font.regularSystemFont(12),
  289.   });
  290. }
  291. }
  292.  
  293. function addWidgetTextLine(
  294.   widget,
  295.   text,
  296.   {
  297.     color = "#ffffff",
  298.     textSize = 12,
  299.     opacity = 1,
  300.     align,
  301.     font = "",
  302.     lineLimit = 0,
  303.   }
  304. ) {
  305.   let textLine = widget.addText(text);
  306.   textLine.textColor = new Color(color);
  307.   if (typeof font === "string") {
  308.     textLine.font = new Font(font, textSize);
  309.   } else {
  310.     textLine.font = font;
  311.   }
  312.   console.log(`${text}`);
  313.   console.log(`${typeof opacity}`);
  314.   textLine.textOpacity = opacity;
  315.   switch (align) {
  316.     case "left":
  317.       textLine.leftAlignText();
  318.       break;
  319.     case "center":
  320.       textLine.centerAlignText();
  321.       break;
  322.     case "right":
  323.       textLine.rightAlignText();
  324.       break;
  325.     default:
  326.       textLine.leftAlignText();
  327.       break;
  328.   }
  329. }
  330.  
  331. function getImageUrl(name) {
  332.   let fm = FileManager.iCloud();
  333.   let dir = fm.documentsDirectory();
  334.   return fm.joinPath(dir, `${name}`);
  335. }
  336.  
  337. function setWidgetBackground(widget, imageName) {
  338.   const imageUrl = getImageUrl(imageName);
  339.   console.log(imageUrl);
  340.   widget.backgroundImage = Image.fromFile(imageUrl);
  341. }
Advertisement
Add Comment
Please, Sign In to add comment