Files
Wino-Mail/Wino.Mail.ViewModels/Data/ThreadMailItemViewModel.cs
T

115 lines
2.9 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
2025-02-16 11:54:23 +01:00
namespace Wino.Mail.ViewModels.Data;
/// <summary>
/// Thread mail item (multiple IMailItem) view model representation.
/// </summary>
2025-10-03 15:46:38 +02:00
public partial class ThreadMailItemViewModel : ObservableRecipient, IDisposable
2024-04-18 01:44:37 +02:00
{
2025-10-03 15:46:38 +02:00
private readonly string _threadId;
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
private readonly List<MailItemViewModel> _threadEmails = [];
private bool _disposed;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[ObservableProperty]
2025-10-03 15:46:38 +02:00
[NotifyPropertyChangedRecipients]
public partial bool IsThreadExpanded { get; set; }
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
[ObservableProperty]
public partial bool IsSelected { get; set; }
/// <summary>
/// Gets the number of emails in this thread
/// </summary>
public int EmailCount => _threadEmails.Count;
/// <summary>
/// Gets the latest email's subject for display
/// </summary>
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
.OrderByDescending(e => e.MailCopy?.CreationDate)
.FirstOrDefault()?.MailCopy?.SenderContact.Name;
/// <summary>
/// Gets the latest email's creation date for sorting
/// </summary>
public DateTime LatestEmailDate => _threadEmails
.OrderByDescending(e => e.MailCopy?.CreationDate)
.FirstOrDefault()?.MailCopy?.CreationDate ?? DateTime.MinValue;
/// <summary>
/// Gets all emails in this thread (read-only)
/// </summary>
public IReadOnlyList<MailItemViewModel> ThreadEmails => _threadEmails.AsReadOnly();
public ThreadMailItemViewModel(string threadId)
2025-02-16 11:54:23 +01:00
{
2025-10-03 15:46:38 +02:00
_threadId = threadId;
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
partial void OnIsSelectedChanged(bool value)
{
2025-10-03 15:46:38 +02:00
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
protected virtual void Dispose(bool disposing)
2025-02-16 11:54:23 +01:00
{
2025-10-03 15:46:38 +02:00
if (_disposed)
return;
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
if (disposing)
{
_threadEmails.Clear();
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
_disposed = true;
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
public void Dispose()
2025-02-16 11:54:23 +01:00
{
2025-10-03 15:46:38 +02:00
Dispose(true);
GC.SuppressFinalize(this);
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
private void NotifyPropertyChanges()
{
OnPropertyChanged(nameof(Subject));
OnPropertyChanged(nameof(FromName));
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
/// <summary>
/// Adds an email to this thread
/// </summary>
public void AddEmail(MailItemViewModel email)
{
if (email.MailCopy.ThreadId != _threadId)
throw new ArgumentException($"Email ThreadId '{email.MailCopy.ThreadId}' does not match expander ThreadId '{_threadId}'");
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
_threadEmails.Add(email);
2025-02-16 11:54:23 +01:00
NotifyPropertyChanges();
}
2024-04-18 01:44:37 +02:00
2025-10-03 15:46:38 +02:00
/// <summary>
/// Removes an email from this thread
/// </summary>
public void RemoveEmail(MailItemViewModel email)
2025-02-16 11:54:23 +01:00
{
2025-10-03 15:46:38 +02:00
if (_threadEmails.Remove(email))
{
NotifyPropertyChanges();
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
}