< Summary

Information
Class: MyNet.UI.Notifications.NotificationBuilder
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Notifications/NotificationBuilder.cs
Tag: 323_28699572109
Line coverage
93%
Covered lines: 55
Uncovered lines: 4
Coverable lines: 59
Total lines: 176
Line coverage: 93.2%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
WithMessage(...)100%11100%
WithTitle(...)100%11100%
WithSeverity(...)100%11100%
AsSuccess()100%11100%
AsError()100%11100%
AsWarning()100%11100%
AsInformation()100%210%
Deduplicable()100%11100%
Closable(...)100%11100%
OnClick(...)100%11100%
WithClosingStrategy(...)100%11100%
WithFreezeOnMouseEnter(...)100%11100%
WithDuration(...)100%11100%
WithToastSettings(...)100%210%
Build()100%66100%
Publish()100%22100%
MergeToastSettings(...)87.5%88100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NotificationBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using MyNet.UI.Notifications.Models;
 9using MyNet.UI.Toasting.Settings;
 10
 11namespace MyNet.UI.Notifications;
 12
 13/// <summary>
 14/// Fluent builder for constructing and publishing notifications.
 15/// </summary>
 16public sealed class NotificationBuilder : INotificationBuilder
 17{
 18    private readonly INotificationPublisher? _publisher;
 19
 2720    private string _message = string.Empty;
 2721    private string _title = string.Empty;
 2722    private NotificationSeverity _severity = NotificationSeverity.Information;
 23    private bool _deduplicable;
 24    private bool _useClosableNotification;
 2725    private bool _isClosable = true;
 26    private Action<INotification>? _onClick;
 27    private ToastSettingsOverrides? _toastSettings;
 28
 29    /// <summary>
 30    /// Initializes a new instance of the <see cref="NotificationBuilder"/> class without an associated publisher.
 31    /// </summary>
 32    public NotificationBuilder()
 33    {
 1834    }
 35
 36    /// <summary>
 37    /// Initializes a new instance of the <see cref="NotificationBuilder"/> class bound to the given publisher.
 38    /// </summary>
 39    /// <param name="publisher">Publisher used by <see cref="Publish"/>.</param>
 940    public NotificationBuilder(INotificationPublisher publisher) => _publisher = publisher;
 41
 42    /// <inheritdoc />
 43    public INotificationBuilder WithMessage(string message)
 44    {
 2745        _message = message;
 2746        return this;
 47    }
 48
 49    /// <inheritdoc />
 50    public INotificationBuilder WithTitle(string title)
 51    {
 352        _title = title;
 353        return this;
 54    }
 55
 56    /// <inheritdoc />
 57    public INotificationBuilder WithSeverity(NotificationSeverity severity)
 58    {
 1559        _severity = severity;
 1560        return this;
 61    }
 62
 63    /// <inheritdoc />
 664    public INotificationBuilder AsSuccess() => WithSeverity(NotificationSeverity.Success);
 65
 66    /// <inheritdoc />
 667    public INotificationBuilder AsError() => WithSeverity(NotificationSeverity.Error);
 68
 69    /// <inheritdoc />
 370    public INotificationBuilder AsWarning() => WithSeverity(NotificationSeverity.Warning);
 71
 72    /// <inheritdoc />
 073    public INotificationBuilder AsInformation() => WithSeverity(NotificationSeverity.Information);
 74
 75    /// <inheritdoc />
 76    public INotificationBuilder Deduplicable()
 77    {
 378        _deduplicable = true;
 379        return this;
 80    }
 81
 82    /// <inheritdoc />
 83    public INotificationBuilder Closable(bool isClosable = true)
 84    {
 385        _useClosableNotification = true;
 386        _isClosable = isClosable;
 387        return this;
 88    }
 89
 90    /// <inheritdoc />
 91    public INotificationBuilder OnClick(Action<INotification> action)
 92    {
 693        _onClick = action;
 694        _useClosableNotification = true;
 695        return this;
 96    }
 97
 98    /// <inheritdoc />
 99    public INotificationBuilder WithClosingStrategy(ToastClosingStrategy closingStrategy)
 100    {
 3101        _toastSettings = MergeToastSettings(_toastSettings, new() { ClosingStrategy = closingStrategy });
 3102        return this;
 103    }
 104
 105    /// <inheritdoc />
 106    public INotificationBuilder WithFreezeOnMouseEnter(bool freezeOnMouseEnter)
 107    {
 3108        _toastSettings = MergeToastSettings(_toastSettings, new() { FreezeOnMouseEnter = freezeOnMouseEnter });
 3109        return this;
 110    }
 111
 112    /// <inheritdoc />
 113    public INotificationBuilder WithDuration(TimeSpan duration)
 114    {
 3115        _toastSettings = MergeToastSettings(_toastSettings, new() { Duration = duration });
 3116        return this;
 117    }
 118
 119    /// <inheritdoc />
 120    public INotificationBuilder WithToastSettings(ToastSettingsOverrides toastSettings)
 121    {
 0122        ArgumentNullException.ThrowIfNull(toastSettings);
 0123        _toastSettings = MergeToastSettings(_toastSettings, toastSettings);
 0124        return this;
 125    }
 126
 127    /// <inheritdoc />
 128    public INotification Build()
 129    {
 24130        if (_useClosableNotification || _onClick is not null)
 131        {
 9132            return new ActionNotification(_message, _title, _severity, _isClosable, _onClick)
 9133            {
 9134                ToastSettings = _toastSettings
 9135            };
 136        }
 137
 15138        if (_deduplicable)
 139        {
 3140            return new DeduplicableMessageNotification(_message, _title, _severity)
 3141            {
 3142                ToastSettings = _toastSettings
 3143            };
 144        }
 145
 12146        return new MessageNotification(_message, _title, _severity)
 12147        {
 12148            ToastSettings = _toastSettings
 12149        };
 150    }
 151
 152    /// <inheritdoc />
 153    public void Publish()
 154    {
 12155        if (_publisher is null)
 3156            throw new InvalidOperationException("Cannot publish: create the builder via INotificationPublisher.Notify() 
 157
 9158        _publisher.Publish(Build());
 9159    }
 160
 161    private static ToastSettingsOverrides MergeToastSettings(
 162        ToastSettingsOverrides? current,
 163        ToastSettingsOverrides update)
 164    {
 9165        if (current is null)
 3166            return update;
 167
 6168        return new()
 6169        {
 6170            ClosingStrategy = update.ClosingStrategy ?? current.ClosingStrategy,
 6171            FreezeOnMouseEnter = update.FreezeOnMouseEnter ?? current.FreezeOnMouseEnter,
 6172            Duration = update.Duration ?? current.Duration
 6173        };
 174    }
 175}
 176