Intercepting containers for threads.

This commit is contained in:
Burak Kaan Köse
2025-10-26 23:35:09 +01:00
parent 79d5b6ed40
commit d9fc365aeb
12 changed files with 528 additions and 175 deletions
@@ -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;
@@ -1,4 +1,5 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Wino.Mail.ViewModels.Data;
namespace Wino.Mail.WinUI.Controls.ListView;
@@ -7,34 +8,70 @@ public partial class WinoListView : Microsoft.UI.Xaml.Controls.ListView
{
public bool IsAllSelected => Items.Count == SelectedItems.Count;
protected override DependencyObject GetContainerForItemOverride() => new WinoListViewItem();
public WinoListView()
{
ChoosingItemContainer += WinoListView_ChoosingItemContainer;
}
private void WinoListView_ChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
if (args.Item is ThreadMailItemViewModel)
{
args.ItemContainer = new WinoThreadMailItemViewModelListViewItem();
}
else if (args.Item is MailItemViewModel)
{
args.ItemContainer = new WinoMailItemViewModelListViewItem();
}
// Handle the preparation in PrepareContainerForItemOverride
args.IsContainerPrepared = false;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
if (item is MailItemViewModel mailItemViewModel && element is WinoMailItemViewModelListViewItem container)
{
// Ensure the container's selection state matches the model's state
// This is crucial for virtualization scenarios where containers are recycled
container.IsSelected = mailItemViewModel.IsSelected;
}
else if (item is ThreadMailItemViewModel threadMailItemViewModel && element is WinoThreadMailItemViewModelListViewItem threadContainer)
{
threadContainer.IsThreadExpanded = threadMailItemViewModel.IsThreadExpanded;
}
base.PrepareContainerForItemOverride(element, item);
}
public bool SelectMailItemContainer(MailItemViewModel mailItemViewModel)
{
WinoListViewItem? itemContainer = null;
WinoMailItemViewModelListViewItem? itemContainer = null;
WinoThreadMailItemViewModelListViewItem? threadContainer = null;
foreach (var item in Items)
{
if (item is MailItemViewModel mailItem && mailItem.Id == mailItemViewModel.Id)
{
itemContainer = ContainerFromItem(mailItemViewModel) as WinoListViewItem;
itemContainer = ContainerFromItem(mailItemViewModel) as WinoMailItemViewModelListViewItem;
break;
}
else if (item is ThreadMailItemViewModel threadMailItemViewModel && threadMailItemViewModel.HasUniqueId(mailItemViewModel.MailCopy.UniqueId))
{
itemContainer = ContainerFromItem(threadMailItemViewModel) as WinoListViewItem;
threadContainer = ContainerFromItem(threadMailItemViewModel) as WinoThreadMailItemViewModelListViewItem;
// Try to get the inner WinoListView.
if (itemContainer != null)
if (threadContainer != null)
{
itemContainer.IsExpanded = true;
threadContainer.IsThreadExpanded = true;
var innerListViewControl = itemContainer.GetWinoListViewControl();
var innerListViewControl = threadContainer.GetWinoListViewControl();
if (innerListViewControl != null)
{
itemContainer = innerListViewControl.ContainerFromItem(mailItemViewModel) as WinoListViewItem;
itemContainer = innerListViewControl.ContainerFromItem(mailItemViewModel) as WinoMailItemViewModelListViewItem;
}
}
@@ -42,8 +79,37 @@ public partial class WinoListView : Microsoft.UI.Xaml.Controls.ListView
}
}
itemContainer?.IsSelected = true;
if (itemContainer != null)
{
itemContainer.IsSelected = true;
return true;
}
else if (threadContainer != null)
{
return true;
}
return itemContainer != null;
return false;
}
public void ChangeSelectionMode(ListViewSelectionMode mode)
{
// Not only this control, but also all inner WinoListView controls should change the selection mode.
// TODO: New threads added after this call won't have the correct selection mode.
SelectionMode = mode;
foreach (var item in Items)
{
if (item is ThreadMailItemViewModel)
{
var itemContainer = ContainerFromItem(item) as WinoThreadMailItemViewModelListViewItem;
if (itemContainer != null)
{
var innerListViewControl = itemContainer.GetWinoListViewControl();
innerListViewControl?.ChangeSelectionMode(mode);
}
}
}
}
}
@@ -2,14 +2,16 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Wino.Controls"
xmlns:local="using:Wino.Mail.WinUI.Controls.ListView">
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="/Styles/WinoExpanderStyle.xaml" />
<ResourceDictionary>
<!-- Thread Mail ListViewItem Style -->
<Style x:Key="DefaultThreadListViewItemStyle" TargetType="local:WinoListViewItem">
<Style x:Key="DefaultThreadListViewItemStyle" TargetType="local:WinoMailItemViewModelListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="{ThemeResource ListViewItemBackground}" />
@@ -30,20 +32,17 @@
<Setter Property="FocusVisualSecondaryThickness" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:WinoListViewItem">
<Expander Header="Thread" IsExpanded="{TemplateBinding IsExpanded}">
<Expander.Content>
<!-- Expandable Content -->
<ContentPresenter
x:Name="ThreadContent"
Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}" />
</Expander.Content>
</Expander>
<ControlTemplate TargetType="local:WinoMailItemViewModelListViewItem">
<!-- Expandable Content -->
<ContentPresenter
x:Name="ThreadContent"
Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}" />
</ControlTemplate>
</Setter.Value>
</Setter>
@@ -53,7 +52,7 @@
<Style
x:Key="DefaultMailListViewItemStyle"
BasedOn="{StaticResource DefaultListViewItemStyle}"
TargetType="local:WinoListViewItem" />
TargetType="local:WinoMailItemViewModelListViewItem" />
<local:WinoMailItemContainerStyleSelector
x:Name="WinoMailItemContainerStyleSelector"
@@ -1,47 +1,30 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Wino.Mail.ViewModels.Data;
using Wino.Messaging.Client.Mails;
namespace Wino.Mail.WinUI.Controls.ListView;
public partial class WinoListViewItem : ListViewItem
public partial class WinoMailItemViewModelListViewItem : ListViewItem
{
public bool IsExpanded
public WinoMailItemViewModelListViewItem()
{
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}
public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register(nameof(IsExpanded), typeof(bool), typeof(WinoListViewItem), new PropertyMetadata(false, OnIsExpandedChanged));
public WinoListViewItem()
{
DefaultStyleKey = typeof(WinoListViewItem);
DefaultStyleKey = typeof(WinoMailItemViewModelListViewItem);
RegisterPropertyChangedCallback(IsSelectedProperty, OnIsSelectedChanged);
}
private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is WinoListViewItem item)
{
// Handle expansion state change if needed
}
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (oldContent is IMailListItem oldMailItem)
if (oldContent is MailItemViewModel oldMailItem)
{
UnregisterSelectionCallback(oldMailItem);
}
if (newContent is IMailListItem newMailItem)
if (newContent is MailItemViewModel newMailItem)
{
IsSelected = newMailItem.IsSelected;
RegisterSelectionCallback(newMailItem);
@@ -61,9 +44,9 @@ public partial class WinoListViewItem : ListViewItem
// From model
private void MailPropChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (sender is not IMailListItem mailItem) return;
if (sender is not MailItemViewModel mailItem) return;
if (e.PropertyName == nameof(IMailListItem.IsSelected)) ApplySelectionForContainer(mailItem);
if (e.PropertyName == nameof(MailItemViewModel.IsSelected)) ApplySelectionForContainer(mailItem);
}
// From container.
@@ -91,13 +74,4 @@ public partial class WinoListViewItem : ListViewItem
IsSelected = mailItem.IsSelected;
}
}
public WinoListView? GetWinoListViewControl()
{
var expander = GetTemplateChild("ExpanderPart") as Expander;
if (expander?.Content is ContentPresenter presenter) return VisualTreeHelper.GetChild(presenter, 0) as WinoListView;
return null;
}
}
@@ -0,0 +1,116 @@
using System.ComponentModel;
using System.Linq;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Wino.Controls;
using Wino.Helpers;
using Wino.Mail.ViewModels.Data;
namespace Wino.Mail.WinUI.Controls.ListView;
public partial class WinoThreadMailItemViewModelListViewItem : ListViewItem
{
public bool IsThreadExpanded
{
get { return (bool)GetValue(IsThreadExpandedProperty); }
set { SetValue(IsThreadExpandedProperty, value); }
}
public static readonly DependencyProperty IsThreadExpandedProperty = DependencyProperty.Register(nameof(IsThreadExpanded), typeof(bool), typeof(WinoThreadMailItemViewModelListViewItem), new PropertyMetadata(false, new PropertyChangedCallback(OnIsThreadExpandedChanged)));
public WinoThreadMailItemViewModelListViewItem()
{
RegisterPropertyChangedCallback(IsSelectedProperty, OnIsSelectedChanged);
}
private static void OnIsThreadExpandedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs dp)
{
// 1. Reflect expansion changes to WinoExpander.
// 2. Automatically select first item on expansion, if none selected.
// 3. Unselect all items on collapse.
var control = sender as WinoThreadMailItemViewModelListViewItem;
var innerControl = control?.GetWinoListViewControl();
var expander = control?.GetExpander();
if (innerControl == null || control == null || expander == null) return;
// 1
expander.IsExpanded = control.IsThreadExpanded;
// 2
if (control.IsThreadExpanded && innerControl.SelectedItems.Count == 0 && innerControl.Items.Count > 0)
{
innerControl.SelectedItem = innerControl.Items[0];
}
// 3
if (!control.IsSelected) innerControl?.SelectedItems.Clear();
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (oldContent is ThreadMailItemViewModel oldMailItem)
{
UnregisterSelectionCallback(oldMailItem);
}
if (newContent is ThreadMailItemViewModel newMailItem)
{
IsSelected = newMailItem.IsSelected;
RegisterSelectionCallback(newMailItem);
}
}
private void OnIsSelectedChanged(DependencyObject sender, DependencyProperty dp)
{
IsThreadExpanded = IsSelected;
}
private void UnregisterSelectionCallback(ThreadMailItemViewModel mailItem)
{
mailItem.PropertyChanged -= MailPropChanged;
}
private void MailPropChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not ThreadMailItemViewModel mailItem) return;
if (e.PropertyName == nameof(ThreadMailItemViewModel.IsThreadExpanded))
{
ApplySelectionForContainer(mailItem);
}
}
private void RegisterSelectionCallback(ThreadMailItemViewModel mailItem)
{
mailItem.PropertyChanged += MailPropChanged;
}
private void ApplySelectionForModel(ThreadMailItemViewModel mailItem)
{
if (mailItem.IsThreadExpanded != IsThreadExpanded)
{
mailItem.IsThreadExpanded = IsThreadExpanded;
}
}
private void ApplySelectionForContainer(ThreadMailItemViewModel mailItem)
{
if (IsThreadExpanded != mailItem.IsThreadExpanded) IsThreadExpanded = mailItem.IsThreadExpanded;
}
public WinoListView? GetWinoListViewControl()
{
var expander = GetExpander();
if (expander?.Content is WinoListView control) return control;
return null;
}
public WinoExpander? GetExpander() => WinoVisualTreeHelper.FindDescendants<WinoExpander>(this).FirstOrDefault();
}
+1 -1
View File
@@ -83,7 +83,7 @@ public partial class WinoExpander : Control
return;
}
IsExpanded = !IsExpanded;
// IsExpanded = !IsExpanded;
}
private static void OnIsExpandedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
+34 -1
View File
@@ -69,7 +69,39 @@
</DataTemplate>
<DataTemplate x:Key="ThreadMailItemTemplate" x:DataType="viewModelData:ThreadMailItemViewModel">
<TextBlock Text="thread :)" />
<controls:WinoExpander x:Name="ExpanderPart" IsExpanded="{x:Bind IsThreadExpanded, Mode=TwoWay}">
<controls:WinoExpander.Header>
<controls:MailItemDisplayInformationControl
x:DefaultBindMode="OneWay"
CenterHoverAction="{Binding ElementName=root, Path=ViewModel.PreferencesService.CenterHoverAction, Mode=OneWay}"
ContextRequested="MailItemContextRequested"
DisplayMode="{Binding ElementName=root, Path=ViewModel.PreferencesService.MailItemDisplayMode, Mode=OneWay}"
HoverActionExecutedCommand="{Binding ElementName=root, Path=ViewModel.ExecuteHoverActionCommand}"
IsAvatarVisible="{Binding ElementName=root, Path=ViewModel.PreferencesService.IsShowSenderPicturesEnabled, Mode=OneWay}"
IsHoverActionsEnabled="{Binding ElementName=root, Path=ViewModel.PreferencesService.IsHoverActionsEnabled, Mode=OneWay}"
IsThreadExpanded="{x:Bind IsThreadExpanded, Mode=OneWay}"
IsThreadExpanderVisible="True"
IsThumbnailUpdated="{x:Bind LatestMailViewModel.ThumbnailUpdatedEvent, Mode=OneWay}"
LeftHoverAction="{Binding ElementName=root, Path=ViewModel.PreferencesService.LeftHoverAction, Mode=OneWay}"
MailItem="{x:Bind LatestMailViewModel.MailCopy, Mode=OneWay}"
Prefer24HourTimeFormat="{Binding ElementName=root, Path=ViewModel.PreferencesService.Prefer24HourTimeFormat, Mode=OneWay}"
RightHoverAction="{Binding ElementName=root, Path=ViewModel.PreferencesService.RightHoverAction, Mode=OneWay}"
ShowPreviewText="{Binding ElementName=root, Path=ViewModel.PreferencesService.IsShowPreviewEnabled, Mode=OneWay}" />
</controls:WinoExpander.Header>
<controls:WinoExpander.Content>
<listview:WinoListView
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
toolkitExt:ListViewExtensions.ItemContainerStretchDirection="Horizontal"
toolkitExt:ScrollViewerExtensions.VerticalScrollBarMargin="0"
ItemContainerStyle="{StaticResource DefaultMailListViewItemStyle}"
ItemTemplate="{StaticResource SingleMailItemTemplate}"
ItemsSource="{x:Bind ThreadEmails, Mode=OneTime}"
ProcessKeyboardAccelerators="WinoListViewProcessKeyboardAccelerators"
SelectionMode="Extended" />
</controls:WinoExpander.Content>
</controls:WinoExpander>
</DataTemplate>
<listview:WinoMailItemTemplateSelector
@@ -355,6 +387,7 @@
ItemTemplateSelector="{StaticResource MailItemTemplateSelector}"
ItemsSource="{x:Bind MailCollectionViewSource.View, Mode=OneWay}"
ProcessKeyboardAccelerators="WinoListViewProcessKeyboardAccelerators"
SelectionChanged="WinoListViewSelectionChanged"
SelectionMode="Extended">
<listview:WinoListView.ItemContainerTransitions>
<TransitionCollection>
+7 -64
View File
@@ -8,7 +8,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
@@ -21,7 +20,6 @@ using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.MailItem;
using Wino.Core.Domain.Models.Menus;
using Wino.Core.Domain.Models.Navigation;
using Wino.Helpers;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.ViewModels.Messages;
using Wino.MenuFlyouts.Context;
@@ -123,7 +121,7 @@ public sealed partial class MailListPage : MailListPageAbstract,
private void ChangeSelectionMode(ListViewSelectionMode mode)
{
MailListView.SelectionMode = mode;
MailListView.ChangeSelectionMode(mode);
if (ViewModel?.PivotFolders != null)
{
@@ -509,72 +507,12 @@ public sealed partial class MailListPage : MailListPageAbstract,
private void DeleteAllInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
=> ViewModel.ExecuteMailOperationCommand.Execute(MailOperation.SoftDelete);
/// <summary>
/// Animates the rotation using high-performance Composition APIs
/// </summary>
private void AnimateRotationWithComposition(FrameworkElement element, float targetAngleInDegrees)
{
// Get the element's visual from the composition layer
var visual = ElementCompositionPreview.GetElementVisual(element);
var compositor = visual.Compositor;
// Set the center point for rotation (center of the element)
visual.CenterPoint = new System.Numerics.Vector3(
(float)element.ActualWidth / 2f,
(float)element.ActualHeight / 2f,
0f);
// Create a rotation animation
var rotationAnimation = compositor.CreateScalarKeyFrameAnimation();
rotationAnimation.Target = "RotationAngleInDegrees";
rotationAnimation.Duration = TimeSpan.FromMilliseconds(200);
// Add easing function for smooth animation
var easingFunction = compositor.CreateCubicBezierEasingFunction(
new System.Numerics.Vector2(0.25f, 0.1f), // Control point 1
new System.Numerics.Vector2(0.25f, 1f)); // Control point 2 (similar to CircleEase)
// Insert keyframe with the target angle and easing
rotationAnimation.InsertKeyFrame(1.0f, targetAngleInDegrees, easingFunction);
// Start the animation
visual.StartAnimation("RotationAngleInDegrees", rotationAnimation);
}
private void WinoMailCollectionSelectionChanged(object sender, EventArgs args)
private void WinoMailCollectionSelectionChanged(object? sender, EventArgs args)
{
UpdateSelectAllButtonStatus();
UpdateAdaptiveness();
}
private void ThreadContainerRightTapped(object sender, RightTappedRoutedEventArgs e)
{
if (sender is ItemContainer container && container.Tag is ThreadMailItemViewModel expander)
{
expander.IsThreadExpanded = !expander.IsThreadExpanded;
// Select all.
// ViewModel.MailCollection.AllItems.Where(a => expander.ThreadEmails.Contains(a)).ForEach(a => a.IsSelected = true);
}
}
private void ThreadContainerTapped(object sender, TappedRoutedEventArgs e)
{
if (sender is ItemContainer container && container.Tag is ThreadMailItemViewModel expander)
{
// Toggle expansion state
expander.IsThreadExpanded = !expander.IsThreadExpanded;
// Find the expander icon and animate its rotation using Composition APIs
var expanderIcon = WinoVisualTreeHelper.GetChildObject<FontIcon>(container, "ExpanderIcon");
if (expanderIcon != null)
{
var targetAngle = expander.IsThreadExpanded ? 90f : 0f;
AnimateRotationWithComposition(expanderIcon, targetAngle);
}
}
}
private async void WinoListViewProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{
if (args.Key == VirtualKey.Delete)
@@ -588,4 +526,9 @@ public sealed partial class MailListPage : MailListPageAbstract,
await ViewModel.MailCollection.ToggleSelectAllAsync();
}
}
private void WinoListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
+53 -2
View File
@@ -28,7 +28,6 @@ public class MailService : BaseDatabaseService, IMailService
private readonly IContactService _contactService;
private readonly IAccountService _accountService;
private readonly ISignatureService _signatureService;
private readonly IMimeFileService _mimeFileService;
private readonly IPreferencesService _preferencesService;
@@ -247,7 +246,59 @@ public class MailService : BaseDatabaseService, IMailService
cancellationToken.ThrowIfCancellationRequested();
return [.. mails];
// If CreateThreads is false, just return the mails as-is
if (!options.CreateThreads)
{
return [.. mails];
}
// Include other mails in the same threads
var expandedMails = new List<MailCopy>(mails);
var processedThreadIds = new HashSet<string>();
foreach (var mail in mails)
{
if (!string.IsNullOrEmpty(mail.ThreadId) && !processedThreadIds.Contains(mail.ThreadId))
{
processedThreadIds.Add(mail.ThreadId);
// Get all other mails with the same ThreadId that are not already in the result
var existingMailIds = expandedMails.Select(m => m.Id).ToHashSet();
var threadMails = await GetMailsByThreadIdAsync(mail.ThreadId, existingMailIds).ConfigureAwait(false);
if (threadMails?.Any() == true)
{
// Load assigned properties for the thread mails
foreach (var threadMail in threadMails)
{
await LoadAssignedPropertiesWithCacheAsync(threadMail, folderCache, accountCache, contactCache).ConfigureAwait(false);
}
// Remove items that have no assigned account or folder
threadMails.RemoveAll(a => a.AssignedAccount == null || a.AssignedFolder == null);
expandedMails.AddRange(threadMails);
}
}
cancellationToken.ThrowIfCancellationRequested();
}
return [.. expandedMails];
}
private async Task<List<MailCopy>> GetMailsByThreadIdAsync(string threadId, HashSet<string> excludeMailIds)
{
if (string.IsNullOrEmpty(threadId))
return [];
var query = new Query("MailCopy")
.Where("ThreadId", threadId)
.WhereNotIn("Id", excludeMailIds)
.SelectRaw("MailCopy.*")
.GetRawQuery();
return await Connection.QueryAsync<MailCopy>(query);
}
/// <summary>