Respect API calendar colors unless overridden
This commit is contained in:
@@ -87,6 +87,7 @@ public partial class CalendarAccountSettingsPageViewModel : CalendarBaseViewMode
|
||||
if (AccountCalendar != null && !string.IsNullOrEmpty(value))
|
||||
{
|
||||
AccountCalendar.BackgroundColorHex = value;
|
||||
AccountCalendar.IsBackgroundColorUserOverridden = true;
|
||||
SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class AccountCalendar : IAccountCalendar
|
||||
/// </summary>
|
||||
public string TextColorHex { get; set; }
|
||||
public string BackgroundColorHex { get; set; }
|
||||
public bool IsBackgroundColorUserOverridden { get; set; }
|
||||
public string TimeZone { get; set; }
|
||||
|
||||
[Ignore]
|
||||
|
||||
@@ -741,10 +741,11 @@ public class GmailSynchronizer : WinoSynchronizer<IClientServiceRequest, Message
|
||||
if (existingLocalCalendar == null)
|
||||
{
|
||||
// Insert new calendar.
|
||||
var fallbackColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, calendar.BackgroundColor);
|
||||
var remoteBackgroundColor = GetRemoteGmailCalendarBackgroundColor(calendar);
|
||||
var fallbackColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, remoteBackgroundColor);
|
||||
var localCalendar = calendar.AsCalendar(Account.Id, fallbackColor);
|
||||
localCalendar.IsPrimary = string.Equals(localCalendar.RemoteCalendarId, remotePrimaryCalendarId, StringComparison.OrdinalIgnoreCase);
|
||||
localCalendar.BackgroundColorHex = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, localCalendar.BackgroundColorHex);
|
||||
localCalendar.BackgroundColorHex = ResolveSynchronizedCalendarBackgroundColor(remoteBackgroundColor, localCalendar, usedCalendarColors);
|
||||
localCalendar.TextColorHex = ColorHelpers.GetReadableTextColorHex(localCalendar.BackgroundColorHex);
|
||||
usedCalendarColors.Add(localCalendar.BackgroundColorHex);
|
||||
insertedCalendars.Add(localCalendar);
|
||||
@@ -752,7 +753,7 @@ public class GmailSynchronizer : WinoSynchronizer<IClientServiceRequest, Message
|
||||
else
|
||||
{
|
||||
// Update existing calendar. Right now we only update the name.
|
||||
var resolvedColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, existingLocalCalendar.BackgroundColorHex);
|
||||
var resolvedColor = ResolveSynchronizedCalendarBackgroundColor(GetRemoteGmailCalendarBackgroundColor(calendar), existingLocalCalendar, usedCalendarColors);
|
||||
if (ShouldUpdateCalendar(calendar, existingLocalCalendar, remotePrimaryCalendarId) ||
|
||||
!string.Equals(existingLocalCalendar.BackgroundColorHex, resolvedColor, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
@@ -939,8 +940,8 @@ public class GmailSynchronizer : WinoSynchronizer<IClientServiceRequest, Message
|
||||
{
|
||||
var remoteCalendarName = calendarListEntry.Summary;
|
||||
var remoteTimeZone = calendarListEntry.TimeZone;
|
||||
var remoteBackgroundColor = string.IsNullOrEmpty(calendarListEntry.BackgroundColor) ? accountCalendar.BackgroundColorHex : calendarListEntry.BackgroundColor;
|
||||
var remoteTextColor = string.IsNullOrEmpty(calendarListEntry.ForegroundColor) ? accountCalendar.TextColorHex : calendarListEntry.ForegroundColor;
|
||||
var remoteBackgroundColor = ResolveSynchronizedCalendarBackgroundColor(GetRemoteGmailCalendarBackgroundColor(calendarListEntry), accountCalendar);
|
||||
var remoteTextColor = ColorHelpers.GetReadableTextColorHex(remoteBackgroundColor);
|
||||
var remoteIsPrimary = string.Equals(calendarListEntry.Id, remotePrimaryCalendarId, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
bool isNameChanged = !string.Equals(accountCalendar.Name, remoteCalendarName, StringComparison.OrdinalIgnoreCase);
|
||||
@@ -952,6 +953,26 @@ public class GmailSynchronizer : WinoSynchronizer<IClientServiceRequest, Message
|
||||
return isNameChanged || isTimeZoneChanged || isBackgroundColorChanged || isTextColorChanged || isPrimaryChanged;
|
||||
}
|
||||
|
||||
private static string GetRemoteGmailCalendarBackgroundColor(CalendarListEntry calendarListEntry)
|
||||
=> string.IsNullOrWhiteSpace(calendarListEntry?.BackgroundColor) ? null : calendarListEntry.BackgroundColor;
|
||||
|
||||
private static string ResolveSynchronizedCalendarBackgroundColor(
|
||||
string remoteBackgroundColor,
|
||||
AccountCalendar accountCalendar,
|
||||
ISet<string> 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<CalendarListEntry> remoteCalendars)
|
||||
{
|
||||
if (remoteCalendars == null || remoteCalendars.Count == 0)
|
||||
|
||||
@@ -2517,10 +2517,11 @@ public class OutlookSynchronizer : WinoSynchronizer<RequestInformation, Message,
|
||||
if (existingLocalCalendar == null)
|
||||
{
|
||||
// Insert new calendar.
|
||||
var fallbackColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, calendar.HexColor);
|
||||
var remoteBackgroundColor = GetRemoteOutlookCalendarBackgroundColor(calendar);
|
||||
var fallbackColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, remoteBackgroundColor);
|
||||
var localCalendar = calendar.AsCalendar(Account, fallbackColor);
|
||||
localCalendar.IsPrimary = string.Equals(localCalendar.RemoteCalendarId, remotePrimaryCalendarId, StringComparison.OrdinalIgnoreCase);
|
||||
localCalendar.BackgroundColorHex = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, localCalendar.BackgroundColorHex);
|
||||
localCalendar.BackgroundColorHex = ResolveSynchronizedCalendarBackgroundColor(remoteBackgroundColor, localCalendar, usedCalendarColors);
|
||||
localCalendar.TextColorHex = ColorHelpers.GetReadableTextColorHex(localCalendar.BackgroundColorHex);
|
||||
usedCalendarColors.Add(localCalendar.BackgroundColorHex);
|
||||
insertedCalendars.Add(localCalendar);
|
||||
@@ -2528,7 +2529,7 @@ public class OutlookSynchronizer : WinoSynchronizer<RequestInformation, Message,
|
||||
else
|
||||
{
|
||||
// Update existing calendar. Right now we only update the name.
|
||||
var resolvedColor = ColorHelpers.GetDistinctFlatColorHex(usedCalendarColors, existingLocalCalendar.BackgroundColorHex);
|
||||
var resolvedColor = ResolveSynchronizedCalendarBackgroundColor(GetRemoteOutlookCalendarBackgroundColor(calendar), existingLocalCalendar, usedCalendarColors);
|
||||
if (ShouldUpdateCalendar(calendar, existingLocalCalendar, remotePrimaryCalendarId) ||
|
||||
!string.Equals(existingLocalCalendar.BackgroundColorHex, resolvedColor, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
@@ -2570,7 +2571,7 @@ public class OutlookSynchronizer : WinoSynchronizer<RequestInformation, Message,
|
||||
private bool ShouldUpdateCalendar(Calendar calendar, AccountCalendar accountCalendar, string remotePrimaryCalendarId)
|
||||
{
|
||||
var remoteCalendarName = calendar.Name;
|
||||
var remoteBackgroundColor = string.IsNullOrEmpty(calendar.HexColor) ? accountCalendar.BackgroundColorHex : calendar.HexColor;
|
||||
var remoteBackgroundColor = ResolveSynchronizedCalendarBackgroundColor(GetRemoteOutlookCalendarBackgroundColor(calendar), accountCalendar);
|
||||
var remoteIsPrimary = string.Equals(calendar.Id, remotePrimaryCalendarId, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
bool isNameChanged = !string.Equals(accountCalendar.Name, remoteCalendarName, StringComparison.OrdinalIgnoreCase);
|
||||
@@ -2580,6 +2581,26 @@ public class OutlookSynchronizer : WinoSynchronizer<RequestInformation, Message,
|
||||
return isNameChanged || isBackgroundColorChanged || isPrimaryChanged;
|
||||
}
|
||||
|
||||
private static string GetRemoteOutlookCalendarBackgroundColor(Calendar calendar)
|
||||
=> string.IsNullOrWhiteSpace(calendar?.HexColor) ? null : calendar.HexColor;
|
||||
|
||||
private static string ResolveSynchronizedCalendarBackgroundColor(
|
||||
string remoteBackgroundColor,
|
||||
AccountCalendar accountCalendar,
|
||||
ISet<string> 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<string> GetPrimaryCalendarIdAsync(IList<Calendar> remoteCalendars, CancellationToken cancellationToken)
|
||||
{
|
||||
if (remoteCalendars == null || remoteCalendars.Count == 0)
|
||||
|
||||
@@ -342,6 +342,7 @@ public partial class AccountDetailsPageViewModel : MailBaseViewModel
|
||||
return;
|
||||
|
||||
calendarItem.SetBackgroundColor(color);
|
||||
calendarItem.Calendar.IsBackgroundColorUserOverridden = true;
|
||||
await _calendarService.UpdateAccountCalendarAsync(calendarItem.Calendar);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user