Local time rendering for events.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Itenso.TimePeriod;
|
||||
using Wino.Core.Domain.Entities.Calendar;
|
||||
@@ -22,6 +23,16 @@ public partial class CalendarItemViewModel : ObservableObject, ICalendarItem, IC
|
||||
|
||||
public DateTime EndDateTime => CalendarItem.EndDateTime;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the start date and time in the local time zone for display purposes.
|
||||
/// </summary>
|
||||
public DateTime LocalStartDateTime => ConvertToLocalTime();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the end date and time in the local time zone for display purposes.
|
||||
/// </summary>
|
||||
public DateTime LocalEndDateTime => ConvertToLocalTime();
|
||||
|
||||
public ITimePeriod Period => CalendarItem.Period;
|
||||
|
||||
public bool IsRecurringEvent => !string.IsNullOrEmpty(CalendarItem.RecurrenceRules) || !string.IsNullOrEmpty(CalendarItem.RecurringEventId);
|
||||
@@ -31,11 +42,50 @@ public partial class CalendarItemViewModel : ObservableObject, ICalendarItem, IC
|
||||
|
||||
public ObservableCollection<CalendarEventAttendee> Attendees { get; } = new ObservableCollection<CalendarEventAttendee>();
|
||||
|
||||
public CalendarItemType ItemType => ((ICalendarItem)CalendarItem).ItemType;
|
||||
public CalendarItemType ItemType => CalendarItem.ItemType;
|
||||
|
||||
public CalendarItemViewModel(CalendarItem calendarItem)
|
||||
{
|
||||
CalendarItem = calendarItem;
|
||||
|
||||
Debug.WriteLine($"{Title} : {ItemType}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a DateTime to local time based on the provided timezone.
|
||||
/// If timezone is empty or null, assumes the DateTime is in UTC.
|
||||
/// </summary>
|
||||
/// <param name="dateTime">The DateTime to convert</param>
|
||||
/// <param name="timeZone">The timezone string. If empty/null, assumes UTC.</param>
|
||||
/// <returns>DateTime converted to local time</returns>
|
||||
private DateTime ConvertToLocalTime()
|
||||
{
|
||||
// All day events ignore time zones and are treated as local time.
|
||||
if (ItemType == CalendarItemType.AllDay || ItemType == CalendarItemType.MultiDayAllDay || ItemType == CalendarItemType.RecurringAllDay)
|
||||
return CalendarItem.StartDateTime;
|
||||
|
||||
if (string.IsNullOrEmpty(CalendarItem.TimeZone))
|
||||
{
|
||||
// If no timezone specified, assume it's UTC and convert to local time
|
||||
return DateTime.SpecifyKind(CalendarItem.StartDateTime, DateTimeKind.Utc).ToLocalTime();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Parse the timezone and convert to local time
|
||||
var sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(CalendarItem.TimeZone);
|
||||
return TimeZoneInfo.ConvertTimeToUtc(CalendarItem.StartDateTime, sourceTimeZone).ToLocalTime();
|
||||
}
|
||||
catch (TimeZoneNotFoundException)
|
||||
{
|
||||
// If timezone is not found, fallback to treating as UTC
|
||||
return DateTime.SpecifyKind(CalendarItem.StartDateTime, DateTimeKind.Utc).ToLocalTime();
|
||||
}
|
||||
catch (InvalidTimeZoneException)
|
||||
{
|
||||
// If timezone is invalid, fallback to treating as UTC
|
||||
return DateTime.SpecifyKind(CalendarItem.StartDateTime, DateTimeKind.Utc).ToLocalTime();
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString() => CalendarItem.Title;
|
||||
|
||||
Reference in New Issue
Block a user