Files
Wino-Mail/Wino.Calendar.ViewModels/Data/CalendarItemViewModel.cs
T

101 lines
3.2 KiB
C#
Raw Normal View History

2024-12-28 23:17:16 +01:00
using System;
using System.Collections.ObjectModel;
2024-12-28 23:17:16 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using Itenso.TimePeriod;
using Wino.Core.Domain.Entities.Calendar;
2024-12-28 23:17:16 +01:00
using Wino.Core.Domain.Interfaces;
2025-05-18 14:06:25 +02:00
namespace Wino.Calendar.ViewModels.Data;
2024-12-28 23:17:16 +01:00
2025-05-18 14:06:25 +02:00
public partial class CalendarItemViewModel : ObservableObject, ICalendarItem, ICalendarItemViewModel
{
public CalendarItem CalendarItem { get; }
2024-12-28 23:17:16 +01:00
2025-05-18 14:06:25 +02:00
public string Title => CalendarItem.Title;
2024-12-28 23:17:16 +01:00
2025-05-18 14:06:25 +02:00
public Guid Id => CalendarItem.Id;
2024-12-28 23:17:16 +01:00
2025-05-18 14:06:25 +02:00
public IAccountCalendar AssignedCalendar => CalendarItem.AssignedCalendar;
2024-12-28 23:17:16 +01:00
/// <summary>
2026-01-01 10:07:56 +01:00
/// Gets or sets the start date converted to user's local timezone for display.
/// The underlying CalendarItem stores dates according to their timezone.
/// </summary>
public DateTime StartDate
{
get
{
2026-01-01 10:07:56 +01:00
// Get start date in user's local timezone
return CalendarItem.LocalStartDate;
}
set
{
2026-01-01 10:07:56 +01:00
// When setting from UI (in local time), convert to event's timezone for storage
if (!string.IsNullOrEmpty(CalendarItem.StartTimeZone))
{
try
{
2026-01-01 10:07:56 +01:00
var sourceTimeZone = TimeZoneInfo.Local;
var targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(CalendarItem.StartTimeZone);
CalendarItem.StartDate = TimeZoneInfo.ConvertTime(value, sourceTimeZone, targetTimeZone);
}
catch
{
2026-01-01 10:07:56 +01:00
// If timezone lookup fails, set as-is
CalendarItem.StartDate = value;
}
}
else
{
2026-01-01 10:07:56 +01:00
// No timezone info, set as-is
CalendarItem.StartDate = value;
}
}
}
2024-12-28 23:17:16 +01:00
/// <summary>
2026-01-01 10:07:56 +01:00
/// Gets the end date converted to user's local timezone for display.
/// The underlying CalendarItem stores dates according to their timezone.
/// </summary>
public DateTime EndDate
{
get
{
2026-01-01 10:07:56 +01:00
// Get end date in user's local timezone
return CalendarItem.LocalEndDate;
}
}
2025-05-18 14:06:25 +02:00
public double DurationInSeconds { get => CalendarItem.DurationInSeconds; set => CalendarItem.DurationInSeconds = value; }
/// <summary>
/// Gets the time period in local time.
/// </summary>
public ITimePeriod Period
{
get
{
// Return a period using local times for UI display
return new TimeRange(StartDate, EndDate);
}
}
2025-05-18 14:06:25 +02:00
public bool IsAllDayEvent => CalendarItem.IsAllDayEvent;
public bool IsMultiDayEvent => CalendarItem.IsMultiDayEvent;
public bool IsRecurringEvent => CalendarItem.IsRecurringEvent;
public bool IsRecurringChild => CalendarItem.IsRecurringChild;
public bool IsRecurringParent => CalendarItem.IsRecurringParent;
2025-05-18 14:06:25 +02:00
[ObservableProperty]
public partial bool IsSelected { get; set; }
2025-05-18 14:06:25 +02:00
public ObservableCollection<CalendarEventAttendee> Attendees { get; } = new ObservableCollection<CalendarEventAttendee>();
2025-05-18 14:06:25 +02:00
public CalendarItemViewModel(CalendarItem calendarItem)
{
CalendarItem = calendarItem;
2024-12-28 23:17:16 +01:00
}
2025-05-18 14:06:25 +02:00
public override string ToString() => CalendarItem.Title;
2026-01-01 10:07:56 +01:00
}