| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellNotificationsViewModel.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.Specialized; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Reactive.Disposables; |
| | | 11 | | using System.Windows.Input; |
| | | 12 | | using MyNet.Observable; |
| | | 13 | | using MyNet.Observable.Collections; |
| | | 14 | | using MyNet.UI.Commands; |
| | | 15 | | using MyNet.UI.Notifications; |
| | | 16 | | using MyNet.UI.Notifications.Models; |
| | | 17 | | using MyNet.UI.Threading; |
| | | 18 | | |
| | | 19 | | namespace MyNet.UI.ViewModels.Shell.Notifications; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// View model for the shell notifications panel (list, severity, close actions). |
| | | 23 | | /// Drawer visibility is controlled by the shell host notifications drawer. |
| | | 24 | | /// </summary> |
| | | 25 | | public sealed class ShellNotificationsViewModel : ViewModelBase |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="ShellNotificationsViewModel"/> class. |
| | | 29 | | /// </summary> |
| | 30 | 30 | | public ShellNotificationsViewModel( |
| | 30 | 31 | | INotificationsManager notificationsManager, |
| | 30 | 32 | | ISchedulerProvider schedulerProvider, |
| | 30 | 33 | | ICommandFactory? commandFactory = null) |
| | | 34 | | { |
| | 30 | 35 | | ArgumentNullException.ThrowIfNull(notificationsManager); |
| | 30 | 36 | | ArgumentNullException.ThrowIfNull(schedulerProvider); |
| | | 37 | | |
| | 30 | 38 | | var commands = commandFactory.GetOrDefault(); |
| | | 39 | | |
| | 30 | 40 | | Notifications = ExtendedCollection.Create<IClosableNotification>(schedulerProvider.Ui); |
| | | 41 | | |
| | 30 | 42 | | Notifications.AddRange(notificationsManager.Notifications.OfType<IClosableNotification>()); |
| | | 43 | | |
| | 30 | 44 | | INotifyCollectionChanged collection = notificationsManager.Notifications; |
| | 30 | 45 | | collection.CollectionChanged += collectionChanged; |
| | 30 | 46 | | Disposables.Add(Disposable.Create(() => collection.CollectionChanged -= collectionChanged)); |
| | | 47 | | |
| | 30 | 48 | | ExecuteActionCommand = commands.CreateRequired<ActionNotification>( |
| | 30 | 49 | | static notification => notification.Action?.Invoke(notification), |
| | 30 | 50 | | static notification => notification.Action is not null); |
| | | 51 | | |
| | 30 | 52 | | CloseCommand = commands.CreateRequired<IClosableNotification>(static n => n.RequestClose()); |
| | | 53 | | |
| | 30 | 54 | | ClearCommand = commands.Create( |
| | 30 | 55 | | () => Notifications.Where(static x => x.IsClosable).ToList().ForEach(static x => x.RequestClose()), |
| | 30 | 56 | | () => Notifications.Any(static x => x.IsClosable)); |
| | | 57 | | |
| | 30 | 58 | | Disposables.Add( |
| | 30 | 59 | | Notifications.ToObservableChangeSet().Subscribe(_ => |
| | 30 | 60 | | { |
| | 30 | 61 | | MaxSeverity = Notifications.Count != 0 |
| | 30 | 62 | | ? Notifications.OfType<MessageNotification>().Max(static x => x.Severity) |
| | 30 | 63 | | : null; |
| | 30 | 64 | | |
| | 30 | 65 | | HasNotifications = Notifications.Count > 0; |
| | 30 | 66 | | NotificationsChanged?.Invoke(this, EventArgs.Empty); |
| | 30 | 67 | | })); |
| | | 68 | | |
| | 30 | 69 | | RefreshNotificationState(); |
| | 30 | 70 | | return; |
| | | 71 | | void collectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => ApplyCollectionChange(e); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Raised when the notification list or <see cref="HasNotifications"/> changes. |
| | | 76 | | /// </summary> |
| | | 77 | | public event EventHandler? NotificationsChanged; |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the notifications bound to the panel. |
| | | 81 | | /// </summary> |
| | | 82 | | public ExtendedCollection<IClosableNotification> Notifications { get; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets a value indicating whether at least one notification is present. |
| | | 86 | | /// </summary> |
| | 63 | 87 | | public bool HasNotifications { get; private set => SetProperty(ref field, value); } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets the highest severity among <see cref="MessageNotification"/> items, if any. |
| | | 91 | | /// </summary> |
| | 63 | 92 | | public NotificationSeverity? MaxSeverity { get; private set => SetProperty(ref field, value); } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets the command that executes an actionable notification. |
| | | 96 | | /// </summary> |
| | | 97 | | public ICommand ExecuteActionCommand { get; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets the command that closes a single notification. |
| | | 101 | | /// </summary> |
| | | 102 | | public ICommand CloseCommand { get; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets the command that closes all closable notifications. |
| | | 106 | | /// </summary> |
| | | 107 | | public ICommand ClearCommand { get; } |
| | | 108 | | |
| | | 109 | | private void ApplyCollectionChange(NotifyCollectionChangedEventArgs e) |
| | | 110 | | { |
| | 3 | 111 | | switch (e.Action) |
| | | 112 | | { |
| | | 113 | | case NotifyCollectionChangedAction.Add: |
| | 3 | 114 | | Notifications.AddRange(e.NewItems!.Cast<INotification>().OfType<IClosableNotification>()); |
| | 3 | 115 | | break; |
| | | 116 | | |
| | | 117 | | case NotifyCollectionChangedAction.Remove: |
| | 0 | 118 | | foreach (var removed in e.OldItems!.Cast<INotification>()) |
| | | 119 | | { |
| | 0 | 120 | | var match = Notifications.FirstOrDefault(x => x.Id == removed.Id); |
| | 0 | 121 | | if (match is not null) |
| | 0 | 122 | | Notifications.Remove(match); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | break; |
| | | 126 | | |
| | | 127 | | case NotifyCollectionChangedAction.Reset: |
| | 0 | 128 | | Notifications.Clear(); |
| | | 129 | | break; |
| | | 130 | | } |
| | | 131 | | |
| | 3 | 132 | | RefreshNotificationState(); |
| | 3 | 133 | | } |
| | | 134 | | |
| | | 135 | | private void RefreshNotificationState() |
| | | 136 | | { |
| | 33 | 137 | | MaxSeverity = Notifications.Count != 0 |
| | 33 | 138 | | ? Notifications.OfType<MessageNotification>().Max(static x => x.Severity) |
| | 33 | 139 | | : null; |
| | | 140 | | |
| | 33 | 141 | | HasNotifications = Notifications.Count > 0; |
| | 33 | 142 | | NotificationsChanged?.Invoke(this, EventArgs.Empty); |
| | 0 | 143 | | } |
| | | 144 | | } |
| | | 145 | | |