Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Todays and Tomorrows events sorted by start time #53

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ The IcalCalendar gives Homey flow cards to trigger on your calendar events
- Added more choices (timespan) for triggers and conditions
- Added possibility to remove calendars from settings
- Added global flow tokens for tomorrows events -> [Issue #36](https://github.com/runely/calendar-homey/issues/36)
- Todays events and Tomorrows events are now sorted by start time -> [Issue #51](https://github.com/runely/calendar-homey/issues/51)
- 0.1.0
- Added support for Mailfence (DTSTART;VALUE=DATE-TIME / DTEND;VALUE=DATE-TIME)
- Added support for recurring events :D
Expand Down
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Homey = require('homey');

const getContent = require('./lib/get-ical-content');
const getActiveEvents = require('./lib/get-active-events');
const sortEvents = require('./lib/sort-events');
const sortCalendarsEvents = require('./lib/sort-calendars');
const variableMgmt = require('./lib/variableMgmt');

const triggersHandler = require('./handlers/triggers');
Expand Down Expand Up @@ -103,7 +103,7 @@ class IcalCalendar extends Homey.App {
}

variableMgmt.calendars = calendarsEvents;
sortEvents(variableMgmt.calendars);
sortCalendarsEvents(variableMgmt.calendars);

return true;
}
Expand Down
88 changes: 38 additions & 50 deletions handlers/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ const startTrigger = (calendarName, event, app, state) => {
}

const updateFlowTokens = (nextEvent, app) => {
let calendarsToday = getTodaysEvents(app.variableMgmt.calendars);
let calendarsTomorrow = getTomorrowsEvents(app.variableMgmt.calendars);
let eventsToday = getTodaysEvents(app.variableMgmt.calendars);
let eventsTomorrow = getTomorrowsEvents(app.variableMgmt.calendars);
let eventDuration;

if (nextEvent.event) {
Expand Down Expand Up @@ -163,69 +163,57 @@ const updateFlowTokens = (nextEvent, app) => {
}
else if (token.id === 'events_today_title_stamps') {
let value = '';
calendarsToday.map(calendar => {
calendar.events.map(event => {
if (event.datetype === 'date-time') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_starts')} ${event.start.format(Homey.__('flowTokens.events_today-tomorrow_startstamp_time_format'))}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_stops')} ${event.end.format(Homey.__('flowTokens.events_today-tomorrow_stopstamp_time_format'))}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_today_title_stamps_pre')}\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
eventsToday.map(event => {
if (event.datetype === 'date-time') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_starts')} ${event.start.format(Homey.__('flowTokens.events_today-tomorrow_startstamp_time_format'))}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_stops')} ${event.end.format(Homey.__('flowTokens.events_today-tomorrow_stopstamp_time_format'))}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_today_title_stamps_pre')}\n${eventValue}`;
}
else if (event.datetype === 'date') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_startstamp_fullday')}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_today_title_stamps_pre')}\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
});
}
else if (event.datetype === 'date') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_startstamp_fullday')}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_today_title_stamps_pre')}\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
}
});
token.setValue(value);
}
else if (token.id === 'events_today_count') {
let todaysEventCount = 0;
calendarsToday.map(calendar => {
todaysEventCount += calendar.events.length;
});
token.setValue(todaysEventCount);
token.setValue(eventsToday.length);
}
else if (token.id === 'events_tomorrow_title_stamps') {
let value = '';
calendarsTomorrow.map(calendar => {
calendar.events.map(event => {
if (event.datetype === 'date-time') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_starts')} ${event.start.format(Homey.__('flowTokens.events_today-tomorrow_startstamp_time_format'))}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_stops')} ${event.end.format(Homey.__('flowTokens.events_today-tomorrow_stopstamp_time_format'))}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_tomorrow_title_stamps_pre')}\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
eventsTomorrow.map(event => {
if (event.datetype === 'date-time') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_starts')} ${event.start.format(Homey.__('flowTokens.events_today-tomorrow_startstamp_time_format'))}, ${Homey.__('flowTokens.events_today-tomorrow_title_stamps_stops')} ${event.end.format(Homey.__('flowTokens.events_today-tomorrow_stopstamp_time_format'))}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_tomorrow_title_stamps_pre')}\n${eventValue}`;
}
else if (event.datetype === 'date') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_startstamp_fullday')}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_tomorrow_title_stamps_pre')}\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
});
}
else if (event.datetype === 'date') {
let eventValue = `${event.summary}, ${Homey.__('flowTokens.events_today-tomorrow_startstamp_fullday')}`;
if (value === '') {
value = `${Homey.__('flowTokens.events_tomorrow_title_stamps_pre')}\n${eventValue}`;
}
else {
value += `.\n${eventValue}`;
}
}
});
token.setValue(value);
}
else if (token.id === 'events_tomorrow_count') {
let tomorrowsEventCount = 0;
calendarsTomorrow.map(calendar => {
tomorrowsEventCount += calendar.events.length;
});
token.setValue(tomorrowsEventCount);
token.setValue(eventsTomorrow.length);
}
});
}
Expand Down
12 changes: 5 additions & 7 deletions lib/get-todays-events.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';

const moment = require('moment');
const sortEvents = require('./sort-events');

module.exports = (calendars) => {
let eventsToday = [];
let now = moment();
let calendarsToday = [];

calendars.map(calendar => {
let calendarEvents = [];

calendar.events.map(event => {
let startDiff = now.diff(event.start);
let stopDiff = now.diff(event.end);
Expand All @@ -18,12 +17,11 @@ module.exports = (calendars) => {
let todayAlreadyStarted = (startDiff > 0 && startIsSameDay && stopDiff < 0);
let startPastButNotStopped = (startDiff > 0 && !startIsSameDay && stopDiff < 0);
if (todayNotStartedYet || todayAlreadyStarted || startPastButNotStopped) {
calendarEvents.push(event);
eventsToday.push({ ...event, calendarname: calendar.name });
}
});

calendarsToday.push({ name: calendar.name, events: calendarEvents });
});

return calendarsToday;
sortEvents(eventsToday);
return eventsToday;
}
12 changes: 5 additions & 7 deletions lib/get-tomorrows-events.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';

const moment = require('moment');
const sortEvents = require('./sort-events');

module.exports = (calendars) => {
let eventsTomorrow = [];
let tomorrowStart = moment().add(1, 'day').startOf('day');
let calendarsTomorrow = [];

calendars.map(calendar => {
let calendarEvents = [];

calendar.events.map(event => {
let startDiff = tomorrowStart.diff(event.start);
let endDiff = tomorrowStart.diff(event.end);
Expand All @@ -17,12 +16,11 @@ module.exports = (calendars) => {
let tomorrowNotStartedYet = (startDiff < 0 && startIsSameDay);
let startPastButNotStopped = (startDiff > 0 && !startIsSameDay && endDiff < 0);
if (tomorrowNotStartedYet || startPastButNotStopped) {
calendarEvents.push(event);
eventsTomorrow.push({ ...event, calendarname: calendar.name });
}
});

calendarsTomorrow.push({ name: calendar.name, events: calendarEvents })
});

return calendarsTomorrow;
sortEvents(eventsTomorrow);
return eventsTomorrow;
}
9 changes: 9 additions & 0 deletions lib/sort-calendars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const sortEvents = require('./sort-events');

module.exports = (calendars) => {
return calendars.map(calendar => {
return { ...calendar, events: sortEvents(calendar.events) };
});
}
7 changes: 2 additions & 5 deletions lib/sort-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

const sortEvent = require('./sort-event');

module.exports = (calendars) => {
return calendars.map(calendar => {
let sortedEvents = calendar.events.sort((a, b) => sortEvent(a, b));
return { ...calendar, events: sortedEvents };
});
module.exports = (events) => {
return events.sort((a, b) => sortEvent(a, b));
}