2024-12-27 00:18:46 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2024-12-28 23:17:16 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
2024-12-30 23:10:51 +01:00
|
|
|
|
using Ical.Net.CalendarComponents;
|
2024-12-28 23:17:16 +01:00
|
|
|
|
using Ical.Net.DataTypes;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using SqlKata;
|
2024-12-28 23:17:16 +01:00
|
|
|
|
using Wino.Core.Domain;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using Wino.Core.Domain.Entities.Calendar;
|
|
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
2024-12-30 23:10:51 +01:00
|
|
|
|
using Wino.Core.Domain.Models.Calendar;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using Wino.Messaging.Client.Calendar;
|
|
|
|
|
|
using Wino.Services.Extensions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Wino.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class CalendarService : BaseDatabaseService, ICalendarService
|
|
|
|
|
|
{
|
|
|
|
|
|
public CalendarService(IDatabaseService databaseService) : base(databaseService)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<List<AccountCalendar>> GetAccountCalendarsAsync(Guid accountId)
|
|
|
|
|
|
=> Connection.Table<AccountCalendar>().Where(x => x.AccountId == accountId).ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
public async Task InsertAccountCalendarAsync(AccountCalendar accountCalendar)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Connection.InsertAsync(accountCalendar);
|
|
|
|
|
|
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new CalendarListAdded(accountCalendar));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateAccountCalendarAsync(AccountCalendar accountCalendar)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Connection.UpdateAsync(accountCalendar);
|
|
|
|
|
|
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new CalendarListUpdated(accountCalendar));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteAccountCalendarAsync(AccountCalendar accountCalendar)
|
|
|
|
|
|
{
|
|
|
|
|
|
var deleteCalendarItemsQuery = new Query()
|
|
|
|
|
|
.From(nameof(CalendarItem))
|
|
|
|
|
|
.Where(nameof(CalendarItem.CalendarId), accountCalendar.Id)
|
|
|
|
|
|
.Where(nameof(AccountCalendar.AccountId), accountCalendar.AccountId);
|
|
|
|
|
|
|
|
|
|
|
|
var rawQuery = deleteCalendarItemsQuery.GetRawQuery();
|
|
|
|
|
|
|
|
|
|
|
|
await Connection.ExecuteAsync(rawQuery);
|
|
|
|
|
|
await Connection.DeleteAsync(accountCalendar);
|
|
|
|
|
|
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new CalendarListDeleted(accountCalendar));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteCalendarItemAsync(Guid calendarItemId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var calendarItem = await Connection.GetAsync<CalendarItem>(calendarItemId);
|
|
|
|
|
|
|
|
|
|
|
|
if (calendarItem == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
await Connection.Table<CalendarItem>().DeleteAsync(x => x.Id == calendarItemId);
|
|
|
|
|
|
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send(new CalendarItemDeleted(calendarItem));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task CreateNewCalendarItemAsync(CalendarItem calendarItem, List<CalendarEventAttendee> attendees)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Connection.RunInTransactionAsync((conn) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
conn.Insert(calendarItem);
|
|
|
|
|
|
conn.InsertAll(attendees);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-12-28 23:17:16 +01:00
|
|
|
|
|
2024-12-30 23:10:51 +01:00
|
|
|
|
public async Task<List<CalendarItem>> GetCalendarEventsAsync(IAccountCalendar calendar, DayRangeRenderModel dayRangeRenderModel)
|
2024-12-28 23:17:16 +01:00
|
|
|
|
{
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
2024-12-29 19:37:36 +01:00
|
|
|
|
var accountEvents = await Connection.Table<CalendarItem>().Where(x => x.CalendarId == calendar.Id).ToListAsync();
|
|
|
|
|
|
var result = new List<CalendarItem>();
|
2024-12-28 23:17:16 +01:00
|
|
|
|
|
|
|
|
|
|
foreach (var ev in accountEvents)
|
|
|
|
|
|
{
|
2024-12-29 19:37:36 +01:00
|
|
|
|
ev.AssignedCalendar = calendar;
|
|
|
|
|
|
|
2024-12-28 23:17:16 +01:00
|
|
|
|
// Parse recurrence rules
|
2024-12-30 23:10:51 +01:00
|
|
|
|
var calendarEvent = new CalendarEvent
|
2024-12-28 23:17:16 +01:00
|
|
|
|
{
|
2024-12-30 23:10:51 +01:00
|
|
|
|
Start = new CalDateTime(ev.StartDate),
|
|
|
|
|
|
End = new CalDateTime(ev.EndDate),
|
2024-12-28 23:17:16 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(ev.Recurrence))
|
|
|
|
|
|
{
|
2024-12-30 23:10:51 +01:00
|
|
|
|
// No recurrence, only check if we fall into the given period.
|
|
|
|
|
|
|
|
|
|
|
|
if (ev.Period.OverlapsWith(dayRangeRenderModel.Period))
|
2024-12-28 23:17:16 +01:00
|
|
|
|
{
|
2024-12-30 23:10:51 +01:00
|
|
|
|
// TODO: We overlap, but this might be a multi-day event.
|
|
|
|
|
|
// Should we split the events here or in panel?
|
|
|
|
|
|
// For now just continue.
|
|
|
|
|
|
|
2024-12-28 23:17:16 +01:00
|
|
|
|
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.
|
2024-12-30 23:10:51 +01:00
|
|
|
|
var occurrences = calendarEvent.GetOccurrences(dayRangeRenderModel.Period.Start, dayRangeRenderModel.Period.End);
|
2024-12-28 23:17:16 +01:00
|
|
|
|
|
|
|
|
|
|
foreach (var occurrence in occurrences)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.Add(ev);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2024-12-30 23:10:51 +01:00
|
|
|
|
|
|
|
|
|
|
public Task<AccountCalendar> GetAccountCalendarAsync(Guid accountCalendarId)
|
|
|
|
|
|
=> Connection.GetAsync<AccountCalendar>(accountCalendarId);
|
2024-12-27 00:18:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|