Fixing UI thread issues with bulk operations and request queue refactoring.
This commit is contained in:
@@ -14,6 +14,7 @@ using Wino.Core.Domain.Enums;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
using Wino.Mail.ViewModels.Data;
|
||||
using Wino.Messaging.Client.Mails;
|
||||
using Wino.Messaging.UI;
|
||||
|
||||
namespace Wino.Mail.ViewModels.Collections;
|
||||
|
||||
@@ -895,6 +896,121 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
return UpdateExistingItemAsync(itemContainer, updatedMailCopy, mailUpdateSource, changedProperties);
|
||||
});
|
||||
|
||||
public Task UpdateMailStateAsync(MailStateChange updatedState, EntityUpdateSource mailUpdateSource)
|
||||
=> RunSerializedAsync(() =>
|
||||
{
|
||||
if (updatedState == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
var itemContainer = GetMailItemContainer(updatedState.UniqueId);
|
||||
|
||||
if (itemContainer?.ItemViewModel == null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
return UpdateExistingMailStateAsync(itemContainer, updatedState, mailUpdateSource);
|
||||
});
|
||||
|
||||
public Task UpdateMailStatesAsync(IEnumerable<MailStateChange> updatedStates, EntityUpdateSource mailUpdateSource)
|
||||
=> RunSerializedAsync(() => UpdateMailStatesInternalAsync(updatedStates, mailUpdateSource));
|
||||
|
||||
public Task UpdateMailCopiesAsync(IEnumerable<MailCopy> updatedMailCopies, EntityUpdateSource mailUpdateSource, MailCopyChangeFlags changedProperties = MailCopyChangeFlags.None)
|
||||
=> RunSerializedAsync(() => UpdateMailCopiesInternalAsync(updatedMailCopies, mailUpdateSource, changedProperties));
|
||||
|
||||
private async Task UpdateExistingMailStateAsync(MailItemContainer itemContainer, MailStateChange updatedState, EntityUpdateSource mailUpdateSource)
|
||||
{
|
||||
if (itemContainer?.ItemViewModel == null || updatedState == null)
|
||||
return;
|
||||
|
||||
var existingItem = itemContainer.ItemViewModel;
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
var appliedChanges = existingItem.ApplyStateChanges(updatedState.IsRead, updatedState.IsFlagged);
|
||||
existingItem.IsBusy = mailUpdateSource == EntityUpdateSource.ClientUpdated;
|
||||
|
||||
if (itemContainer.ThreadViewModel != null && appliedChanges != MailCopyChangeFlags.None)
|
||||
{
|
||||
itemContainer.ThreadViewModel.NotifyMailItemUpdated(existingItem, appliedChanges);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task UpdateMailStatesInternalAsync(IEnumerable<MailStateChange> updatedStates, EntityUpdateSource mailUpdateSource)
|
||||
{
|
||||
var updates = updatedStates?
|
||||
.Where(x => x != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group =>
|
||||
{
|
||||
var updatedState = group.Last();
|
||||
return new
|
||||
{
|
||||
UpdatedState = updatedState,
|
||||
ItemContainer = GetMailItemContainer(updatedState.UniqueId)
|
||||
};
|
||||
})
|
||||
.Where(x => x.ItemContainer?.ItemViewModel != null)
|
||||
.ToList() ?? [];
|
||||
|
||||
if (updates.Count == 0)
|
||||
return;
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
foreach (var update in updates)
|
||||
{
|
||||
var existingItem = update.ItemContainer.ItemViewModel;
|
||||
var appliedChanges = existingItem.ApplyStateChanges(update.UpdatedState.IsRead, update.UpdatedState.IsFlagged);
|
||||
existingItem.IsBusy = mailUpdateSource == EntityUpdateSource.ClientUpdated;
|
||||
|
||||
if (update.ItemContainer.ThreadViewModel != null && appliedChanges != MailCopyChangeFlags.None)
|
||||
{
|
||||
update.ItemContainer.ThreadViewModel.NotifyMailItemUpdated(existingItem, appliedChanges);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task UpdateMailCopiesInternalAsync(IEnumerable<MailCopy> updatedMailCopies, EntityUpdateSource mailUpdateSource, MailCopyChangeFlags changedProperties)
|
||||
{
|
||||
var updates = updatedMailCopies?
|
||||
.Where(x => x != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group =>
|
||||
{
|
||||
var updatedMail = group.First();
|
||||
return new
|
||||
{
|
||||
UpdatedMail = updatedMail,
|
||||
ItemContainer = GetMailItemContainer(updatedMail.UniqueId)
|
||||
};
|
||||
})
|
||||
.Where(x => x.ItemContainer?.ItemViewModel != null)
|
||||
.ToList() ?? [];
|
||||
|
||||
if (updates.Count == 0)
|
||||
return;
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
foreach (var update in updates)
|
||||
{
|
||||
var updatedMail = update.UpdatedMail;
|
||||
var itemContainer = update.ItemContainer;
|
||||
var existingItem = itemContainer.ItemViewModel;
|
||||
var appliedChanges = existingItem.UpdateFrom(updatedMail, changedProperties);
|
||||
existingItem.IsBusy = mailUpdateSource == EntityUpdateSource.ClientUpdated;
|
||||
|
||||
if (itemContainer.ThreadViewModel != null && appliedChanges != MailCopyChangeFlags.None)
|
||||
{
|
||||
itemContainer.ThreadViewModel.NotifyMailItemUpdated(existingItem, appliedChanges);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public MailItemViewModel GetFirst() => AllItems.ElementAtOrDefault(0);
|
||||
|
||||
public MailItemViewModel GetNextItem(MailCopy mailCopy)
|
||||
@@ -963,7 +1079,32 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
public Task RemoveAsync(MailCopy removeItem)
|
||||
=> RunSerializedAsync(() => RemoveInternalAsync(removeItem));
|
||||
|
||||
public Task RemoveRangeAsync(IEnumerable<MailCopy> removeItems)
|
||||
=> RunSerializedAsync(() => RemoveRangeInternalAsync(removeItems));
|
||||
|
||||
private async Task RemoveInternalAsync(MailCopy removeItem)
|
||||
=> await RemoveInternalAsync(removeItem, notifySelectionChanges: true);
|
||||
|
||||
private async Task RemoveRangeInternalAsync(IEnumerable<MailCopy> removeItems)
|
||||
{
|
||||
var distinctItems = removeItems?
|
||||
.Where(x => x != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group => group.First())
|
||||
.ToList() ?? [];
|
||||
|
||||
if (distinctItems.Count == 0)
|
||||
return;
|
||||
|
||||
foreach (var removeItem in distinctItems)
|
||||
{
|
||||
await RemoveInternalAsync(removeItem, notifySelectionChanges: false);
|
||||
}
|
||||
|
||||
await NotifySelectionChangesAsync();
|
||||
}
|
||||
|
||||
private async Task RemoveInternalAsync(MailCopy removeItem, bool notifySelectionChanges)
|
||||
{
|
||||
var itemContainer = GetMailItemContainer(removeItem.UniqueId);
|
||||
|
||||
@@ -1030,7 +1171,10 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
}
|
||||
}
|
||||
|
||||
await NotifySelectionChangesAsync();
|
||||
if (notifySelectionChanges)
|
||||
{
|
||||
await NotifySelectionChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<IMailListItem> AllItemsIncludingThreads
|
||||
@@ -1207,4 +1351,5 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
ItemSelectionChanged?.Invoke(this, null);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -320,6 +320,30 @@ public partial class MailItemViewModel(MailCopy mailCopy) : ObservableRecipient,
|
||||
return changedFlags;
|
||||
}
|
||||
|
||||
public MailCopyChangeFlags ApplyStateChanges(bool? isRead = null, bool? isFlagged = null)
|
||||
{
|
||||
var changedFlags = MailCopyChangeFlags.None;
|
||||
|
||||
if (isRead.HasValue && MailCopy.IsRead != isRead.Value)
|
||||
{
|
||||
MailCopy.IsRead = isRead.Value;
|
||||
changedFlags |= MailCopyChangeFlags.IsRead;
|
||||
}
|
||||
|
||||
if (isFlagged.HasValue && MailCopy.IsFlagged != isFlagged.Value)
|
||||
{
|
||||
MailCopy.IsFlagged = isFlagged.Value;
|
||||
changedFlags |= MailCopyChangeFlags.IsFlagged;
|
||||
}
|
||||
|
||||
if (changedFlags != MailCopyChangeFlags.None)
|
||||
{
|
||||
RaisePropertyChanges(changedFlags);
|
||||
}
|
||||
|
||||
return changedFlags;
|
||||
}
|
||||
|
||||
private static MailCopyChangeFlags SetIfChanged<T>(T currentValue, T newValue, Action<T> setter, MailCopyChangeFlags flag)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(currentValue, newValue))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using System.Collections.Generic;
|
||||
using Wino.Core.Domain.Entities.Mail;
|
||||
using Wino.Core.Domain.Entities.Shared;
|
||||
using Wino.Core.Domain.Enums;
|
||||
@@ -10,8 +11,13 @@ namespace Wino.Mail.ViewModels;
|
||||
|
||||
public class MailBaseViewModel : CoreBaseViewModel,
|
||||
IRecipient<MailAddedMessage>,
|
||||
IRecipient<BulkMailAddedMessage>,
|
||||
IRecipient<MailRemovedMessage>,
|
||||
IRecipient<BulkMailRemovedMessage>,
|
||||
IRecipient<MailStateUpdatedMessage>,
|
||||
IRecipient<BulkMailStateUpdatedMessage>,
|
||||
IRecipient<MailUpdatedMessage>,
|
||||
IRecipient<BulkMailUpdatedMessage>,
|
||||
IRecipient<MailDownloadedMessage>,
|
||||
IRecipient<DraftCreated>,
|
||||
IRecipient<DraftFailed>,
|
||||
@@ -21,8 +27,41 @@ public class MailBaseViewModel : CoreBaseViewModel,
|
||||
IRecipient<FolderSynchronizationEnabled>
|
||||
{
|
||||
protected virtual void OnMailAdded(MailCopy addedMail, EntityUpdateSource source) { }
|
||||
protected virtual void OnBulkMailAdded(IReadOnlyList<MailCopy> addedMails, EntityUpdateSource source)
|
||||
{
|
||||
foreach (var addedMail in addedMails ?? [])
|
||||
{
|
||||
OnMailAdded(addedMail, source);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnMailRemoved(MailCopy removedMail, EntityUpdateSource source) { }
|
||||
protected virtual void OnBulkMailRemoved(IReadOnlyList<MailCopy> removedMails, EntityUpdateSource source)
|
||||
{
|
||||
foreach (var removedMail in removedMails ?? [])
|
||||
{
|
||||
OnMailRemoved(removedMail, source);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnMailStateUpdated(MailStateChange updatedState, EntityUpdateSource source) { }
|
||||
protected virtual void OnBulkMailStateUpdated(IReadOnlyList<MailStateChange> updatedStates, EntityUpdateSource source)
|
||||
{
|
||||
foreach (var updatedState in updatedStates ?? [])
|
||||
{
|
||||
OnMailStateUpdated(updatedState, source);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnMailUpdated(MailCopy updatedMail, EntityUpdateSource source, MailCopyChangeFlags changedProperties) { }
|
||||
protected virtual void OnBulkMailUpdated(IReadOnlyList<MailCopy> updatedMails, EntityUpdateSource source, MailCopyChangeFlags changedProperties)
|
||||
{
|
||||
foreach (var updatedMail in updatedMails ?? [])
|
||||
{
|
||||
OnMailUpdated(updatedMail, source, changedProperties);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnMailDownloaded(MailCopy downloadedMail) { }
|
||||
protected virtual void OnDraftCreated(MailCopy draftMail, MailAccount account) { }
|
||||
protected virtual void OnDraftFailed(MailCopy draftMail, MailAccount account) { }
|
||||
@@ -32,8 +71,13 @@ public class MailBaseViewModel : CoreBaseViewModel,
|
||||
protected virtual void OnFolderSynchronizationEnabled(IMailItemFolder mailItemFolder) { }
|
||||
|
||||
void IRecipient<MailAddedMessage>.Receive(MailAddedMessage message) => OnMailAdded(message.AddedMail, message.Source);
|
||||
void IRecipient<BulkMailAddedMessage>.Receive(BulkMailAddedMessage message) => OnBulkMailAdded(message.AddedMails, message.Source);
|
||||
void IRecipient<MailRemovedMessage>.Receive(MailRemovedMessage message) => OnMailRemoved(message.RemovedMail, message.Source);
|
||||
void IRecipient<BulkMailRemovedMessage>.Receive(BulkMailRemovedMessage message) => OnBulkMailRemoved(message.RemovedMails, message.Source);
|
||||
void IRecipient<MailStateUpdatedMessage>.Receive(MailStateUpdatedMessage message) => OnMailStateUpdated(message.UpdatedState, message.Source);
|
||||
void IRecipient<BulkMailStateUpdatedMessage>.Receive(BulkMailStateUpdatedMessage message) => OnBulkMailStateUpdated(message.UpdatedStates, message.Source);
|
||||
void IRecipient<MailUpdatedMessage>.Receive(MailUpdatedMessage message) => OnMailUpdated(message.UpdatedMail, message.Source, message.ChangedProperties);
|
||||
void IRecipient<BulkMailUpdatedMessage>.Receive(BulkMailUpdatedMessage message) => OnBulkMailUpdated(message.UpdatedMails, message.Source, message.ChangedProperties);
|
||||
void IRecipient<MailDownloadedMessage>.Receive(MailDownloadedMessage message) => OnMailDownloaded(message.DownloadedMail);
|
||||
|
||||
void IRecipient<DraftMapped>.Receive(DraftMapped message) => OnDraftMapped(message.LocalDraftCopyId, message.RemoteDraftCopyId);
|
||||
@@ -51,8 +95,13 @@ public class MailBaseViewModel : CoreBaseViewModel,
|
||||
UnregisterRecipients();
|
||||
|
||||
Messenger.Register<MailAddedMessage>(this);
|
||||
Messenger.Register<BulkMailAddedMessage>(this);
|
||||
Messenger.Register<MailRemovedMessage>(this);
|
||||
Messenger.Register<BulkMailRemovedMessage>(this);
|
||||
Messenger.Register<MailStateUpdatedMessage>(this);
|
||||
Messenger.Register<BulkMailStateUpdatedMessage>(this);
|
||||
Messenger.Register<MailUpdatedMessage>(this);
|
||||
Messenger.Register<BulkMailUpdatedMessage>(this);
|
||||
Messenger.Register<MailDownloadedMessage>(this);
|
||||
Messenger.Register<DraftCreated>(this);
|
||||
Messenger.Register<DraftFailed>(this);
|
||||
@@ -67,8 +116,13 @@ public class MailBaseViewModel : CoreBaseViewModel,
|
||||
base.UnregisterRecipients();
|
||||
|
||||
Messenger.Unregister<MailAddedMessage>(this);
|
||||
Messenger.Unregister<BulkMailAddedMessage>(this);
|
||||
Messenger.Unregister<MailRemovedMessage>(this);
|
||||
Messenger.Unregister<BulkMailRemovedMessage>(this);
|
||||
Messenger.Unregister<MailStateUpdatedMessage>(this);
|
||||
Messenger.Unregister<BulkMailStateUpdatedMessage>(this);
|
||||
Messenger.Unregister<MailUpdatedMessage>(this);
|
||||
Messenger.Unregister<BulkMailUpdatedMessage>(this);
|
||||
Messenger.Unregister<MailDownloadedMessage>(this);
|
||||
Messenger.Unregister<DraftCreated>(this);
|
||||
Messenger.Unregister<DraftFailed>(this);
|
||||
|
||||
@@ -23,6 +23,7 @@ using Wino.Core.Domain.Models.MailItem;
|
||||
using Wino.Core.Domain.Models.Menus;
|
||||
using Wino.Core.Domain.Models.Navigation;
|
||||
using Wino.Core.Domain.Models.Reader;
|
||||
using Wino.Core.Domain.Models.Synchronization;
|
||||
using Wino.Core.Requests.Mail;
|
||||
using Wino.Core.Services;
|
||||
using Wino.Mail.ViewModels.Collections;
|
||||
@@ -77,6 +78,7 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
private readonly INotificationBuilder _notificationBuilder;
|
||||
private readonly IFolderService _folderService;
|
||||
private readonly IContextMenuItemService _contextMenuItemService;
|
||||
private readonly ILogger _logger = Log.ForContext<MailListPageViewModel>();
|
||||
private readonly IMailCategoryService _mailCategoryService;
|
||||
private readonly IWinoRequestDelegator _winoRequestDelegator;
|
||||
private readonly IKeyPressService _keyPressService;
|
||||
@@ -492,29 +494,29 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
{
|
||||
if (!CanSynchronize) return;
|
||||
|
||||
_notificationBuilder.CreateNotificationsAsync(MailCollection.SelectedItems.Select(a => a.MailCopy));
|
||||
return;
|
||||
//_notificationBuilder.CreateNotificationsAsync(MailCollection.SelectedItems.Select(a => a.MailCopy));
|
||||
//return;
|
||||
|
||||
// Only synchronize listed folders.
|
||||
|
||||
// When doing linked inbox sync, we need to save the sync id to report progress back only once.
|
||||
// Otherwise, we will report progress for each folder and that's what we don't want.
|
||||
|
||||
//trackingSynchronizationId = Guid.NewGuid();
|
||||
//completedTrackingSynchronizationCount = 0;
|
||||
trackingSynchronizationId = Guid.NewGuid();
|
||||
completedTrackingSynchronizationCount = 0;
|
||||
|
||||
//foreach (var folder in ActiveFolder.HandlingFolders)
|
||||
//{
|
||||
// var options = new MailSynchronizationOptions()
|
||||
// {
|
||||
// AccountId = folder.MailAccountId,
|
||||
// Type = MailSynchronizationType.CustomFolders,
|
||||
// SynchronizationFolderIds = [folder.Id],
|
||||
// GroupedSynchronizationTrackingId = trackingSynchronizationId
|
||||
// };
|
||||
foreach (var folder in ActiveFolder.HandlingFolders)
|
||||
{
|
||||
var options = new MailSynchronizationOptions()
|
||||
{
|
||||
AccountId = folder.MailAccountId,
|
||||
Type = MailSynchronizationType.CustomFolders,
|
||||
SynchronizationFolderIds = [folder.Id],
|
||||
GroupedSynchronizationTrackingId = trackingSynchronizationId
|
||||
};
|
||||
|
||||
// Messenger.Send(new NewMailSynchronizationRequested(options));
|
||||
//}
|
||||
Messenger.Send(new NewMailSynchronizationRequested(options));
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
@@ -749,7 +751,7 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
return;
|
||||
|
||||
var requests = new List<IRequestBase>();
|
||||
foreach (var mailItem in targetList.Select(a => a.MailCopy).DistinctBy(a => a.UniqueId))
|
||||
foreach (var mailItem in targetList.Select(a => a.MailCopy).GroupBy(a => a.UniqueId).Select(group => group.First()))
|
||||
{
|
||||
var categoryNames = await _mailCategoryService.GetCategoryNamesForMailAsync(mailItem.UniqueId).ConfigureAwait(false);
|
||||
requests.Add(new MailCategoryAssignmentRequest(mailItem, category.Id, category.Name, categoryNames, !isAssignedToAll));
|
||||
@@ -958,7 +960,6 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
protected override async void OnMailUpdated(MailCopy updatedMail, EntityUpdateSource source, MailCopyChangeFlags changedProperties)
|
||||
{
|
||||
base.OnMailUpdated(updatedMail, source, changedProperties);
|
||||
|
||||
try
|
||||
{
|
||||
await listManipulationSemepahore.WaitAsync();
|
||||
@@ -983,6 +984,115 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
await ExecuteUIThread(() => { SetupTopBarActions(); });
|
||||
}
|
||||
|
||||
protected override async void OnMailStateUpdated(MailStateChange updatedState, EntityUpdateSource source)
|
||||
{
|
||||
base.OnMailStateUpdated(updatedState, source);
|
||||
|
||||
if (updatedState == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await listManipulationSemepahore.WaitAsync();
|
||||
|
||||
if (!MailCollection.ContainsMailUniqueId(updatedState.UniqueId))
|
||||
return;
|
||||
|
||||
await MailCollection.UpdateMailStateAsync(updatedState, source);
|
||||
}
|
||||
finally
|
||||
{
|
||||
listManipulationSemepahore.Release();
|
||||
}
|
||||
|
||||
await ExecuteUIThread(() => { SetupTopBarActions(); });
|
||||
}
|
||||
|
||||
protected override async void OnBulkMailStateUpdated(IReadOnlyList<MailStateChange> updatedStates, EntityUpdateSource source)
|
||||
{
|
||||
var targetStates = updatedStates?
|
||||
.Where(x => x != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group => group.Last())
|
||||
.ToList() ?? [];
|
||||
|
||||
if (targetStates.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await listManipulationSemepahore.WaitAsync();
|
||||
|
||||
var listedStates = targetStates
|
||||
.Where(state => MailCollection.ContainsMailUniqueId(state.UniqueId))
|
||||
.ToList();
|
||||
|
||||
if (listedStates.Count == 0)
|
||||
return;
|
||||
|
||||
await MailCollection.UpdateMailStatesAsync(listedStates, source);
|
||||
}
|
||||
finally
|
||||
{
|
||||
listManipulationSemepahore.Release();
|
||||
}
|
||||
|
||||
await ExecuteUIThread(() => { SetupTopBarActions(); });
|
||||
}
|
||||
|
||||
protected override async void OnBulkMailUpdated(IReadOnlyList<MailCopy> updatedMails, EntityUpdateSource source, MailCopyChangeFlags changedProperties)
|
||||
{
|
||||
var targetMails = updatedMails?
|
||||
.Where(x => x != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group => group.First())
|
||||
.ToList() ?? [];
|
||||
|
||||
if (targetMails.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await listManipulationSemepahore.WaitAsync();
|
||||
|
||||
var listedMails = targetMails
|
||||
.Where(mail => MailCollection.ContainsMailUniqueId(mail.UniqueId))
|
||||
.ToList();
|
||||
|
||||
if (listedMails.Count == 0)
|
||||
return;
|
||||
|
||||
var mailsToRemove = listedMails
|
||||
.Where(ShouldRemoveUpdatedMailFromCurrentList)
|
||||
.ToList();
|
||||
|
||||
var mailIdsToRemove = mailsToRemove.Select(x => x.UniqueId).ToHashSet();
|
||||
var mailsToUpdate = listedMails
|
||||
.Where(mail => !mailIdsToRemove.Contains(mail.UniqueId))
|
||||
.ToList();
|
||||
|
||||
if (mailsToRemove.Count > 0)
|
||||
{
|
||||
await MailCollection.RemoveRangeAsync(mailsToRemove);
|
||||
}
|
||||
|
||||
if (mailsToUpdate.Count > 0)
|
||||
{
|
||||
await MailCollection.UpdateMailCopiesAsync(mailsToUpdate, source, changedProperties);
|
||||
}
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
NotifyItemFoundState();
|
||||
SetupTopBarActions();
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
listManipulationSemepahore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected override async void OnMailRemoved(MailCopy removedMail, EntityUpdateSource source)
|
||||
{
|
||||
base.OnMailRemoved(removedMail, source);
|
||||
@@ -1003,18 +1113,18 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
|
||||
if (removedItemExistsInCurrentList && !isDeletedByGmailUnreadFolderAction)
|
||||
{
|
||||
bool isDeletedMailSelected = MailCollection.SelectedItems.Any(a => a.MailCopy.UniqueId == removedMail.UniqueId);
|
||||
|
||||
// Automatically select the next item in the list if the setting is enabled.
|
||||
MailItemViewModel nextItem = null;
|
||||
bool isDeletedMailSelected = false;
|
||||
|
||||
if (isDeletedMailSelected && PreferencesService.AutoSelectNextItem)
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
await ExecuteUIThread(() =>
|
||||
isDeletedMailSelected = MailCollection.SelectedItems.Any(a => a.MailCopy.UniqueId == removedMail.UniqueId);
|
||||
|
||||
if (isDeletedMailSelected && PreferencesService.AutoSelectNextItem)
|
||||
{
|
||||
nextItem = MailCollection.GetNextItem(removedMail);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// RemoveAsync already handles UI threading internally
|
||||
await MailCollection.RemoveAsync(removedMail);
|
||||
@@ -1044,6 +1154,115 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
}
|
||||
}
|
||||
|
||||
protected override async void OnBulkMailRemoved(IReadOnlyList<MailCopy> removedMails, EntityUpdateSource source)
|
||||
{
|
||||
var targetMails = removedMails?
|
||||
.Where(x => x != null && x.AssignedAccount != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group => group.First())
|
||||
.ToList() ?? [];
|
||||
|
||||
if (targetMails.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await listManipulationSemepahore.WaitAsync();
|
||||
|
||||
var existingMails = targetMails
|
||||
.Where(mail => MailCollection.ContainsMailUniqueId(mail.UniqueId))
|
||||
.ToList();
|
||||
|
||||
if (existingMails.Count == 0)
|
||||
return;
|
||||
|
||||
var removedMailIds = existingMails.Select(mail => mail.UniqueId).ToHashSet();
|
||||
var shouldClearSelection = false;
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
shouldClearSelection = MailCollection.SelectedItems.Any(item => removedMailIds.Contains(item.MailCopy.UniqueId));
|
||||
});
|
||||
|
||||
await MailCollection.RemoveRangeAsync(existingMails);
|
||||
|
||||
if (shouldClearSelection)
|
||||
{
|
||||
await MailCollection.UnselectAllAsync();
|
||||
}
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
NotifyItemFoundState();
|
||||
SetupTopBarActions();
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
listManipulationSemepahore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected override async void OnBulkMailAdded(IReadOnlyList<MailCopy> addedMails, EntityUpdateSource source)
|
||||
{
|
||||
var targetMails = addedMails?
|
||||
.Where(x => x != null)
|
||||
.GroupBy(x => x.UniqueId)
|
||||
.Select(group => group.First())
|
||||
.ToList() ?? [];
|
||||
|
||||
if (targetMails.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await listManipulationSemepahore.WaitAsync();
|
||||
|
||||
var mailsToAdd = new List<MailCopy>();
|
||||
|
||||
foreach (var addedMail in targetMails)
|
||||
{
|
||||
if (MailCollection.ContainsMailUniqueId(addedMail.UniqueId))
|
||||
continue;
|
||||
|
||||
if (!ShouldIncludeAddedMailInCurrentList(addedMail))
|
||||
continue;
|
||||
|
||||
if (ShouldPreventItemAdd(addedMail))
|
||||
continue;
|
||||
|
||||
if (SelectedFolderPivot?.IsFocused is bool isFocused && addedMail.IsFocused != isFocused)
|
||||
continue;
|
||||
|
||||
if (IsInSearchMode)
|
||||
{
|
||||
if (IsOnlineSearchEnabled || AreSearchResultsOnline)
|
||||
continue;
|
||||
|
||||
if (!IsMailMatchingLocalSearch(addedMail))
|
||||
continue;
|
||||
}
|
||||
|
||||
mailsToAdd.Add(addedMail);
|
||||
}
|
||||
|
||||
if (mailsToAdd.Count == 0)
|
||||
return;
|
||||
|
||||
await MailCollection.AddRangeAsync(mailsToAdd.Select(mail => new MailItemViewModel(mail)), false);
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
NotifyItemFoundState();
|
||||
SetupTopBarActions();
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
listManipulationSemepahore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected override async void OnFolderDeleted(MailItemFolder folder)
|
||||
{
|
||||
base.OnFolderDeleted(folder);
|
||||
@@ -1648,4 +1867,5 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
var package = new MailOperationPreperationRequest(message.Operation, mailCopies);
|
||||
await ExecuteMailOperationAsync(package);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -667,6 +667,23 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
|
||||
await ExecuteUIThread(() => { InitializeCommandBarItems(); });
|
||||
}
|
||||
|
||||
protected override async void OnMailStateUpdated(MailStateChange updatedState, EntityUpdateSource source)
|
||||
{
|
||||
base.OnMailStateUpdated(updatedState, source);
|
||||
|
||||
if (initializedMailItemViewModel == null || updatedState == null)
|
||||
return;
|
||||
|
||||
if (initializedMailItemViewModel.MailCopy.UniqueId != updatedState.UniqueId)
|
||||
return;
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
initializedMailItemViewModel.ApplyStateChanges(updatedState.IsRead, updatedState.IsFlagged);
|
||||
InitializeCommandBarItems();
|
||||
});
|
||||
}
|
||||
|
||||
protected override async void OnMailRemoved(MailCopy removedMail, EntityUpdateSource source)
|
||||
{
|
||||
base.OnMailRemoved(removedMail, source);
|
||||
|
||||
Reference in New Issue
Block a user