| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NotificationPublisherExtensions.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.Globalization; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Globalization.Facade; |
| | | 12 | | using MyNet.Primitives; |
| | | 13 | | using MyNet.Primitives.Exceptions; |
| | | 14 | | using MyNet.UI.Notifications.Models; |
| | | 15 | | using MyNet.UI.ViewModels.Shell.Taskbar; |
| | | 16 | | |
| | | 17 | | #pragma warning disable IDE0130 |
| | | 18 | | namespace MyNet.UI.Notifications; |
| | | 19 | | #pragma warning restore IDE0130 |
| | | 20 | | |
| | | 21 | | public static class NotificationPublisherExtensions |
| | | 22 | | { |
| | | 23 | | extension(INotificationPublisher notificationPublisher) |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Starts a fluent notification builder bound to this publisher. |
| | | 27 | | /// </summary> |
| | 6 | 28 | | public INotificationBuilder Notify() => new NotificationBuilder(notificationPublisher); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Publishes a message notification with the given severity. |
| | | 32 | | /// </summary> |
| | | 33 | | public void PublishMessage(string message, |
| | | 34 | | NotificationSeverity severity, |
| | | 35 | | string title = "") => |
| | 54 | 36 | | notificationPublisher.Publish(new MessageNotification(message, title, severity)); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Publishes a success notification. |
| | | 40 | | /// </summary> |
| | | 41 | | public void PublishSuccess(string message, string title = "") => |
| | 3 | 42 | | notificationPublisher.PublishMessage(message, NotificationSeverity.Success, title); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Publishes an error notification. |
| | | 46 | | /// </summary> |
| | | 47 | | public void PublishError(string message, string title = "") => |
| | 39 | 48 | | notificationPublisher.PublishMessage(message, NotificationSeverity.Error, title); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Publishes a warning notification. |
| | | 52 | | /// </summary> |
| | | 53 | | public void PublishWarning(string message, string title = "") => |
| | 0 | 54 | | notificationPublisher.PublishMessage(message, NotificationSeverity.Warning, title); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Publishes an informational notification. |
| | | 58 | | /// </summary> |
| | | 59 | | public void PublishInformation(string message, string title = "") => |
| | 0 | 60 | | notificationPublisher.PublishMessage(message, NotificationSeverity.Information, title); |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Publishes each non-empty error message as an error notification. |
| | | 64 | | /// </summary> |
| | | 65 | | public void PublishErrors(IEnumerable<string> errors) |
| | | 66 | | { |
| | 15 | 67 | | ArgumentNullException.ThrowIfNull(errors); |
| | | 68 | | |
| | 72 | 69 | | foreach (var error in errors.Where(static x => !string.IsNullOrWhiteSpace(x))) |
| | 21 | 70 | | notificationPublisher.PublishError(error); |
| | 15 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Logs an exception and publishes it as a UI notification. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="exception">Exception to surface.</param> |
| | | 77 | | /// <param name="showInTaskBar">Whether to set the taskbar to an error state.</param> |
| | | 78 | | /// <param name="taskbarProgress">Optional taskbar progress source (from <c>AddShell()</c>).</param> |
| | | 79 | | /// <param name="reportTaskBar">Optional callback when <paramref name="taskbarProgress"/> is not provided.</para |
| | | 80 | | /// <param name="onClick">Optional action executed when notification is activated.</param> |
| | | 81 | | public void PublishException(Exception exception, |
| | | 82 | | bool showInTaskBar = false, |
| | | 83 | | ITaskbarProgressSource? taskbarProgress = null, |
| | | 84 | | Action<TaskbarProgressState, double?>? reportTaskBar = null, |
| | | 85 | | Action<INotification>? onClick = null) |
| | | 86 | | { |
| | 15 | 87 | | var innerException = exception.InnerException ?? exception; |
| | | 88 | | |
| | 15 | 89 | | if (showInTaskBar) |
| | | 90 | | { |
| | 6 | 91 | | if (taskbarProgress is not null) |
| | 3 | 92 | | taskbarProgress.SetError(); |
| | | 93 | | else |
| | 3 | 94 | | reportTaskBar?.Invoke(TaskbarProgressState.Error, 1); |
| | | 95 | | } |
| | | 96 | | |
| | 15 | 97 | | var message = innerException is TranslatableException translatableException |
| | 15 | 98 | | ? translatableException.Parameters.Count > 0 |
| | 15 | 99 | | ? translatableException.ResourceKey.Translate().FormatWith(CultureInfo.CurrentCulture, translatableE |
| | 15 | 100 | | : translatableException.ResourceKey.Translate() |
| | 15 | 101 | | : innerException.Message; |
| | | 102 | | |
| | 15 | 103 | | if (onClick is null) |
| | | 104 | | { |
| | 12 | 105 | | notificationPublisher.PublishError(message); |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 3 | 109 | | notificationPublisher.Notify() |
| | 3 | 110 | | .WithMessage(message) |
| | 3 | 111 | | .AsError() |
| | 3 | 112 | | .OnClick(onClick) |
| | 3 | 113 | | .Publish(); |
| | | 114 | | } |
| | 3 | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |