Files
Wino-Mail/Wino.Core.Domain/Collections/CalendarEventCollection.cs

156 lines
5.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Itenso.TimePeriod;
using Wino.Core.Domain.Entities.Calendar;
2024-12-28 23:17:16 +01:00
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Calendar;
2024-12-28 23:17:16 +01:00
2025-02-16 11:54:23 +01:00
namespace Wino.Core.Domain.Collections;
public class CalendarEventCollection
2024-12-28 23:17:16 +01:00
{
2025-02-16 11:54:23 +01:00
public event EventHandler<ICalendarItem> CalendarItemAdded;
public event EventHandler<ICalendarItem> CalendarItemRemoved;
2025-02-16 11:54:23 +01:00
public event EventHandler CalendarItemsCleared;
2025-02-16 11:54:23 +01:00
private ObservableRangeCollection<ICalendarItem> _internalRegularEvents = [];
private ObservableRangeCollection<ICalendarItem> _internalAllDayEvents = [];
2025-02-16 11:54:23 +01:00
public ReadOnlyObservableCollection<ICalendarItem> RegularEvents { get; }
public ReadOnlyObservableCollection<ICalendarItem> AllDayEvents { get; } // TODO: Rename this to include multi-day events.
public ITimePeriod Period { get; }
public CalendarSettings Settings { get; }
2025-02-16 11:54:23 +01:00
private readonly List<ICalendarItem> _allItems = new List<ICalendarItem>();
2025-02-16 11:54:23 +01:00
public CalendarEventCollection(ITimePeriod period, CalendarSettings settings)
{
Period = period;
Settings = settings;
2025-02-16 11:54:23 +01:00
RegularEvents = new ReadOnlyObservableCollection<ICalendarItem>(_internalRegularEvents);
AllDayEvents = new ReadOnlyObservableCollection<ICalendarItem>(_internalAllDayEvents);
}
2025-02-16 11:54:23 +01:00
public bool HasCalendarEvent(AccountCalendar accountCalendar)
=> _allItems.Any(x => x.AssignedCalendar.Id == accountCalendar.Id);
2025-02-16 11:54:23 +01:00
public ICalendarItem GetCalendarItem(Guid calendarItemId)
{
return _allItems.FirstOrDefault(x => x.Id == calendarItemId);
}
2025-02-16 11:54:23 +01:00
public void ClearSelectionStates()
{
foreach (var item in _allItems)
{
2025-02-16 11:54:23 +01:00
if (item is ICalendarItemViewModel calendarItemViewModel)
{
2025-02-16 11:54:23 +01:00
calendarItemViewModel.IsSelected = false;
}
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:54:23 +01:00
public void FilterByCalendars(IEnumerable<Guid> visibleCalendarIds)
{
foreach (var item in _allItems)
{
2025-02-16 11:54:23 +01:00
var collections = GetProperCollectionsForCalendarItem(item);
2025-02-16 11:54:23 +01:00
foreach (var collection in collections)
{
if (!visibleCalendarIds.Contains(item.AssignedCalendar.Id) && collection.Contains(item))
{
RemoveCalendarItemInternal(collection, item, false);
}
else if (visibleCalendarIds.Contains(item.AssignedCalendar.Id) && !collection.Contains(item))
{
2025-02-16 11:54:23 +01:00
AddCalendarItemInternal(collection, item, false);
}
}
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:54:23 +01:00
private IEnumerable<ObservableRangeCollection<ICalendarItem>> GetProperCollectionsForCalendarItem(ICalendarItem calendarItem)
{
// All-day events go to all days.
// Multi-day events go to both.
// Anything else goes to regular.
2025-07-07 21:13:53 +02:00
if (calendarItem.ItemType == Enums.CalendarItemType.AllDay || calendarItem.ItemType == Enums.CalendarItemType.MultiDayAllDay || calendarItem.ItemType == Enums.CalendarItemType.RecurringAllDay)
2025-02-16 11:54:23 +01:00
{
return [_internalAllDayEvents];
}
else if (calendarItem.ItemType == Enums.CalendarItemType.MultiDay || calendarItem.ItemType == Enums.CalendarItemType.MultiDayAllDay)
{
2025-02-16 11:54:23 +01:00
return [_internalRegularEvents, _internalAllDayEvents];
}
2025-02-16 11:54:23 +01:00
else
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
return [_internalRegularEvents];
}
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
public void AddCalendarItem(ICalendarItem calendarItem)
{
var collections = GetProperCollectionsForCalendarItem(calendarItem);
foreach (var collection in collections)
{
AddCalendarItemInternal(collection, calendarItem);
}
2025-02-16 11:54:23 +01:00
}
public void RemoveCalendarItem(ICalendarItem calendarItem)
{
var collections = GetProperCollectionsForCalendarItem(calendarItem);
2025-02-16 11:54:23 +01:00
foreach (var collection in collections)
2024-12-28 23:17:16 +01:00
{
2025-02-16 11:54:23 +01:00
RemoveCalendarItemInternal(collection, calendarItem);
}
}
2025-02-16 11:54:23 +01:00
private void AddCalendarItemInternal(ObservableRangeCollection<ICalendarItem> collection, ICalendarItem calendarItem, bool create = true)
{
if (calendarItem is not ICalendarItemViewModel)
throw new ArgumentException("CalendarItem must be of type ICalendarItemViewModel", nameof(calendarItem));
2025-02-16 11:54:23 +01:00
collection.Add(calendarItem);
2025-02-16 11:54:23 +01:00
if (create)
{
_allItems.Add(calendarItem);
2024-12-28 23:17:16 +01:00
}
2025-02-16 11:54:23 +01:00
CalendarItemAdded?.Invoke(this, calendarItem);
}
2025-02-16 11:54:23 +01:00
private void RemoveCalendarItemInternal(ObservableRangeCollection<ICalendarItem> collection, ICalendarItem calendarItem, bool destroy = true)
{
if (calendarItem is not ICalendarItemViewModel)
throw new ArgumentException("CalendarItem must be of type ICalendarItemViewModel", nameof(calendarItem));
2025-02-16 11:54:23 +01:00
collection.Remove(calendarItem);
2025-02-16 11:54:23 +01:00
if (destroy)
{
_allItems.Remove(calendarItem);
2024-12-28 23:17:16 +01:00
}
2025-02-16 11:54:23 +01:00
CalendarItemRemoved?.Invoke(this, calendarItem);
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
public void Clear()
{
_internalAllDayEvents.Clear();
_internalRegularEvents.Clear();
_allItems.Clear();
CalendarItemsCleared?.Invoke(this, EventArgs.Empty);
2024-12-28 23:17:16 +01:00
}
}