Intercepting containers for threads.
This commit is contained in:
@@ -23,6 +23,9 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
|
||||
public HashSet<Guid> MailCopyIdHashSet = [];
|
||||
|
||||
// Cache ThreadIds to quickly find items that should be threaded together
|
||||
private readonly Dictionary<string, List<IMailListItem>> _threadIdToItemsMap = new();
|
||||
|
||||
public event EventHandler<MailItemViewModel> MailItemRemoved;
|
||||
public event EventHandler ItemSelectionChanged;
|
||||
|
||||
@@ -78,6 +81,7 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
{
|
||||
_mailItemSource.Clear();
|
||||
MailCopyIdHashSet.Clear();
|
||||
_threadIdToItemsMap.Clear();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -104,9 +108,70 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateThreadIdCache(IMailListItem item, bool isAdd)
|
||||
{
|
||||
var threadIds = GetThreadIdsFromItem(item);
|
||||
|
||||
foreach (var threadId in threadIds)
|
||||
{
|
||||
if (string.IsNullOrEmpty(threadId)) continue;
|
||||
|
||||
if (isAdd)
|
||||
{
|
||||
if (!_threadIdToItemsMap.ContainsKey(threadId))
|
||||
{
|
||||
_threadIdToItemsMap[threadId] = new List<IMailListItem>();
|
||||
}
|
||||
_threadIdToItemsMap[threadId].Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_threadIdToItemsMap.ContainsKey(threadId))
|
||||
{
|
||||
_threadIdToItemsMap[threadId].Remove(item);
|
||||
if (_threadIdToItemsMap[threadId].Count == 0)
|
||||
{
|
||||
_threadIdToItemsMap.Remove(threadId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetThreadIdsFromItem(IMailListItem item)
|
||||
{
|
||||
if (item is MailItemViewModel mailItem && !string.IsNullOrEmpty(mailItem.MailCopy.ThreadId))
|
||||
{
|
||||
yield return mailItem.MailCopy.ThreadId;
|
||||
}
|
||||
else if (item is ThreadMailItemViewModel threadItem)
|
||||
{
|
||||
var uniqueThreadIds = threadItem.ThreadEmails
|
||||
.Where(e => !string.IsNullOrEmpty(e.MailCopy.ThreadId))
|
||||
.Select(e => e.MailCopy.ThreadId)
|
||||
.Distinct();
|
||||
|
||||
foreach (var threadId in uniqueThreadIds)
|
||||
{
|
||||
yield return threadId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IMailListItem FindThreadableItem(string threadId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(threadId) || !_threadIdToItemsMap.ContainsKey(threadId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return _threadIdToItemsMap[threadId].FirstOrDefault();
|
||||
}
|
||||
|
||||
private async Task InsertItemInternalAsync(object groupKey, IMailListItem mailItem)
|
||||
{
|
||||
UpdateUniqueIdHashes(mailItem, true);
|
||||
UpdateThreadIdCache(mailItem, true);
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
_mailItemSource.InsertItem(groupKey, listComparer, mailItem, listComparer);
|
||||
@@ -116,6 +181,7 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
private async Task RemoveItemInternalAsync(ObservableGroup<object, IMailListItem> group, IMailListItem mailItem)
|
||||
{
|
||||
UpdateUniqueIdHashes(mailItem, false);
|
||||
UpdateThreadIdCache(mailItem, false);
|
||||
|
||||
if (mailItem is MailItemViewModel singleMailItem)
|
||||
{
|
||||
@@ -156,12 +222,18 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
{
|
||||
var existingGroupKey = GetGroupingKey(threadViewModel);
|
||||
|
||||
// Update ThreadId cache before modifying the thread
|
||||
UpdateThreadIdCache(threadViewModel, false);
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
var newMailItem = new MailItemViewModel(addedItem);
|
||||
threadViewModel.AddEmail(newMailItem);
|
||||
});
|
||||
|
||||
// Update ThreadId cache after modifying the thread
|
||||
UpdateThreadIdCache(threadViewModel, true);
|
||||
|
||||
var newGroupKey = GetGroupingKey(threadViewModel);
|
||||
|
||||
if (!existingGroupKey.Equals(newGroupKey))
|
||||
@@ -212,41 +284,50 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
|
||||
public async Task AddAsync(MailCopy addedItem)
|
||||
{
|
||||
foreach (var group in _mailItemSource)
|
||||
// First check if this is an update to an existing item
|
||||
if (MailCopyIdHashSet.Contains(addedItem.UniqueId))
|
||||
{
|
||||
foreach (var item in group)
|
||||
// Find and update the existing item
|
||||
var existingItemContainer = GetMailItemContainer(addedItem.UniqueId);
|
||||
if (existingItemContainer?.ItemViewModel != null)
|
||||
{
|
||||
// Compare ThreadIds - if they match and both have ThreadIds, thread them together
|
||||
bool shouldThread = !string.IsNullOrEmpty(addedItem.ThreadId) &&
|
||||
item is MailItemViewModel mailItem &&
|
||||
!string.IsNullOrEmpty(mailItem.MailCopy.ThreadId) &&
|
||||
string.Equals(addedItem.ThreadId, mailItem.MailCopy.ThreadId, StringComparison.OrdinalIgnoreCase);
|
||||
await UpdateExistingItemAsync(existingItemContainer.ItemViewModel, addedItem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!shouldThread && item is ThreadMailItemViewModel threadViewModel)
|
||||
// Check if this item should be threaded with an existing item
|
||||
if (!string.IsNullOrEmpty(addedItem.ThreadId))
|
||||
{
|
||||
var threadableItem = FindThreadableItem(addedItem.ThreadId);
|
||||
if (threadableItem != null)
|
||||
{
|
||||
// Find the group containing this item
|
||||
var targetGroup = FindGroupContainingItem(threadableItem);
|
||||
if (targetGroup != null)
|
||||
{
|
||||
// Check if any email in the thread has matching ThreadId
|
||||
shouldThread = !string.IsNullOrEmpty(addedItem.ThreadId) &&
|
||||
threadViewModel.ThreadEmails.Any(e =>
|
||||
!string.IsNullOrEmpty(e.MailCopy.ThreadId) &&
|
||||
string.Equals(addedItem.ThreadId, e.MailCopy.ThreadId, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
if (shouldThread)
|
||||
{
|
||||
await HandleThreadingAsync(group, item, addedItem);
|
||||
return;
|
||||
}
|
||||
else if (item is MailItemViewModel itemViewModel && itemViewModel.MailCopy.UniqueId == addedItem.UniqueId)
|
||||
{
|
||||
await UpdateExistingItemAsync(itemViewModel, addedItem);
|
||||
await HandleThreadingAsync(targetGroup, threadableItem, addedItem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No threading needed, add as new item
|
||||
await AddNewItemAsync(addedItem);
|
||||
}
|
||||
|
||||
private ObservableGroup<object, IMailListItem> FindGroupContainingItem(IMailListItem item)
|
||||
{
|
||||
foreach (var group in _mailItemSource)
|
||||
{
|
||||
if (group.Contains(item))
|
||||
{
|
||||
return group;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task AddNewItemAsync(MailCopy addedItem)
|
||||
{
|
||||
var newMailItem = new MailItemViewModel(addedItem);
|
||||
@@ -265,25 +346,103 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
/// <summary>
|
||||
/// Adds multiple emails to the collection.
|
||||
/// </summary>
|
||||
public async Task AddRangeAsync(IEnumerable<IMailListItem> items, bool clearIdCache)
|
||||
public async Task AddRangeAsync(IEnumerable<MailItemViewModel> items, bool clearIdCache)
|
||||
{
|
||||
if (clearIdCache)
|
||||
{
|
||||
MailCopyIdHashSet.Clear();
|
||||
_threadIdToItemsMap.Clear();
|
||||
}
|
||||
|
||||
var groupedByName = items
|
||||
.GroupBy(GetGroupingKey)
|
||||
.Select(a => new ObservableGroup<object, IMailListItem>(a.Key, a));
|
||||
var itemsList = items.ToList();
|
||||
var itemsToAdd = new List<IMailListItem>();
|
||||
var processedItems = new HashSet<MailItemViewModel>();
|
||||
|
||||
// Process items and handle threading
|
||||
foreach (var item in itemsList)
|
||||
{
|
||||
if (processedItems.Contains(item))
|
||||
continue;
|
||||
|
||||
// Check if this is an update to an existing item
|
||||
if (MailCopyIdHashSet.Contains(item.MailCopy.UniqueId))
|
||||
{
|
||||
var existingItemContainer = GetMailItemContainer(item.MailCopy.UniqueId);
|
||||
if (existingItemContainer?.ItemViewModel != null)
|
||||
{
|
||||
await UpdateExistingItemAsync(existingItemContainer.ItemViewModel, item.MailCopy);
|
||||
processedItems.Add(item);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this item should be threaded
|
||||
if (!string.IsNullOrEmpty(item.MailCopy.ThreadId))
|
||||
{
|
||||
// Look for existing item with same ThreadId
|
||||
var existingThreadableItem = FindThreadableItem(item.MailCopy.ThreadId);
|
||||
|
||||
if (existingThreadableItem != null)
|
||||
{
|
||||
// Thread with existing item
|
||||
var targetGroup = FindGroupContainingItem(existingThreadableItem);
|
||||
if (targetGroup != null)
|
||||
{
|
||||
await HandleThreadingAsync(targetGroup, existingThreadableItem, item.MailCopy);
|
||||
processedItems.Add(item);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for other items in the current batch with same ThreadId
|
||||
var threadableItems = itemsList
|
||||
.Where(i => !processedItems.Contains(i) &&
|
||||
!string.IsNullOrEmpty(i.MailCopy.ThreadId) &&
|
||||
i.MailCopy.ThreadId == item.MailCopy.ThreadId)
|
||||
.ToList();
|
||||
|
||||
if (threadableItems.Count > 1)
|
||||
{
|
||||
// Create a new thread with all matching items
|
||||
var threadViewModel = new ThreadMailItemViewModel(item.MailCopy.ThreadId);
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
foreach (var threadItem in threadableItems)
|
||||
{
|
||||
threadViewModel.AddEmail(threadItem);
|
||||
}
|
||||
});
|
||||
|
||||
itemsToAdd.Add(threadViewModel);
|
||||
|
||||
// Mark all threaded items as processed
|
||||
foreach (var threadItem in threadableItems)
|
||||
{
|
||||
processedItems.Add(threadItem);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// No threading needed, add as single item
|
||||
itemsToAdd.Add(item);
|
||||
processedItems.Add(item);
|
||||
}
|
||||
|
||||
// Group items by their grouping key and add them
|
||||
var groupedItems = itemsToAdd
|
||||
.GroupBy(GetGroupingKey)
|
||||
.Select(g => new ObservableGroup<object, IMailListItem>(g.Key, g));
|
||||
|
||||
await ExecuteUIThread(() =>
|
||||
{
|
||||
foreach (var group in groupedByName)
|
||||
foreach (var group in groupedItems)
|
||||
{
|
||||
// Store all mail copy ids for faster access.
|
||||
foreach (var item in group)
|
||||
{
|
||||
UpdateUniqueIdHashes(item, true);
|
||||
UpdateThreadIdCache(item, true);
|
||||
}
|
||||
|
||||
var existingGroup = _mailItemSource.FirstGroupByKeyOrDefault(group.Key);
|
||||
@@ -497,8 +656,17 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
|
||||
var oldGroupKey = GetGroupingKey(threadMailItemViewModel);
|
||||
|
||||
// Update ThreadId cache before modifying the thread
|
||||
UpdateThreadIdCache(threadMailItemViewModel, false);
|
||||
|
||||
await ExecuteUIThread(() => { threadMailItemViewModel.RemoveEmail(removalItem); });
|
||||
|
||||
// Update ThreadId cache after modifying the thread
|
||||
if (threadMailItemViewModel.EmailCount > 0)
|
||||
{
|
||||
UpdateThreadIdCache(threadMailItemViewModel, true);
|
||||
}
|
||||
|
||||
if (threadMailItemViewModel.EmailCount == 1)
|
||||
{
|
||||
// Convert to single item.
|
||||
@@ -559,8 +727,6 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
{
|
||||
foreach (var item in group)
|
||||
{
|
||||
if (item is not MailItemViewModel mailItemViewModel) throw new Exception("Item is not MailItemViewModel in AllItems");
|
||||
|
||||
if (item is ThreadMailItemViewModel threadMail)
|
||||
{
|
||||
foreach (var singleItem in threadMail.ThreadEmails)
|
||||
@@ -568,8 +734,8 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
yield return singleItem;
|
||||
}
|
||||
}
|
||||
|
||||
yield return mailItemViewModel;
|
||||
else if (item is MailItemViewModel mailItemViewModel)
|
||||
yield return mailItemViewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Wino.Core.Domain.Entities.Mail;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
|
||||
namespace Wino.Mail.ViewModels.Data;
|
||||
|
||||
@@ -90,6 +89,10 @@ public partial class MailItemViewModel(MailCopy mailCopy) : ObservableObject, IM
|
||||
set => SetProperty(MailCopy.HasAttachments, value, MailCopy, (u, n) => u.HasAttachments = n);
|
||||
}
|
||||
|
||||
partial void OnIsSelectedChanged(bool oldValue, bool newValue)
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerable<Guid> GetContainingIds() => [MailCopy.UniqueId];
|
||||
|
||||
public IEnumerable<MailItemViewModel> GetSelectedMailItems()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
|
||||
namespace Wino.Mail.ViewModels.Data;
|
||||
|
||||
@@ -13,7 +13,6 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
{
|
||||
private readonly string _threadId;
|
||||
|
||||
private readonly List<MailItemViewModel> _threadEmails = [];
|
||||
private bool _disposed;
|
||||
|
||||
[ObservableProperty]
|
||||
@@ -26,35 +25,37 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
/// <summary>
|
||||
/// Gets the number of emails in this thread
|
||||
/// </summary>
|
||||
public int EmailCount => _threadEmails.Count;
|
||||
public int EmailCount => ThreadEmails.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the latest email's subject for display
|
||||
/// </summary>
|
||||
public string Subject => _threadEmails
|
||||
public string Subject => ThreadEmails
|
||||
.OrderByDescending(e => e.MailCopy?.CreationDate)
|
||||
.FirstOrDefault()?.MailCopy?.Subject;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the latest email's sender name for display
|
||||
/// </summary>
|
||||
public string FromName => _threadEmails
|
||||
public string FromName => ThreadEmails
|
||||
.OrderByDescending(e => e.MailCopy?.CreationDate)
|
||||
.FirstOrDefault()?.MailCopy?.SenderContact.Name;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the latest email's creation date for sorting
|
||||
/// </summary>
|
||||
public DateTime CreationDate => _threadEmails
|
||||
public DateTime CreationDate => ThreadEmails
|
||||
.OrderByDescending(e => e.MailCopy?.CreationDate)
|
||||
.FirstOrDefault()?.MailCopy?.CreationDate ?? DateTime.MinValue;
|
||||
|
||||
/// <summary>
|
||||
/// Gets all emails in this thread (read-only)
|
||||
/// Gets all emails in this thread (observable)
|
||||
/// </summary>
|
||||
public IReadOnlyList<MailItemViewModel> ThreadEmails => _threadEmails.AsReadOnly();
|
||||
///
|
||||
[ObservableProperty]
|
||||
public partial ObservableCollection<MailItemViewModel> ThreadEmails { get; set; } = [];
|
||||
|
||||
public MailItemViewModel LatestMailViewModel => _threadEmails.OrderByDescending(e => e.MailCopy?.CreationDate).FirstOrDefault()!;
|
||||
public MailItemViewModel LatestMailViewModel => ThreadEmails.OrderByDescending(e => e.MailCopy?.CreationDate).FirstOrDefault()!;
|
||||
|
||||
public ThreadMailItemViewModel(string threadId)
|
||||
{
|
||||
@@ -68,7 +69,7 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_threadEmails.Clear();
|
||||
ThreadEmails.Clear();
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
@@ -86,6 +87,7 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
OnPropertyChanged(nameof(FromName));
|
||||
OnPropertyChanged(nameof(CreationDate));
|
||||
OnPropertyChanged(nameof(LatestMailViewModel));
|
||||
OnPropertyChanged(nameof(ThreadEmails));
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +99,7 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
if (email.MailCopy.ThreadId != _threadId)
|
||||
throw new ArgumentException($"Email ThreadId '{email.MailCopy.ThreadId}' does not match expander ThreadId '{_threadId}'");
|
||||
|
||||
_threadEmails.Add(email);
|
||||
ThreadEmails.Add(email);
|
||||
NotifyPropertyChanges();
|
||||
}
|
||||
|
||||
@@ -106,7 +108,7 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
/// </summary>
|
||||
public void RemoveEmail(MailItemViewModel email)
|
||||
{
|
||||
if (_threadEmails.Remove(email))
|
||||
if (ThreadEmails.Remove(email))
|
||||
{
|
||||
NotifyPropertyChanges();
|
||||
}
|
||||
@@ -115,7 +117,7 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable,
|
||||
/// <summary>
|
||||
/// Checks if this thread contains an email with the specified unique ID
|
||||
/// </summary>
|
||||
public bool HasUniqueId(Guid uniqueId) => _threadEmails.Any(email => email.MailCopy.UniqueId == uniqueId);
|
||||
public bool HasUniqueId(Guid uniqueId) => ThreadEmails.Any(email => email.MailCopy.UniqueId == uniqueId);
|
||||
|
||||
public IEnumerable<Guid> GetContainingIds() => ThreadEmails.Select(a => a.MailCopy.UniqueId);
|
||||
|
||||
|
||||
@@ -753,7 +753,7 @@ public partial class MailListPageViewModel : MailBaseViewModel,
|
||||
|
||||
try
|
||||
{
|
||||
_ = MailCollection.ClearAsync();
|
||||
await MailCollection.ClearAsync();
|
||||
|
||||
if (ActiveFolder == null)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user