| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NotificationBuilder.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.UI.Notifications.Models; |
| | | 9 | | using MyNet.UI.Toasting.Settings; |
| | | 10 | | |
| | | 11 | | namespace MyNet.UI.Notifications; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Fluent builder for constructing and publishing notifications. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class NotificationBuilder : INotificationBuilder |
| | | 17 | | { |
| | | 18 | | private readonly INotificationPublisher? _publisher; |
| | | 19 | | |
| | 27 | 20 | | private string _message = string.Empty; |
| | 27 | 21 | | private string _title = string.Empty; |
| | 27 | 22 | | private NotificationSeverity _severity = NotificationSeverity.Information; |
| | | 23 | | private bool _deduplicable; |
| | | 24 | | private bool _useClosableNotification; |
| | 27 | 25 | | 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 | | { |
| | 18 | 34 | | } |
| | | 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> |
| | 9 | 40 | | public NotificationBuilder(INotificationPublisher publisher) => _publisher = publisher; |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public INotificationBuilder WithMessage(string message) |
| | | 44 | | { |
| | 27 | 45 | | _message = message; |
| | 27 | 46 | | return this; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public INotificationBuilder WithTitle(string title) |
| | | 51 | | { |
| | 3 | 52 | | _title = title; |
| | 3 | 53 | | return this; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public INotificationBuilder WithSeverity(NotificationSeverity severity) |
| | | 58 | | { |
| | 15 | 59 | | _severity = severity; |
| | 15 | 60 | | return this; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 6 | 64 | | public INotificationBuilder AsSuccess() => WithSeverity(NotificationSeverity.Success); |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | 6 | 67 | | public INotificationBuilder AsError() => WithSeverity(NotificationSeverity.Error); |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | 3 | 70 | | public INotificationBuilder AsWarning() => WithSeverity(NotificationSeverity.Warning); |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | 0 | 73 | | public INotificationBuilder AsInformation() => WithSeverity(NotificationSeverity.Information); |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public INotificationBuilder Deduplicable() |
| | | 77 | | { |
| | 3 | 78 | | _deduplicable = true; |
| | 3 | 79 | | return this; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public INotificationBuilder Closable(bool isClosable = true) |
| | | 84 | | { |
| | 3 | 85 | | _useClosableNotification = true; |
| | 3 | 86 | | _isClosable = isClosable; |
| | 3 | 87 | | return this; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | public INotificationBuilder OnClick(Action<INotification> action) |
| | | 92 | | { |
| | 6 | 93 | | _onClick = action; |
| | 6 | 94 | | _useClosableNotification = true; |
| | 6 | 95 | | return this; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | public INotificationBuilder WithClosingStrategy(ToastClosingStrategy closingStrategy) |
| | | 100 | | { |
| | 3 | 101 | | _toastSettings = MergeToastSettings(_toastSettings, new() { ClosingStrategy = closingStrategy }); |
| | 3 | 102 | | return this; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <inheritdoc /> |
| | | 106 | | public INotificationBuilder WithFreezeOnMouseEnter(bool freezeOnMouseEnter) |
| | | 107 | | { |
| | 3 | 108 | | _toastSettings = MergeToastSettings(_toastSettings, new() { FreezeOnMouseEnter = freezeOnMouseEnter }); |
| | 3 | 109 | | return this; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | | 113 | | public INotificationBuilder WithDuration(TimeSpan duration) |
| | | 114 | | { |
| | 3 | 115 | | _toastSettings = MergeToastSettings(_toastSettings, new() { Duration = duration }); |
| | 3 | 116 | | return this; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public INotificationBuilder WithToastSettings(ToastSettingsOverrides toastSettings) |
| | | 121 | | { |
| | 0 | 122 | | ArgumentNullException.ThrowIfNull(toastSettings); |
| | 0 | 123 | | _toastSettings = MergeToastSettings(_toastSettings, toastSettings); |
| | 0 | 124 | | return this; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <inheritdoc /> |
| | | 128 | | public INotification Build() |
| | | 129 | | { |
| | 24 | 130 | | if (_useClosableNotification || _onClick is not null) |
| | | 131 | | { |
| | 9 | 132 | | return new ActionNotification(_message, _title, _severity, _isClosable, _onClick) |
| | 9 | 133 | | { |
| | 9 | 134 | | ToastSettings = _toastSettings |
| | 9 | 135 | | }; |
| | | 136 | | } |
| | | 137 | | |
| | 15 | 138 | | if (_deduplicable) |
| | | 139 | | { |
| | 3 | 140 | | return new DeduplicableMessageNotification(_message, _title, _severity) |
| | 3 | 141 | | { |
| | 3 | 142 | | ToastSettings = _toastSettings |
| | 3 | 143 | | }; |
| | | 144 | | } |
| | | 145 | | |
| | 12 | 146 | | return new MessageNotification(_message, _title, _severity) |
| | 12 | 147 | | { |
| | 12 | 148 | | ToastSettings = _toastSettings |
| | 12 | 149 | | }; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <inheritdoc /> |
| | | 153 | | public void Publish() |
| | | 154 | | { |
| | 12 | 155 | | if (_publisher is null) |
| | 3 | 156 | | throw new InvalidOperationException("Cannot publish: create the builder via INotificationPublisher.Notify() |
| | | 157 | | |
| | 9 | 158 | | _publisher.Publish(Build()); |
| | 9 | 159 | | } |
| | | 160 | | |
| | | 161 | | private static ToastSettingsOverrides MergeToastSettings( |
| | | 162 | | ToastSettingsOverrides? current, |
| | | 163 | | ToastSettingsOverrides update) |
| | | 164 | | { |
| | 9 | 165 | | if (current is null) |
| | 3 | 166 | | return update; |
| | | 167 | | |
| | 6 | 168 | | return new() |
| | 6 | 169 | | { |
| | 6 | 170 | | ClosingStrategy = update.ClosingStrategy ?? current.ClosingStrategy, |
| | 6 | 171 | | FreezeOnMouseEnter = update.FreezeOnMouseEnter ?? current.FreezeOnMouseEnter, |
| | 6 | 172 | | Duration = update.Duration ?? current.Duration |
| | 6 | 173 | | }; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |