| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NotificationsManager.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Reactive.Concurrency; |
| | | 12 | | using System.Reactive.Linq; |
| | | 13 | | using MyNet.UI.Notifications.Models; |
| | | 14 | | using MyNet.UI.Threading; |
| | | 15 | | |
| | | 16 | | namespace MyNet.UI.Notifications; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Implements the <see cref="INotificationsManager"/> interface, managing a collection of notifications received from a |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class NotificationsManager : INotificationsManager, IDisposable |
| | | 22 | | { |
| | 15 | 23 | | private readonly ObservableCollection<INotification> _items = []; |
| | 15 | 24 | | private readonly Dictionary<Guid, EventHandler<CloseRequestedEventArgs>> _closeHandlers = []; |
| | | 25 | | private readonly ISchedulerProvider _schedulerProvider; |
| | | 26 | | private readonly IDisposable _subscription; |
| | | 27 | | private bool _isDisposed; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="NotificationsManager"/> class, subscribing to the notification stre |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="service">The notification service to subscribe to for receiving notifications.</param> |
| | | 33 | | /// <param name="schedulerProvider">The scheduler provider to ensure UI thread safety.</param> |
| | | 34 | | public NotificationsManager( |
| | | 35 | | INotificationService service, |
| | | 36 | | ISchedulerProvider schedulerProvider) |
| | | 37 | | { |
| | 15 | 38 | | _schedulerProvider = schedulerProvider; |
| | 15 | 39 | | Notifications = new(_items); |
| | | 40 | | |
| | 15 | 41 | | _subscription = service.Notifications |
| | 15 | 42 | | .ObserveOn(schedulerProvider.Ui) |
| | 15 | 43 | | .Subscribe(OnNotificationReceived); |
| | 15 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public ReadOnlyObservableCollection<INotification> Notifications { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Handles incoming notifications, checks for duplicates, then tracks lifecycle hooks for closable notifications. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="notification">The notification received from the service.</param> |
| | | 53 | | private void OnNotificationReceived(INotification notification) |
| | | 54 | | { |
| | 15 | 55 | | if (IsDuplicate(notification)) |
| | 3 | 56 | | return; |
| | | 57 | | |
| | 12 | 58 | | _items.Add(notification); |
| | | 59 | | |
| | 12 | 60 | | HookLifecycle(notification); |
| | 12 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Determines whether the given notification is a duplicate of any existing notification in the collection, based o |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="notification">The notification to check for duplication.</param> |
| | | 67 | | /// <returns>True if the notification is a duplicate; otherwise, false.</returns> |
| | | 68 | | private bool IsDuplicate(INotification notification) => |
| | 15 | 69 | | notification is IDeduplicableNotification dedup && _items.Any(x => |
| | 15 | 70 | | x is IDeduplicableNotification existing && |
| | 15 | 71 | | dedup.IsDuplicateOf(existing)); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Hooks into the lifecycle of a closable notification by subscribing to its CloseRequested event, allowing for aut |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="notification">The closable notification to hook into.</param> |
| | | 77 | | private void HookLifecycle(INotification notification) |
| | | 78 | | { |
| | 12 | 79 | | if (notification is not IClosableNotification closableNotification) |
| | 9 | 80 | | return; |
| | | 81 | | |
| | 3 | 82 | | _closeHandlers[notification.Id] = handler; |
| | 3 | 83 | | closableNotification.CloseRequested += handler; |
| | 3 | 84 | | return; |
| | | 85 | | |
| | | 86 | | void handler(object? sender, CloseRequestedEventArgs e) => Remove(notification); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private void UnhookLifecycle(INotification notification) |
| | | 90 | | { |
| | 12 | 91 | | if (notification is not IClosableNotification closableNotification) |
| | 9 | 92 | | return; |
| | | 93 | | |
| | 3 | 94 | | if (!_closeHandlers.TryGetValue(notification.Id, out var handler)) |
| | 0 | 95 | | return; |
| | | 96 | | |
| | 3 | 97 | | closableNotification.CloseRequested -= handler; |
| | 3 | 98 | | _closeHandlers.Remove(notification.Id); |
| | 3 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | 6 | 102 | | public void Remove(INotification notification) => _schedulerProvider.Ui.Schedule(() => RemoveCore(notification)); |
| | | 103 | | |
| | | 104 | | private void RemoveCore(INotification notification) |
| | | 105 | | { |
| | 6 | 106 | | if (_isDisposed) |
| | 0 | 107 | | return; |
| | | 108 | | |
| | 6 | 109 | | var item = _items.FirstOrDefault(x => x.Id == notification.Id); |
| | 6 | 110 | | if (item is null) |
| | 0 | 111 | | return; |
| | | 112 | | |
| | 6 | 113 | | UnhookLifecycle(item); |
| | 6 | 114 | | _items.Remove(item); |
| | 6 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | 3 | 118 | | public void Clear() => _schedulerProvider.Ui.Schedule(ClearCore); |
| | | 119 | | |
| | | 120 | | private void ClearCore() |
| | | 121 | | { |
| | 3 | 122 | | if (_isDisposed) |
| | 0 | 123 | | return; |
| | | 124 | | |
| | 12 | 125 | | foreach (var item in _items.ToList()) |
| | 3 | 126 | | UnhookLifecycle(item); |
| | | 127 | | |
| | 3 | 128 | | _items.Clear(); |
| | 3 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | public void Dispose() |
| | | 133 | | { |
| | 15 | 134 | | if (_isDisposed) |
| | 0 | 135 | | return; |
| | | 136 | | |
| | 15 | 137 | | _isDisposed = true; |
| | 15 | 138 | | _subscription.Dispose(); |
| | | 139 | | |
| | 36 | 140 | | foreach (var item in _items.ToList()) |
| | 3 | 141 | | UnhookLifecycle(item); |
| | | 142 | | |
| | 15 | 143 | | _items.Clear(); |
| | 15 | 144 | | } |
| | | 145 | | } |
| | | 146 | | |