From aba7539e7dcc27aa8bbd4b9ccaac17c364567106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Tue, 20 May 2025 21:08:19 +0200 Subject: [PATCH] Replaced DateTimeOffset usage in the calendar. --- .../AccountManagementViewModel.cs | 2 ++ .../CalendarPageViewModel.cs | 26 ++++++++-------- .../Data/CalendarItemViewModel.cs | 4 +-- .../Entities/Calendar/CalendarItem.cs | 29 ++++++++---------- Wino.Core.Domain/Interfaces/ICalendarItem.cs | 4 +-- .../Extensions/GoogleIntegratorExtensions.cs | 4 +-- .../Processors/GmailChangeProcessor.cs | 30 +++---------------- .../Processors/OutlookChangeProcessor.cs | 13 ++++---- Wino.Services/CalendarService.cs | 2 +- 9 files changed, 47 insertions(+), 67 deletions(-) diff --git a/Wino.Calendar.ViewModels/AccountManagementViewModel.cs b/Wino.Calendar.ViewModels/AccountManagementViewModel.cs index f7eb9563..a7f087c5 100644 --- a/Wino.Calendar.ViewModels/AccountManagementViewModel.cs +++ b/Wino.Calendar.ViewModels/AccountManagementViewModel.cs @@ -142,5 +142,7 @@ public partial class AccountManagementViewModel : AccountManagementPageViewModel }; var synchronizationResponse = await WinoServerConnectionManager.GetResponseAsync(new NewCalendarSynchronizationRequested(synchronizationOptions, SynchronizationSource.Client)); + accountCreationDialog.Complete(cancel: false); + } } diff --git a/Wino.Calendar.ViewModels/CalendarPageViewModel.cs b/Wino.Calendar.ViewModels/CalendarPageViewModel.cs index 99cd7a94..f882114b 100644 --- a/Wino.Calendar.ViewModels/CalendarPageViewModel.cs +++ b/Wino.Calendar.ViewModels/CalendarPageViewModel.cs @@ -237,20 +237,20 @@ public partial class CalendarPageViewModel : CalendarBaseViewModel, { var durationSeconds = (QuickEventEndTime - QuickEventStartTime).TotalSeconds; - var testCalendarItem = new CalendarItem - { - CalendarId = SelectedQuickEventAccountCalendar.Id, - StartDate = QuickEventStartTime, - DurationInSeconds = durationSeconds, - CreatedAt = DateTime.UtcNow, - Description = string.Empty, - Location = Location, - Title = EventName, - Id = Guid.NewGuid() - }; + //var testCalendarItem = new CalendarItem + //{ + // CalendarId = SelectedQuickEventAccountCalendar.Id, + // StartDate = QuickEventStartTime, + // DurationInSeconds = durationSeconds, + // CreatedAt = DateTime.UtcNow, + // Description = string.Empty, + // Location = Location, + // Title = EventName, + // Id = Guid.NewGuid() + //}; - IsQuickEventDialogOpen = false; - await _calendarService.CreateNewCalendarItemAsync(testCalendarItem, null); + //IsQuickEventDialogOpen = false; + //await _calendarService.CreateNewCalendarItemAsync(testCalendarItem, null); // TODO: Create the request with the synchronizer. } diff --git a/Wino.Calendar.ViewModels/Data/CalendarItemViewModel.cs b/Wino.Calendar.ViewModels/Data/CalendarItemViewModel.cs index f402a562..8966bd0f 100644 --- a/Wino.Calendar.ViewModels/Data/CalendarItemViewModel.cs +++ b/Wino.Calendar.ViewModels/Data/CalendarItemViewModel.cs @@ -17,11 +17,11 @@ public partial class CalendarItemViewModel : ObservableObject, ICalendarItem, IC public IAccountCalendar AssignedCalendar => CalendarItem.AssignedCalendar; - public DateTime StartDate { get => CalendarItem.StartDate; set => CalendarItem.StartDate = value; } + public DateTime StartDate => CalendarItem.StartDate; public DateTime EndDate => CalendarItem.EndDate; - public double DurationInSeconds { get => CalendarItem.DurationInSeconds; set => CalendarItem.DurationInSeconds = value; } + public double DurationInSeconds => CalendarItem.DurationInSeconds; public ITimePeriod Period => CalendarItem.Period; diff --git a/Wino.Core.Domain/Entities/Calendar/CalendarItem.cs b/Wino.Core.Domain/Entities/Calendar/CalendarItem.cs index de03e74e..28b674c4 100644 --- a/Wino.Core.Domain/Entities/Calendar/CalendarItem.cs +++ b/Wino.Core.Domain/Entities/Calendar/CalendarItem.cs @@ -17,24 +17,23 @@ public class CalendarItem : ICalendarItem public string Description { get; set; } public string Location { get; set; } - public DateTime StartDate { get; set; } + public string StartTimeOffset { get; set; } + public string EndTimeOffset { get; set; } - public DateTime EndDate - { - get - { - return StartDate.AddSeconds(DurationInSeconds); - } - } + public DateTimeOffset StartDateTimeOffset => DateTimeOffset.Parse(StartTimeOffset); + public DateTimeOffset EndDateTimeOffsete => DateTimeOffset.Parse(EndTimeOffset); - public TimeSpan StartDateOffset { get; set; } - public TimeSpan EndDateOffset { get; set; } + public DateTime StartDate => StartDateTimeOffset.DateTime;//TimeZoneInfo.ConvertTime(StartDateTimeOffset, TimeZoneInfo.Local).DateTime; + public DateTime EndDate => EndDateTimeOffsete.DateTime;// TimeZoneInfo.ConvertTime(, TimeZoneInfo.Local).DateTime; private ITimePeriod _period; public ITimePeriod Period { get { + // Period must be loaded by user's current UI culture based on StartDateTimeOffset and EndDateTimeOffset + // Get the time zone corresponding to the current culture + _period ??= new TimeRange(StartDate, EndDate); return _period; @@ -94,7 +93,7 @@ public class CalendarItem : ICalendarItem } } - public double DurationInSeconds { get; set; } + public double DurationInSeconds => Period.Duration.TotalSeconds; public string Recurrence { get; set; } public string OrganizerDisplayName { get; set; } @@ -144,7 +143,7 @@ public class CalendarItem : ICalendarItem /// public Guid EventTrackingId => IsOccurrence ? RecurringCalendarItemId.Value : Id; - public CalendarItem CreateRecurrence(DateTime startDate, double durationInSeconds) + public CalendarItem CreateRecurrence(DateTimeOffset startDateOffset, DateTimeOffset endDateOffset) { // Create a copy with the new start date and duration @@ -154,8 +153,8 @@ public class CalendarItem : ICalendarItem Title = Title, Description = Description, Location = Location, - StartDate = startDate, - DurationInSeconds = durationInSeconds, + StartTimeOffset = startDateOffset.ToString("o"), + EndTimeOffset = endDateOffset.ToString("o"), Recurrence = Recurrence, OrganizerDisplayName = OrganizerDisplayName, OrganizerEmail = OrganizerEmail, @@ -168,8 +167,6 @@ public class CalendarItem : ICalendarItem Status = Status, CustomEventColorHex = CustomEventColorHex, HtmlLink = HtmlLink, - StartDateOffset = StartDateOffset, - EndDateOffset = EndDateOffset, RemoteEventId = RemoteEventId, IsHidden = IsHidden, IsLocked = IsLocked, diff --git a/Wino.Core.Domain/Interfaces/ICalendarItem.cs b/Wino.Core.Domain/Interfaces/ICalendarItem.cs index e83827a6..d0b31fcc 100644 --- a/Wino.Core.Domain/Interfaces/ICalendarItem.cs +++ b/Wino.Core.Domain/Interfaces/ICalendarItem.cs @@ -8,9 +8,9 @@ public interface ICalendarItem string Title { get; } Guid Id { get; } IAccountCalendar AssignedCalendar { get; } - DateTime StartDate { get; set; } + DateTime StartDate { get; } DateTime EndDate { get; } - double DurationInSeconds { get; set; } + double DurationInSeconds { get; } ITimePeriod Period { get; } bool IsAllDayEvent { get; } diff --git a/Wino.Core/Extensions/GoogleIntegratorExtensions.cs b/Wino.Core/Extensions/GoogleIntegratorExtensions.cs index 8e19322a..7282256a 100644 --- a/Wino.Core/Extensions/GoogleIntegratorExtensions.cs +++ b/Wino.Core/Extensions/GoogleIntegratorExtensions.cs @@ -190,7 +190,7 @@ public static class GoogleIntegratorExtensions return calendar; } - public static DateTimeOffset? GetEventDateTimeOffset(EventDateTime calendarEvent) + public static DateTimeOffset GetEventDateTimeOffset(EventDateTime calendarEvent) { if (calendarEvent != null) { @@ -212,7 +212,7 @@ public static class GoogleIntegratorExtensions } } - return null; + throw new Exception("Invalid date format in Google Calendar event date."); } /// diff --git a/Wino.Core/Integration/Processors/GmailChangeProcessor.cs b/Wino.Core/Integration/Processors/GmailChangeProcessor.cs index c478308d..2bdd0459 100644 --- a/Wino.Core/Integration/Processors/GmailChangeProcessor.cs +++ b/Wino.Core/Integration/Processors/GmailChangeProcessor.cs @@ -66,12 +66,6 @@ public class GmailChangeProcessor : DefaultChangeProcessor, IGmailChangeProcesso var eventStartDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.Start); var eventEndDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.End); - double totalDurationInSeconds = 0; - - if (eventStartDateTimeOffset != null && eventEndDateTimeOffset != null) - { - totalDurationInSeconds = (eventEndDateTimeOffset.Value - eventStartDateTimeOffset.Value).TotalSeconds; - } CalendarItem calendarItem = null; @@ -80,12 +74,6 @@ public class GmailChangeProcessor : DefaultChangeProcessor, IGmailChangeProcesso // Exceptions of parent events might not have all the fields populated. // We must use the parent event's data for fields that don't exists. - // Update duration if it's not populated. - if (totalDurationInSeconds == 0) - { - totalDurationInSeconds = parentRecurringEvent.DurationInSeconds; - } - var organizerMail = GetOrganizerEmail(calendarEvent, organizerAccount); var organizerName = GetOrganizerName(calendarEvent, organizerAccount); @@ -96,10 +84,8 @@ public class GmailChangeProcessor : DefaultChangeProcessor, IGmailChangeProcesso CreatedAt = DateTimeOffset.UtcNow, Description = calendarEvent.Description ?? parentRecurringEvent.Description, Id = Guid.NewGuid(), - StartDate = eventStartDateTimeOffset.Value.DateTime, - StartDateOffset = eventStartDateTimeOffset.Value.Offset, - EndDateOffset = eventEndDateTimeOffset?.Offset ?? parentRecurringEvent.EndDateOffset, - DurationInSeconds = totalDurationInSeconds, + StartTimeOffset = eventStartDateTimeOffset.ToString("o"), + EndTimeOffset = eventEndDateTimeOffset.ToString("o"), Location = string.IsNullOrEmpty(calendarEvent.Location) ? parentRecurringEvent.Location : calendarEvent.Location, // Leave it empty if it's not populated. @@ -120,22 +106,14 @@ public class GmailChangeProcessor : DefaultChangeProcessor, IGmailChangeProcesso // This is a parent event creation. // Start-End dates are guaranteed to be populated. - if (eventStartDateTimeOffset == null || eventEndDateTimeOffset == null) - { - Log.Error("Failed to create parent event because either start or end date is not specified."); - return; - } - calendarItem = new CalendarItem() { CalendarId = assignedCalendar.Id, CreatedAt = DateTimeOffset.UtcNow, Description = calendarEvent.Description, Id = Guid.NewGuid(), - StartDate = eventStartDateTimeOffset.Value.DateTime, - StartDateOffset = eventStartDateTimeOffset.Value.Offset, - EndDateOffset = eventEndDateTimeOffset.Value.Offset, - DurationInSeconds = totalDurationInSeconds, + StartTimeOffset = eventStartDateTimeOffset.ToString("o"), + EndTimeOffset = eventEndDateTimeOffset.ToString("o"), Location = calendarEvent.Location, Recurrence = GoogleIntegratorExtensions.GetRecurrenceString(calendarEvent), Status = GetStatus(calendarEvent.Status), diff --git a/Wino.Core/Integration/Processors/OutlookChangeProcessor.cs b/Wino.Core/Integration/Processors/OutlookChangeProcessor.cs index 3b72c33e..6ed2dd92 100644 --- a/Wino.Core/Integration/Processors/OutlookChangeProcessor.cs +++ b/Wino.Core/Integration/Processors/OutlookChangeProcessor.cs @@ -61,18 +61,21 @@ public class OutlookChangeProcessor(IDatabaseService databaseService, DateTimeOffset eventStartDateTimeOffset = OutlookIntegratorExtensions.GetDateTimeOffsetFromDateTimeTimeZone(calendarEvent.Start); DateTimeOffset eventEndDateTimeOffset = OutlookIntegratorExtensions.GetDateTimeOffsetFromDateTimeTimeZone(calendarEvent.End); - var durationInSeconds = (eventEndDateTimeOffset - eventStartDateTimeOffset).TotalSeconds; + savingItem.RemoteEventId = calendarEvent.Id; - savingItem.StartDate = eventStartDateTimeOffset.DateTime; - savingItem.StartDateOffset = eventStartDateTimeOffset.Offset; - savingItem.EndDateOffset = eventEndDateTimeOffset.Offset; - savingItem.DurationInSeconds = durationInSeconds; + savingItem.StartTimeOffset = eventStartDateTimeOffset.ToString("O"); + savingItem.EndTimeOffset = eventEndDateTimeOffset.ToString("O"); savingItem.Title = calendarEvent.Subject; savingItem.Description = calendarEvent.Body?.Content; savingItem.Location = calendarEvent.Location?.DisplayName; + if (savingItem.Title.Contains("Atatürk")) + { + + } + if (calendarEvent.Type == EventType.Exception && !string.IsNullOrEmpty(calendarEvent.SeriesMasterId)) { // This is a recurring event exception. diff --git a/Wino.Services/CalendarService.cs b/Wino.Services/CalendarService.cs index 3f31175c..96780cc4 100644 --- a/Wino.Services/CalendarService.cs +++ b/Wino.Services/CalendarService.cs @@ -160,7 +160,7 @@ public class CalendarService : BaseDatabaseService, ICalendarService // There is no exception for the period. // Change the instance StartDate and Duration. - var recurrence = ev.CreateRecurrence(occurrence.Period.StartTime.Value, occurrence.Period.Duration.TotalSeconds); + var recurrence = ev.CreateRecurrence(occurrence.Period.StartTime.Value, occurrence.Period.EndTime.Value); result.Add(recurrence); }