feat(notification): ✨ Remove notification when read externally (#707)
* feat(notification): ✨ Add notification removal feature Implemented a new method `RemoveNotificationAsync` in the `INotificationBuilder` interface to allow the removal of toast notifications for specific emails identified by a unique ID. This change enhances the notification management by ensuring that notifications can be cleared when emails are marked as read. The `NotificationBuilder` class has been updated to include logic for removing existing notifications and to use the unique ID as a tag for the toast notifications, facilitating their removal. Additionally, the `AppShellViewModel` has been modified to call this new method when an email is updated and marked as read. This improvement aims to provide a better user experience by keeping the notification area relevant and up-to-date. * feat(notification): ✨ Add MailReadStatusChanged event handling Introduced a new event system for handling email read status changes. This includes the addition of a listener in `NotificationBuilder` that removes notifications when an email is marked as read. • Added `MailReadStatusChanged` record to represent the event. • Registered a listener in `NotificationBuilder` to handle notification removal. • Removed the `OnMailUpdated` method from `AppShellViewModel`, delegating notification management to the new event system. • Updated `MailService` to send `MailReadStatusChanged` events when emails are marked as read. This change improves the communication between components and enhances the notification management system. * refactor: Remove comments * Little cleanup. --------- Co-authored-by: Burak Kaan Köse <bkaankose@outlook.com>
This commit is contained in:
committed by
GitHub
parent
c2bb07ff3d
commit
43283b7218
@@ -22,4 +22,9 @@ public interface INotificationBuilder
|
|||||||
/// Creates test notification for test purposes.
|
/// Creates test notification for test purposes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task CreateTestNotificationAsync(string title, string message);
|
Task CreateTestNotificationAsync(string title, string message);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the toast notification for a specific mail by unique id.
|
||||||
|
/// </summary>
|
||||||
|
void RemoveNotification(Guid mailUniqueId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
using CommunityToolkit.WinUI.Notifications;
|
using CommunityToolkit.WinUI.Notifications;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Windows.Data.Xml.Dom;
|
using Windows.Data.Xml.Dom;
|
||||||
@@ -11,7 +13,7 @@ using Wino.Core.Domain.Entities.Mail;
|
|||||||
using Wino.Core.Domain.Enums;
|
using Wino.Core.Domain.Enums;
|
||||||
using Wino.Core.Domain.Interfaces;
|
using Wino.Core.Domain.Interfaces;
|
||||||
using Wino.Core.Domain.Models.MailItem;
|
using Wino.Core.Domain.Models.MailItem;
|
||||||
using System.IO;
|
using Wino.Messaging.UI;
|
||||||
|
|
||||||
namespace Wino.Core.UWP.Services;
|
namespace Wino.Core.UWP.Services;
|
||||||
|
|
||||||
@@ -36,6 +38,11 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
_folderService = folderService;
|
_folderService = folderService;
|
||||||
_mailService = mailService;
|
_mailService = mailService;
|
||||||
_thumbnailService = thumbnailService;
|
_thumbnailService = thumbnailService;
|
||||||
|
|
||||||
|
WeakReferenceMessenger.Default.Register<MailReadStatusChanged>(this, (r, msg) =>
|
||||||
|
{
|
||||||
|
RemoveNotification(msg.UniqueId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateNotificationsAsync(Guid inboxFolderId, IEnumerable<IMailItem> downloadedMailItems)
|
public async Task CreateNotificationsAsync(Guid inboxFolderId, IEnumerable<IMailItem> downloadedMailItems)
|
||||||
@@ -81,7 +88,11 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
foreach (var mailItem in validItems)
|
foreach (var mailItem in validItems)
|
||||||
{
|
{
|
||||||
if (mailItem.IsRead)
|
if (mailItem.IsRead)
|
||||||
|
{
|
||||||
|
// Remove the notification for a specific mail if it exists
|
||||||
|
ToastNotificationManager.History.Remove(mailItem.UniqueId.ToString());
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var builder = new ToastContentBuilder();
|
var builder = new ToastContentBuilder();
|
||||||
builder.SetToastScenario(ToastScenario.Default);
|
builder.SetToastScenario(ToastScenario.Default);
|
||||||
@@ -117,7 +128,8 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
Src = new Uri("ms-winsoundevent:Notification.Mail")
|
Src = new Uri("ms-winsoundevent:Notification.Mail")
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Show();
|
// Use UniqueId as tag to allow removal
|
||||||
|
builder.Show(toast => toast.Tag = mailItem.UniqueId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
await UpdateTaskbarIconBadgeAsync();
|
await UpdateTaskbarIconBadgeAsync();
|
||||||
@@ -230,4 +242,16 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
|
|
||||||
//await Task.CompletedTask;
|
//await Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemoveNotification(Guid mailUniqueId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ToastNotificationManager.History.Remove(mailUniqueId.ToString());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Error(ex, $"Failed to remove notification for mail {mailUniqueId}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
Wino.Messages/UI/MailReadStatusChanged.cs
Normal file
5
Wino.Messages/UI/MailReadStatusChanged.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Wino.Messaging.UI;
|
||||||
|
|
||||||
|
public record MailReadStatusChanged(Guid UniqueId);
|
||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
using MimeKit;
|
using MimeKit;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using SqlKata;
|
using SqlKata;
|
||||||
@@ -584,6 +585,10 @@ public class MailService : BaseDatabaseService, IMailService
|
|||||||
if (item.IsRead == isRead) return false;
|
if (item.IsRead == isRead) return false;
|
||||||
|
|
||||||
item.IsRead = isRead;
|
item.IsRead = isRead;
|
||||||
|
if (isRead && item.UniqueId != Guid.Empty)
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send(new MailReadStatusChanged(item.UniqueId));
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user