| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ToastManager.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; |
| | | 14 | | using MyNet.UI.Notifications.Models; |
| | | 15 | | using MyNet.UI.Threading; |
| | | 16 | | using MyNet.UI.Toasting.Filters; |
| | | 17 | | using MyNet.UI.Toasting.Models; |
| | | 18 | | using MyNet.UI.Toasting.Settings; |
| | | 19 | | |
| | | 20 | | namespace MyNet.UI.Toasting; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Manages the display and lifecycle of toasts based on incoming notifications, applying filtering, prioritization, and |
| | | 24 | | /// </summary> |
| | | 25 | | public sealed class ToastManager : IToastManager |
| | | 26 | | { |
| | 12 | 27 | | private readonly ObservableCollection<IToast> _visible = []; |
| | 12 | 28 | | private readonly PriorityQueue<INotification, int> _queue = new(); |
| | 12 | 29 | | private readonly Dictionary<Guid, IDisposable> _lifetimeSubscriptions = []; |
| | 12 | 30 | | private readonly Dictionary<Guid, EventHandler<CloseRequestedEventArgs>> _closeHandlers = []; |
| | | 31 | | private readonly ISchedulerProvider _scheduler; |
| | | 32 | | private readonly IToastFactory _factory; |
| | | 33 | | private readonly IToastFilter _filter; |
| | | 34 | | private readonly ToastManagerOptions _options; |
| | | 35 | | private readonly IDisposable _subscription; |
| | | 36 | | private bool _isDisposed; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="ToastManager"/> class, subscribing to the notification service to r |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="service">The notification service to subscribe to for receiving notifications.</param> |
| | | 42 | | /// <param name="scheduler">The scheduler provider for managing UI and background thread operations.</param> |
| | | 43 | | /// <param name="factory">The factory used to create toast instances.</param> |
| | | 44 | | /// <param name="filter">The filter used to determine which notifications should be displayed as toasts.</param> |
| | | 45 | | /// <param name="options">The options for configuring the behavior of the toast manager.</param> |
| | | 46 | | public ToastManager( |
| | | 47 | | INotificationService service, |
| | | 48 | | ISchedulerProvider scheduler, |
| | | 49 | | IToastFactory factory, |
| | | 50 | | IToastFilter filter, |
| | | 51 | | ToastManagerOptions? options = null) |
| | | 52 | | { |
| | 12 | 53 | | _scheduler = scheduler; |
| | 12 | 54 | | _factory = factory; |
| | 12 | 55 | | _filter = filter; |
| | 12 | 56 | | _options = options ?? new ToastManagerOptions(); |
| | | 57 | | |
| | 12 | 58 | | Toasts = new(_visible); |
| | | 59 | | |
| | 12 | 60 | | _subscription = service.Notifications |
| | 12 | 61 | | .ObserveOn(_scheduler.Ui) |
| | 12 | 62 | | .Subscribe(OnNotification); |
| | 12 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public ReadOnlyObservableCollection<IToast> Toasts { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Handles incoming notifications from the service, applying the filter to determine if they should be displayed as |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="notification">The notification to process for potential display as a toast.</param> |
| | | 72 | | private void OnNotification(INotification notification) |
| | | 73 | | { |
| | 15 | 74 | | if (_isDisposed) |
| | 0 | 75 | | return; |
| | | 76 | | |
| | 15 | 77 | | if (!_filter.ShouldDisplay(notification)) |
| | 0 | 78 | | return; |
| | | 79 | | |
| | 15 | 80 | | if (_visible.Count < _options.MaxVisibleToasts) |
| | | 81 | | { |
| | 9 | 82 | | Add(notification); |
| | | 83 | | } |
| | | 84 | | else |
| | | 85 | | { |
| | 6 | 86 | | Enqueue(notification); |
| | | 87 | | } |
| | 6 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Creates and displays a toast for the given notification, adding it to the collection of visible toasts and start |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="notification">The notification to display as a toast.</param> |
| | | 94 | | private void Add(INotification notification) |
| | | 95 | | { |
| | 12 | 96 | | var toast = _factory.Create(notification); |
| | | 97 | | |
| | 12 | 98 | | _visible.Add(toast); |
| | | 99 | | |
| | 12 | 100 | | HookLifecycle(toast); |
| | 12 | 101 | | StartLifetime(toast); |
| | 12 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Enqueues a notification that cannot be displayed immediately due to the maximum visible toasts limit, using the |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="notification">The notification to enqueue for later display as a toast.</param> |
| | | 108 | | private void Enqueue(INotification notification) |
| | | 109 | | { |
| | 6 | 110 | | if (_options.MaxQueueSize <= 0 || _queue.Count >= _options.MaxQueueSize) |
| | 3 | 111 | | return; |
| | | 112 | | |
| | 3 | 113 | | var priority = _options.PrioritySelector(notification); |
| | 3 | 114 | | _queue.Enqueue(notification, -priority); |
| | 3 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Starts the lifetime management for a toast that is set to auto-close, using a timer to automatically remove the |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="toast">The toast for which to start lifetime management.</param> |
| | | 121 | | private void StartLifetime(IToast toast) |
| | | 122 | | { |
| | 12 | 123 | | if (toast.Settings.ClosingStrategy is not ToastClosingStrategy.AutoClose and not ToastClosingStrategy.Both) |
| | 9 | 124 | | return; |
| | | 125 | | |
| | 3 | 126 | | if (toast.Settings.FreezeOnMouseEnter) |
| | 3 | 127 | | return; |
| | | 128 | | |
| | 0 | 129 | | var duration = toast.Settings.Duration ?? _options.DefaultDuration; |
| | | 130 | | |
| | 0 | 131 | | var subscription = System.Reactive.Linq.Observable.Timer(duration, _scheduler.Background) |
| | 0 | 132 | | .ObserveOn(_scheduler.Ui) |
| | 0 | 133 | | .Subscribe(_ => Remove(toast)); |
| | | 134 | | |
| | 0 | 135 | | _lifetimeSubscriptions[toast.Notification.Id] = subscription; |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | private void HookLifecycle(IToast toast) |
| | | 139 | | { |
| | 12 | 140 | | if (toast.Notification is not IClosableNotification closableNotification) |
| | 9 | 141 | | return; |
| | | 142 | | |
| | 3 | 143 | | _closeHandlers[toast.Notification.Id] = handler; |
| | 3 | 144 | | closableNotification.CloseRequested += handler; |
| | 3 | 145 | | return; |
| | | 146 | | |
| | | 147 | | void handler(object? sender, CloseRequestedEventArgs e) => Remove(toast); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | private void UnhookLifecycle(IToast toast) |
| | | 151 | | { |
| | 12 | 152 | | if (toast.Notification is IClosableNotification closableNotification |
| | 12 | 153 | | && _closeHandlers.TryGetValue(toast.Notification.Id, out var handler)) |
| | | 154 | | { |
| | 3 | 155 | | closableNotification.CloseRequested -= handler; |
| | 3 | 156 | | _closeHandlers.Remove(toast.Notification.Id); |
| | | 157 | | } |
| | | 158 | | |
| | 12 | 159 | | if (_lifetimeSubscriptions.TryGetValue(toast.Notification.Id, out var subscription)) |
| | | 160 | | { |
| | 0 | 161 | | subscription.Dispose(); |
| | 0 | 162 | | _lifetimeSubscriptions.Remove(toast.Notification.Id); |
| | | 163 | | } |
| | 12 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <inheritdoc /> |
| | 9 | 167 | | public void Remove(IToast toast) => _scheduler.Ui.Schedule(() => RemoveCore(toast)); |
| | | 168 | | |
| | | 169 | | private void RemoveCore(IToast toast) |
| | | 170 | | { |
| | 9 | 171 | | if (_isDisposed) |
| | 0 | 172 | | return; |
| | | 173 | | |
| | 9 | 174 | | if (!_visible.Remove(toast)) |
| | 0 | 175 | | return; |
| | | 176 | | |
| | 9 | 177 | | UnhookLifecycle(toast); |
| | 9 | 178 | | TryDequeue(); |
| | 9 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Attempts to dequeue the next notification from the priority queue and display it as a toast if there is capacity |
| | | 183 | | /// </summary> |
| | | 184 | | private void TryDequeue() |
| | | 185 | | { |
| | 12 | 186 | | while (_visible.Count < _options.MaxVisibleToasts && _queue.Count > 0) |
| | | 187 | | { |
| | 3 | 188 | | var next = _queue.Dequeue(); |
| | 3 | 189 | | Add(next); |
| | | 190 | | } |
| | 9 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <inheritdoc /> |
| | | 194 | | public void Clear() => |
| | 0 | 195 | | _scheduler.Ui.Schedule(() => |
| | 0 | 196 | | { |
| | 0 | 197 | | if (_isDisposed) |
| | 0 | 198 | | return; |
| | 0 | 199 | | |
| | 0 | 200 | | foreach (var toast in _visible.ToList()) |
| | 0 | 201 | | UnhookLifecycle(toast); |
| | 0 | 202 | | |
| | 0 | 203 | | _visible.Clear(); |
| | 0 | 204 | | _queue.Clear(); |
| | 0 | 205 | | }); |
| | | 206 | | |
| | | 207 | | /// <inheritdoc /> |
| | | 208 | | public void Dispose() |
| | | 209 | | { |
| | 12 | 210 | | if (_isDisposed) |
| | 0 | 211 | | return; |
| | | 212 | | |
| | 12 | 213 | | _isDisposed = true; |
| | 12 | 214 | | _subscription.Dispose(); |
| | | 215 | | |
| | 30 | 216 | | foreach (var toast in _visible.ToList()) |
| | 3 | 217 | | UnhookLifecycle(toast); |
| | | 218 | | |
| | 12 | 219 | | _visible.Clear(); |
| | 12 | 220 | | _queue.Clear(); |
| | 12 | 221 | | } |
| | | 222 | | } |
| | | 223 | | |