Prevent delay between mail addings and better folder init cancellation.

This commit is contained in:
Burak Kaan Köse
2024-08-25 02:01:08 +02:00
parent c8fce82dc1
commit d0b54ea44b
3 changed files with 30 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MimeKit;
using Wino.Core.Domain.Entities; using Wino.Core.Domain.Entities;
using Wino.Core.Domain.Models.MailItem; using Wino.Core.Domain.Models.MailItem;
@@ -11,7 +11,7 @@ namespace Wino.Core.Domain.Interfaces
{ {
Task<MailCopy> GetSingleMailItemAsync(string mailCopyId, string remoteFolderId); Task<MailCopy> GetSingleMailItemAsync(string mailCopyId, string remoteFolderId);
Task<MailCopy> GetSingleMailItemAsync(Guid uniqueMailId); Task<MailCopy> GetSingleMailItemAsync(Guid uniqueMailId);
Task<List<IMailItem>> FetchMailsAsync(MailListInitializationOptions options); Task<List<IMailItem>> FetchMailsAsync(MailListInitializationOptions options, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Deletes all mail copies for all folders. /// Deletes all mail copies for all folders.

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Kiota.Abstractions.Extensions; using Microsoft.Kiota.Abstractions.Extensions;
using MimeKit; using MimeKit;
@@ -193,7 +194,7 @@ namespace Wino.Core.Services
return query.GetRawQuery(); return query.GetRawQuery();
} }
public async Task<List<IMailItem>> FetchMailsAsync(MailListInitializationOptions options) public async Task<List<IMailItem>> FetchMailsAsync(MailListInitializationOptions options, CancellationToken cancellationToken = default)
{ {
var query = BuildMailFetchQuery(options); var query = BuildMailFetchQuery(options);
@@ -216,6 +217,8 @@ namespace Wino.Core.Services
if (!options.CreateThreads) if (!options.CreateThreads)
{ {
cancellationToken.ThrowIfCancellationRequested();
// Threading is disabled. Just return everything as it is. // Threading is disabled. Just return everything as it is.
mails.Sort(options.SortingOptionType == SortingOptionType.ReceiveDate ? new DateComparer() : new NameComparer()); mails.Sort(options.SortingOptionType == SortingOptionType.ReceiveDate ? new DateComparer() : new NameComparer());
@@ -229,6 +232,8 @@ namespace Wino.Core.Services
// Each account items must be threaded separately. // Each account items must be threaded separately.
foreach (var group in mails.GroupBy(a => a.AssignedAccount.Id)) foreach (var group in mails.GroupBy(a => a.AssignedAccount.Id))
{ {
cancellationToken.ThrowIfCancellationRequested();
var accountId = group.Key; var accountId = group.Key;
var groupAccount = mails.First(a => a.AssignedAccount.Id == accountId).AssignedAccount; var groupAccount = mails.First(a => a.AssignedAccount.Id == accountId).AssignedAccount;
@@ -242,6 +247,7 @@ namespace Wino.Core.Services
// Almost everything already should be in cache from initial population. // Almost everything already should be in cache from initial population.
foreach (var mail in accountThreadedItems) foreach (var mail in accountThreadedItems)
{ {
cancellationToken.ThrowIfCancellationRequested();
await LoadAssignedPropertiesWithCacheAsync(mail, folderCache, accountCache).ConfigureAwait(false); await LoadAssignedPropertiesWithCacheAsync(mail, folderCache, accountCache).ConfigureAwait(false);
} }
@@ -252,6 +258,7 @@ namespace Wino.Core.Services
} }
threadedItems.Sort(options.SortingOptionType == SortingOptionType.ReceiveDate ? new DateComparer() : new NameComparer()); threadedItems.Sort(options.SortingOptionType == SortingOptionType.ReceiveDate ? new DateComparer() : new NameComparer());
cancellationToken.ThrowIfCancellationRequested();
return threadedItems; return threadedItems;

View File

@@ -614,8 +614,6 @@ namespace Wino.Mail.ViewModels
try try
{ {
await listManipulationSemepahore.WaitAsync();
if (ActiveFolder == null) return; if (ActiveFolder == null) return;
// At least accounts must match. // At least accounts must match.
@@ -631,6 +629,8 @@ namespace Wino.Mail.ViewModels
// Item should be prevented from being added to the list due to filter. // Item should be prevented from being added to the list due to filter.
if (!shouldPreventIgnoringFilter && ShouldPreventItemAdd(addedMail)) return; if (!shouldPreventIgnoringFilter && ShouldPreventItemAdd(addedMail)) return;
await listManipulationSemepahore.WaitAsync();
await MailCollection.AddAsync(addedMail); await MailCollection.AddAsync(addedMail);
await ExecuteUIThread(() => { NotifyItemFoundState(); }); await ExecuteUIThread(() => { NotifyItemFoundState(); });
@@ -762,12 +762,17 @@ namespace Wino.Mail.ViewModels
// Folder is changed during initialization. // Folder is changed during initialization.
// Just cancel the existing one and wait for new initialization. // Just cancel the existing one and wait for new initialization.
if (listManipulationSemepahore.CurrentCount == 0) //if (listManipulationSemepahore.CurrentCount == 0)
{ //{
Debug.WriteLine("Canceling initialization of mails."); // Debug.WriteLine("Canceling initialization of mails.");
// listManipulationCancellationTokenSource.Cancel();
// listManipulationCancellationTokenSource.Token.ThrowIfCancellationRequested();
//}
if (!listManipulationCancellationTokenSource.IsCancellationRequested)
{
listManipulationCancellationTokenSource.Cancel(); listManipulationCancellationTokenSource.Cancel();
listManipulationCancellationTokenSource.Token.ThrowIfCancellationRequested();
} }
listManipulationCancellationTokenSource = new CancellationTokenSource(); listManipulationCancellationTokenSource = new CancellationTokenSource();
@@ -794,15 +799,18 @@ namespace Wino.Mail.ViewModels
SearchQuery, SearchQuery,
MailCollection.MailCopyIdHashSet); MailCollection.MailCopyIdHashSet);
var items = await _mailService.FetchMailsAsync(initializationOptions).ConfigureAwait(false); var items = await _mailService.FetchMailsAsync(initializationOptions, cancellationToken).ConfigureAwait(false);
// Here they are already threaded if needed. if (!listManipulationCancellationTokenSource.IsCancellationRequested)
// We don't need to insert them one by one. {
// Just create VMs and do bulk insert. // Here they are already threaded if needed.
// We don't need to insert them one by one.
// Just create VMs and do bulk insert.
var viewModels = PrepareMailViewModels(items); var viewModels = PrepareMailViewModels(items);
await ExecuteUIThread(() => { MailCollection.AddRange(viewModels, true); }); await ExecuteUIThread(() => { MailCollection.AddRange(viewModels, true); });
}
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {