< Summary

Information
Class: MyNet.UI.Notifications.NotificationPublisherExtensions
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Notifications/Extensions/NotificationPublisherExtensions.cs
Tag: 323_28699572109
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 118
Line coverage: 92.8%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Notify(...)100%11100%
PublishMessage(...)100%11100%
PublishSuccess(...)100%11100%
PublishError(...)100%11100%
PublishWarning(...)100%210%
PublishInformation(...)100%210%
PublishErrors(...)100%22100%
PublishException(...)85.71%1414100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Notifications/Extensions/NotificationPublisherExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NotificationPublisherExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Globalization;
 10using System.Linq;
 11using MyNet.Globalization.Facade;
 12using MyNet.Primitives;
 13using MyNet.Primitives.Exceptions;
 14using MyNet.UI.Notifications.Models;
 15using MyNet.UI.ViewModels.Shell.Taskbar;
 16
 17#pragma warning disable IDE0130
 18namespace MyNet.UI.Notifications;
 19#pragma warning restore IDE0130
 20
 21public static class NotificationPublisherExtensions
 22{
 23    extension(INotificationPublisher notificationPublisher)
 24    {
 25        /// <summary>
 26        /// Starts a fluent notification builder bound to this publisher.
 27        /// </summary>
 628        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 = "") =>
 5436            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 = "") =>
 342            notificationPublisher.PublishMessage(message, NotificationSeverity.Success, title);
 43
 44        /// <summary>
 45        /// Publishes an error notification.
 46        /// </summary>
 47        public void PublishError(string message, string title = "") =>
 3948            notificationPublisher.PublishMessage(message, NotificationSeverity.Error, title);
 49
 50        /// <summary>
 51        /// Publishes a warning notification.
 52        /// </summary>
 53        public void PublishWarning(string message, string title = "") =>
 054            notificationPublisher.PublishMessage(message, NotificationSeverity.Warning, title);
 55
 56        /// <summary>
 57        /// Publishes an informational notification.
 58        /// </summary>
 59        public void PublishInformation(string message, string title = "") =>
 060            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        {
 1567            ArgumentNullException.ThrowIfNull(errors);
 68
 7269            foreach (var error in errors.Where(static x => !string.IsNullOrWhiteSpace(x)))
 2170                notificationPublisher.PublishError(error);
 1571        }
 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        {
 1587            var innerException = exception.InnerException ?? exception;
 88
 1589            if (showInTaskBar)
 90            {
 691                if (taskbarProgress is not null)
 392                    taskbarProgress.SetError();
 93                else
 394                    reportTaskBar?.Invoke(TaskbarProgressState.Error, 1);
 95            }
 96
 1597            var message = innerException is TranslatableException translatableException
 1598                ? translatableException.Parameters.Count > 0
 1599                    ? translatableException.ResourceKey.Translate().FormatWith(CultureInfo.CurrentCulture, translatableE
 15100                    : translatableException.ResourceKey.Translate()
 15101                : innerException.Message;
 102
 15103            if (onClick is null)
 104            {
 12105                notificationPublisher.PublishError(message);
 106            }
 107            else
 108            {
 3109                notificationPublisher.Notify()
 3110                    .WithMessage(message)
 3111                    .AsError()
 3112                    .OnClick(onClick)
 3113                    .Publish();
 114            }
 3115        }
 116    }
 117}
 118