MCPcopy Index your code
hub / github.com/ajatkj/scriptable / fetchCalendar

Function fetchCalendar

LSWeather.js:937–1055  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

935FUNCTION fetchCalendar
936------------------------------------------------------------------*/
937async function fetchCalendar() {
938 if (!CALENDAR_SHOW_CALENDARS) return unknownCalendarData;
939 let pEventsToday = [];
940 let pEventsTomorrow = [];
941 let wEventsToday = [];
942 let wEventsTomorrow = [];
943 let pEventsTodayCount = 0;
944 let pEventsTodayAllDayCount = 0;
945 let pEventsTomorrowCount = 0;
946 let pEventsTomorrowAllDayCount = 0;
947 let wEventsTodayCount = 0;
948 let wEventsTodayAllDayCount = 0;
949 let wEventsTomorrowCount = 0;
950 let wEventsTomorrowAllDayCount = 0;
951 let calendarColors = {};
952 const maxColors = CALENDAR_COLORS.length;
953 let colorIdx = 0;
954 // Filter out falsy values
955 CALENDAR_PERSONAL_CALENDARS = CALENDAR_PERSONAL_CALENDARS.filter(x => x);
956 CALENDAR_WORK_CALENDARS = CALENDAR_WORK_CALENDARS.filter(x => x);
957
958 // Fetch all personal calendar events for today and tomorrow
959 for (const calendarName of CALENDAR_PERSONAL_CALENDARS) {
960 const calendar = await Calendar.forEventsByTitle(calendarName);
961 const todayEvents = await CalendarEvent.today([calendar]);
962 const tomorrowEvents = await CalendarEvent.tomorrow([calendar]);
963 if (todayEvents.length > 0 || tomorrowEvents.length > 0){
964 calendarColors[calendarName] = CALENDAR_COLORS[colorIdx];
965 colorIdx++;
966 if (colorIdx >= maxColors) colorIdx = 0; // Repeat colors after max
967 }
968 for (const e of todayEvents) pEventsToday.push(e);
969 if (CALENDAR_SHOW_TOMORROW_EVENTS){
970 for (const e of tomorrowEvents) pEventsTomorrow.push(e);
971 }
972 }
973
974 // If personal calendar not set, fetch default calendar for events
975 if (CALENDAR_PERSONAL_CALENDARS.length == 0){
976 const calendar = await Calendar.defaultForEvents();
977 const todayEvents = await CalendarEvent.today([calendar]);
978 const tomorrowEvents = await CalendarEvent.tomorrow([calendar]);
979 if (todayEvents.length > 0 || tomorrowEvents.length > 0){
980 calendarColors[calendar.title] = CALENDAR_COLORS[colorIdx];
981 colorIdx++;
982 if (colorIdx >= maxColors) colorIdx = 0; // Repeat colors after max
983 }
984 for (const e of todayEvents) pEventsToday.push(e);
985 if (CALENDAR_SHOW_TOMORROW_EVENTS){
986 for (const e of tomorrowEvents) pEventsTomorrow.push(e);
987 }
988 }
989
990 // Fetch all work calendar events for today and tomorrow
991 if (CALENDAR_WORK_CALENDARS.length > 0) {
992 for (const calendarName of CALENDAR_WORK_CALENDARS) {
993 const calendar = await Calendar.forEventsByTitle(calendarName);
994 const todayEvents = await CalendarEvent.today([calendar]);

Callers 1

LSWeather.jsFile · 0.70

Calls 1

formatEventsFunction · 0.85

Tested by

no test coverage detected