XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

HaloPixelToolBox

【WinUI3】基于USB HID通讯的花再音响工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using HaloPixelToolBox.Core.Models.Notifications;

namespace HaloPixelToolBox.Core.Utilities;

/// <summary>仅把会话预览的新增未读消息作为通知,首次读取用作基线。</summary>
public sealed class UnreadMessageTracker
{
    private readonly Dictionary<MessageNotificationSource, Dictionary<string, UnreadMessagePreview>> snapshots = [];

    public IReadOnlyList<MessageNotification> Update(MessageNotificationSource source, IEnumerable<UnreadMessagePreview> previews)
    {
        var current = previews.GroupBy(item => item.Conversation)
            .ToDictionary(group => group.Key, group => group.First());
        var messages = new List<MessageNotification>();
        if (snapshots.TryGetValue(source, out var previous))
            foreach (var item in current.Values)
                if (item.UnreadCount > 0 && item.Content.Length > 0
                    && (!previous.TryGetValue(item.Conversation, out var old)
                        || old.UnreadCount < item.UnreadCount
                        || (old.UnreadCount >= 99 && (old.Content != item.Content || old.Sender != item.Sender))))
                    messages.Add(new(source, item.Sender, item.Content));
        snapshots[source] = current;
        return messages;
    }

    public void Reset(MessageNotificationSource source) => snapshots.Remove(source);
}