From 448ebd6036b3f2160e55c727f4fc4c8751beeebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Sat, 11 Apr 2026 15:18:30 +0200 Subject: [PATCH] Respect API calendar colors unless overridden --- .../CalendarAccountSettingsPageViewModel.cs | 1 + .../Entities/Calendar/AccountCalendar.cs | 1 + Wino.Core/Synchronizers/GmailSynchronizer.cs | 31 ++++++++++++++++--- .../Synchronizers/OutlookSynchronizer.cs | 29 ++++++++++++++--- .../AccountDetailsPageViewModel.cs | 1 + Wino.Services/DatabaseService.cs | 9 ++++++ 6 files changed, 63 insertions(+), 9 deletions(-) diff --git a/Wino.Calendar.ViewModels/CalendarAccountSettingsPageViewModel.cs b/Wino.Calendar.ViewModels/CalendarAccountSettingsPageViewModel.cs index 79400c7d..69112b07 100644 --- a/Wino.Calendar.ViewModels/CalendarAccountSettingsPageViewModel.cs +++ b/Wino.Calendar.ViewModels/CalendarAccountSettingsPageViewModel.cs @@ -87,6 +87,7 @@ public partial class CalendarAccountSettingsPageViewModel : CalendarBaseViewMode if (AccountCalendar != null && !string.IsNullOrEmpty(value)) { AccountCalendar.BackgroundColorHex = value; + AccountCalendar.IsBackgroundColorUserOverridden = true; SaveChangesAsync(); } } diff --git a/Wino.Core.Domain/Entities/Calendar/AccountCalendar.cs b/Wino.Core.Domain/Entities/Calendar/AccountCalendar.cs index e3fe9ba6..79842c08 100644 --- a/Wino.Core.Domain/Entities/Calendar/AccountCalendar.cs +++ b/Wino.Core.Domain/Entities/Calendar/AccountCalendar.cs @@ -25,6 +25,7 @@ public class AccountCalendar : IAccountCalendar /// public string TextColorHex { get; set; } public string BackgroundColorHex { get; set; } + public bool IsBackgroundColorUserOverridden { get; set; } public string TimeZone { get; set; } [Ignore] diff --git a/Wino.Core/Synchronizers/GmailSynchronizer.cs b/Wino.Core/Synchronizers/GmailSynchronizer.cs index f2d2ec7f..9d6fc869 100644 --- a/Wino.Core/Synchronizers/GmailSynchronizer.cs +++ b/Wino.Core/Synchronizers/GmailSynchronizer.cs @@ -741,10 +741,11 @@ public class GmailSynchronizer : WinoSynchronizer string.IsNullOrWhiteSpace(calendarListEntry?.BackgroundColor) ? null : calendarListEntry.BackgroundColor; + + private static string ResolveSynchronizedCalendarBackgroundColor( + string remoteBackgroundColor, + AccountCalendar accountCalendar, + ISet usedCalendarColors = null) + { + if (accountCalendar.IsBackgroundColorUserOverridden) + return accountCalendar.BackgroundColorHex; + + var preferredColor = string.IsNullOrWhiteSpace(remoteBackgroundColor) + ? accountCalendar.BackgroundColorHex + : remoteBackgroundColor; + + return string.IsNullOrWhiteSpace(remoteBackgroundColor) && usedCalendarColors != null + ? ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, preferredColor) + : preferredColor; + } + private string GetPrimaryCalendarId(IList remoteCalendars) { if (remoteCalendars == null || remoteCalendars.Count == 0) diff --git a/Wino.Core/Synchronizers/OutlookSynchronizer.cs b/Wino.Core/Synchronizers/OutlookSynchronizer.cs index 8790d6f9..fc6f19d2 100644 --- a/Wino.Core/Synchronizers/OutlookSynchronizer.cs +++ b/Wino.Core/Synchronizers/OutlookSynchronizer.cs @@ -2517,10 +2517,11 @@ public class OutlookSynchronizer : WinoSynchronizer string.IsNullOrWhiteSpace(calendar?.HexColor) ? null : calendar.HexColor; + + private static string ResolveSynchronizedCalendarBackgroundColor( + string remoteBackgroundColor, + AccountCalendar accountCalendar, + ISet usedCalendarColors = null) + { + if (accountCalendar.IsBackgroundColorUserOverridden) + return accountCalendar.BackgroundColorHex; + + var preferredColor = string.IsNullOrWhiteSpace(remoteBackgroundColor) + ? accountCalendar.BackgroundColorHex + : remoteBackgroundColor; + + return string.IsNullOrWhiteSpace(remoteBackgroundColor) && usedCalendarColors != null + ? ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, preferredColor) + : preferredColor; + } + private async Task GetPrimaryCalendarIdAsync(IList remoteCalendars, CancellationToken cancellationToken) { if (remoteCalendars == null || remoteCalendars.Count == 0) diff --git a/Wino.Mail.ViewModels/AccountDetailsPageViewModel.cs b/Wino.Mail.ViewModels/AccountDetailsPageViewModel.cs index b6d2d8f2..f2e11754 100644 --- a/Wino.Mail.ViewModels/AccountDetailsPageViewModel.cs +++ b/Wino.Mail.ViewModels/AccountDetailsPageViewModel.cs @@ -342,6 +342,7 @@ public partial class AccountDetailsPageViewModel : MailBaseViewModel return; calendarItem.SetBackgroundColor(color); + calendarItem.Calendar.IsBackgroundColorUserOverridden = true; await _calendarService.UpdateAccountCalendarAsync(calendarItem.Calendar); } diff --git a/Wino.Services/DatabaseService.cs b/Wino.Services/DatabaseService.cs index 18addf24..e617f3ae 100644 --- a/Wino.Services/DatabaseService.cs +++ b/Wino.Services/DatabaseService.cs @@ -142,6 +142,15 @@ public class DatabaseService : IDatabaseService .ConfigureAwait(false); } + var accountCalendarColumns = await Connection.GetTableInfoAsync(nameof(AccountCalendar)).ConfigureAwait(false); + + if (!accountCalendarColumns.Any(c => c.Name == nameof(AccountCalendar.IsBackgroundColorUserOverridden))) + { + await Connection + .ExecuteAsync($"ALTER TABLE {nameof(AccountCalendar)} ADD COLUMN {nameof(AccountCalendar.IsBackgroundColorUserOverridden)} INTEGER NOT NULL DEFAULT 0") + .ConfigureAwait(false); + } + await Connection.ExecuteAsync("DROP TABLE IF EXISTS WinoAccountAddOnCache").ConfigureAwait(false); }