Fix the sorting when adding mails.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace Wino.Core.Domain.Interfaces;
|
||||
|
||||
public interface IMailListItemSorting
|
||||
{
|
||||
DateTime SortingDate { get; }
|
||||
string SortingName { get; }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Wino.Core.Domain.Entities.Mail;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
|
||||
public class ListItemComparer : IComparer<object>
|
||||
{
|
||||
@@ -8,13 +9,14 @@ public class ListItemComparer : IComparer<object>
|
||||
|
||||
public int Compare(object x, object y)
|
||||
{
|
||||
if (x is MailCopy xMail && y is MailCopy yMail)
|
||||
if (x is IMailListItemSorting xSorting && y is IMailListItemSorting ySorting)
|
||||
return SortByName ? string.Compare(xSorting.SortingName, ySorting.SortingName, StringComparison.OrdinalIgnoreCase) : DateTime.Compare(xSorting.SortingDate, ySorting.SortingDate);
|
||||
else if (x is MailCopy xMail && y is MailCopy yMail)
|
||||
return SortByName ? string.Compare(xMail.FromName, yMail.FromName, StringComparison.OrdinalIgnoreCase) : DateTime.Compare(yMail.CreationDate, xMail.CreationDate);
|
||||
else if (x is DateTime dateX && y is DateTime dateY)
|
||||
return DateTime.Compare(dateY, dateX);
|
||||
else if (x is string stringX && y is string stringY)
|
||||
return stringY.CompareTo(stringX);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,10 +42,21 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
|
||||
public ReadOnlyObservableGroupedCollection<object, IMailListItem> MailItems { get; }
|
||||
|
||||
private SortingOptionType _sortingType;
|
||||
|
||||
/// <summary>
|
||||
/// Property that defines how the item sorting should be done in the collection.
|
||||
/// </summary>
|
||||
public SortingOptionType SortingType { get; set; }
|
||||
public SortingOptionType SortingType
|
||||
{
|
||||
get => _sortingType;
|
||||
set
|
||||
{
|
||||
_sortingType = value;
|
||||
// Update the comparer's sort mode when sorting type changes
|
||||
listComparer.SortByName = value == SortingOptionType.Sender;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the grouping type for emails.
|
||||
@@ -79,6 +90,9 @@ public class WinoMailCollection : ObservableRecipient, IRecipient<SelectedItemsC
|
||||
{
|
||||
MailItems = new ReadOnlyObservableGroupedCollection<object, IMailListItem>(_mailItemSource);
|
||||
|
||||
// Initialize sorting type to default (date-based)
|
||||
SortingType = SortingOptionType.ReceiveDate;
|
||||
|
||||
Messenger.Register<SelectedItemsChangedMessage>(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Wino.Mail.ViewModels.Data;
|
||||
/// Common interface for mail items that can be displayed in a mail list.
|
||||
/// Implemented by both MailItemViewModel and ThreadMailItemViewModel.
|
||||
/// </summary>
|
||||
public interface IMailListItem : IMailHashContainer, INotifyPropertyChanged
|
||||
public interface IMailListItem : IMailHashContainer, IMailListItemSorting, INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the latest creation date for sorting purposes.
|
||||
|
||||
@@ -171,6 +171,10 @@ public partial class MailItemViewModel(MailCopy mailCopy) : ObservableRecipient,
|
||||
set => SetProperty(MailCopy.SenderContact.Base64ContactPicture, value, MailCopy, (u, n) => u.SenderContact.Base64ContactPicture = n);
|
||||
}
|
||||
|
||||
public DateTime SortingDate => CreationDate;
|
||||
|
||||
public string SortingName => FromName;
|
||||
|
||||
public IEnumerable<Guid> GetContainingIds() => [MailCopy.UniqueId];
|
||||
|
||||
public IEnumerable<MailItemViewModel> GetSelectedMailItems()
|
||||
|
||||
@@ -167,6 +167,10 @@ public partial class ThreadMailItemViewModel : ObservableRecipient, IMailListIte
|
||||
|
||||
private MailItemViewModel latestMailViewModel => ThreadEmails.OrderByDescending(e => e.MailCopy?.CreationDate).FirstOrDefault()!;
|
||||
|
||||
public DateTime SortingDate => CreationDate;
|
||||
|
||||
public string SortingName => FromName;
|
||||
|
||||
public ThreadMailItemViewModel(string threadId)
|
||||
{
|
||||
_threadId = threadId;
|
||||
|
||||
Reference in New Issue
Block a user