Displaying events and all-day events.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Ical.Net.DataTypes;
|
||||
using SqlKata;
|
||||
using Wino.Core.Domain;
|
||||
using Wino.Core.Domain.Entities.Calendar;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Messaging.Client.Calendar;
|
||||
@@ -67,5 +70,54 @@ namespace Wino.Services
|
||||
conn.InsertAll(attendees);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<ICalendarItem>> GetCalendarEventsAsync(Guid calendarId, DateTime rangeStart, DateTime rangeEnd)
|
||||
{
|
||||
// TODO: We might need to implement caching here.
|
||||
// I don't know how much of the events we'll have in total, but this logic scans all events every time.
|
||||
|
||||
var accountEvents = await Connection.Table<CalendarItem>().Where(x => x.CalendarId == calendarId).ToListAsync();
|
||||
var result = new List<ICalendarItem>();
|
||||
|
||||
foreach (var ev in accountEvents)
|
||||
{
|
||||
// Parse recurrence rules
|
||||
var calendarEvent = new Ical.Net.CalendarComponents.CalendarEvent
|
||||
{
|
||||
Start = new CalDateTime(ev.StartTime.UtcDateTime),
|
||||
Duration = TimeSpan.FromMinutes(ev.DurationInMinutes),
|
||||
};
|
||||
|
||||
if (string.IsNullOrEmpty(ev.Recurrence))
|
||||
{
|
||||
// No recurrence, only check if we fall into the date range.
|
||||
// All events are saved in UTC, so we need to convert the range to UTC as well.
|
||||
if (ev.StartTime.UtcDateTime < rangeEnd
|
||||
&& ev.StartTime.UtcDateTime.AddMinutes(ev.DurationInMinutes) > rangeStart)
|
||||
{
|
||||
result.Add(ev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var recurrenceLines = Regex.Split(ev.Recurrence, Constants.CalendarEventRecurrenceRuleSeperator);
|
||||
|
||||
foreach (var line in recurrenceLines)
|
||||
{
|
||||
calendarEvent.RecurrenceRules.Add(new RecurrencePattern(line));
|
||||
}
|
||||
|
||||
// Calculate occurrences in the range.
|
||||
var occurrences = calendarEvent.GetOccurrences(rangeStart, rangeEnd);
|
||||
|
||||
foreach (var occurrence in occurrences)
|
||||
{
|
||||
result.Add(ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.70" />
|
||||
<PackageReference Include="Ical.Net" Version="4.3.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
<PackageReference Include="Serilog" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
|
||||
|
||||
Reference in New Issue
Block a user