Fixed iCloud/Yahoo special providers and implemented more calendar metadata to support calendar colors in CalDav synchronizer.

This commit is contained in:
Burak Kaan Köse
2026-04-22 09:55:13 +02:00
parent 890bfc84f1
commit d66015bebd
8 changed files with 585 additions and 14 deletions
+54 -6
View File
@@ -1522,7 +1522,7 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
.ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase);
var usedCalendarColors = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var remotePrimaryCalendarId = remoteCalendars.FirstOrDefault()?.RemoteCalendarId;
var remotePrimaryCalendarId = GetPrimaryCalDavCalendarId(remoteCalendars);
foreach (var localCalendar in localCalendars.ToList())
{
@@ -1545,6 +1545,7 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
if (existingLocal == null)
{
var insertedCalendarColor = ResolveSynchronizedCalendarBackgroundColor(remoteCalendar.BackgroundColorHex, null, usedCalendarColors);
var newCalendar = new AccountCalendar
{
Id = Guid.NewGuid(),
@@ -1552,10 +1553,12 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
RemoteCalendarId = remoteCalendar.RemoteCalendarId,
Name = remoteCalendar.Name,
IsPrimary = isPrimary,
IsReadOnly = remoteCalendar.IsReadOnly,
IsSynchronizationEnabled = true,
IsExtended = true,
BackgroundColorHex = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors),
TimeZone = "UTC",
DefaultShowAs = remoteCalendar.DefaultShowAs,
BackgroundColorHex = insertedCalendarColor,
TimeZone = remoteCalendar.TimeZone,
SynchronizationDeltaToken = string.Empty
};
@@ -1565,10 +1568,15 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
continue;
}
var resolvedColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, existingLocal.BackgroundColorHex);
var resolvedColor = ResolveSynchronizedCalendarBackgroundColor(remoteCalendar.BackgroundColorHex, existingLocal, usedCalendarColors);
var resolvedTextColor = ColorHelpers.GetReadableTextColorHex(resolvedColor);
var shouldUpdate = !string.Equals(existingLocal.Name, remoteCalendar.Name, StringComparison.Ordinal)
|| !string.Equals(existingLocal.TimeZone, remoteCalendar.TimeZone, StringComparison.OrdinalIgnoreCase)
|| existingLocal.IsReadOnly != remoteCalendar.IsReadOnly
|| existingLocal.DefaultShowAs != remoteCalendar.DefaultShowAs
|| existingLocal.IsPrimary != isPrimary
|| !string.Equals(existingLocal.BackgroundColorHex, resolvedColor, StringComparison.OrdinalIgnoreCase);
|| !string.Equals(existingLocal.BackgroundColorHex, resolvedColor, StringComparison.OrdinalIgnoreCase)
|| !string.Equals(existingLocal.TextColorHex, resolvedTextColor, StringComparison.OrdinalIgnoreCase);
if (!shouldUpdate)
{
@@ -1577,14 +1585,54 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
}
existingLocal.Name = remoteCalendar.Name;
existingLocal.TimeZone = remoteCalendar.TimeZone;
existingLocal.IsReadOnly = remoteCalendar.IsReadOnly;
existingLocal.DefaultShowAs = remoteCalendar.DefaultShowAs;
existingLocal.IsPrimary = isPrimary;
existingLocal.BackgroundColorHex = resolvedColor;
existingLocal.TextColorHex = ColorHelpers.GetReadableTextColorHex(existingLocal.BackgroundColorHex);
existingLocal.TextColorHex = resolvedTextColor;
usedCalendarColors.Add(existingLocal.BackgroundColorHex);
await _imapChangeProcessor.UpdateAccountCalendarAsync(existingLocal).ConfigureAwait(false);
}
}
private static string GetPrimaryCalDavCalendarId(IReadOnlyList<CalDavCalendar> remoteCalendars)
{
if (remoteCalendars == null || remoteCalendars.Count == 0)
return string.Empty;
if (remoteCalendars.Any(calendar => calendar.Order.HasValue))
{
return remoteCalendars
.OrderBy(calendar => calendar.Order ?? double.MaxValue)
.ThenBy(calendar => calendar.Name, StringComparer.OrdinalIgnoreCase)
.Select(calendar => calendar.RemoteCalendarId)
.FirstOrDefault() ?? string.Empty;
}
return remoteCalendars.First().RemoteCalendarId;
}
private static string ResolveSynchronizedCalendarBackgroundColor(
string remoteBackgroundColor,
AccountCalendar accountCalendar,
ISet<string> usedCalendarColors = null)
{
if (accountCalendar?.IsBackgroundColorUserOverridden == true)
return accountCalendar.BackgroundColorHex;
var preferredColor = string.IsNullOrWhiteSpace(remoteBackgroundColor)
? accountCalendar?.BackgroundColorHex
: remoteBackgroundColor;
if (string.IsNullOrWhiteSpace(remoteBackgroundColor) && usedCalendarColors != null)
return ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, preferredColor);
return string.IsNullOrWhiteSpace(preferredColor)
? ColorHelpers.GenerateFlatColorHex()
: preferredColor;
}
private interface IImapCalendarOperationHandler
{
bool RequiresConnectedClient { get; }