From dc473867267a3da8174634424b4584b6aa071382 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 20 Sep 2024 18:18:22 +0300 Subject: [PATCH] Adds basic notification system --- .../PresentationWebLayerServices.cs | 2 + .../Enums/Notifications/NotificationType.cs | 27 +++++ .../Common/Models/Common/NotificationModel.cs | 14 +++ .../Common/Services/INotificationService.cs | 26 +++++ .../Common/Services/NotificationService.cs | 29 +++++ .../Components/Notifications.razor | 95 ++++++++++++++++ .../Components/Notifications.razor.css | 107 ++++++++++++++++++ .../Layout/MainLayout.razor | 5 +- 8 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 src/Lumina.Presentation.Web/Common/Enums/Notifications/NotificationType.cs create mode 100644 src/Lumina.Presentation.Web/Common/Models/Common/NotificationModel.cs create mode 100644 src/Lumina.Presentation.Web/Common/Services/INotificationService.cs create mode 100644 src/Lumina.Presentation.Web/Common/Services/NotificationService.cs create mode 100644 src/Lumina.Presentation.Web/Components/Notifications.razor create mode 100644 src/Lumina.Presentation.Web/Components/Notifications.razor.css diff --git a/src/Lumina.Presentation.Web/Common/DependencyInjection/PresentationWebLayerServices.cs b/src/Lumina.Presentation.Web/Common/DependencyInjection/PresentationWebLayerServices.cs index 0ac63858..3509faa9 100644 --- a/src/Lumina.Presentation.Web/Common/DependencyInjection/PresentationWebLayerServices.cs +++ b/src/Lumina.Presentation.Web/Common/DependencyInjection/PresentationWebLayerServices.cs @@ -1,6 +1,7 @@ #region ========================================================================= USING ===================================================================================== using FluentValidation; using Lumina.Presentation.Web.Common.Api; +using Lumina.Presentation.Web.Common.Services; using Lumina.Presentation.Web.Core.Services.UI; using Microsoft.Extensions.DependencyInjection; using Polly; @@ -51,6 +52,7 @@ public static class PresentationWebLayerServices .AddPolicyHandler(circuitBreakerPolicy); services.AddSingleton(); + services.AddScoped(); return services; } diff --git a/src/Lumina.Presentation.Web/Common/Enums/Notifications/NotificationType.cs b/src/Lumina.Presentation.Web/Common/Enums/Notifications/NotificationType.cs new file mode 100644 index 00000000..1b0fbd0b --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Enums/Notifications/NotificationType.cs @@ -0,0 +1,27 @@ +namespace Lumina.Presentation.Web.Common.Enums.Notifications; + +/// +/// Enum for the types of notifications. +/// +public enum NotificationType +{ + /// + /// Informational notification. + /// + Info, + + /// + /// Success notification. + /// + Success, + + /// + /// Warning notification. + /// + Warning, + + /// + /// Error notification. + /// + Error +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Common/Models/Common/NotificationModel.cs b/src/Lumina.Presentation.Web/Common/Models/Common/NotificationModel.cs new file mode 100644 index 00000000..3a807b8f --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Models/Common/NotificationModel.cs @@ -0,0 +1,14 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Presentation.Web.Common.Enums.Notifications; +using System; +#endregion + +namespace Lumina.Presentation.Web.Common.Models.Common; + +/// +/// Model for notifications. +/// +/// The id of the notification. +/// The message of the notification. +/// The type of the notification. +public record struct NotificationModel(Guid Id, string Message, NotificationType Type); \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Common/Services/INotificationService.cs b/src/Lumina.Presentation.Web/Common/Services/INotificationService.cs new file mode 100644 index 00000000..1f015589 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Services/INotificationService.cs @@ -0,0 +1,26 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Presentation.Web.Common.Enums.Notifications; +using Lumina.Presentation.Web.Common.Models.Common; +using System; +#endregion + +namespace Lumina.Presentation.Web.Common.Services; + +/// +/// Interface for the service for showing notifications. +/// +public interface INotificationService +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + event Action? OnNotification; + #endregion + + #region ===================================================================== METHODS =================================================================================== + /// + /// Shows a notification. + /// + /// The message of the notification. + /// The type of the notification. + void Show(string message, NotificationType type); + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Common/Services/NotificationService.cs b/src/Lumina.Presentation.Web/Common/Services/NotificationService.cs new file mode 100644 index 00000000..6e2f0de8 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Services/NotificationService.cs @@ -0,0 +1,29 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Presentation.Web.Common.Enums.Notifications; +using Lumina.Presentation.Web.Common.Models.Common; +using System; +#endregion + +namespace Lumina.Presentation.Web.Common.Services; + +/// +/// Service for showing notifications. +/// +public class NotificationService : INotificationService +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + public event Action? OnNotification; + #endregion + + #region ===================================================================== METHODS =================================================================================== + /// + /// Shows a notification. + /// + /// The message of the notification. + /// The type of the notification. + public void Show(string message, NotificationType type) + { + OnNotification?.Invoke(new NotificationModel { Message = message, Type = type, Id = Guid.NewGuid() }); + } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Components/Notifications.razor b/src/Lumina.Presentation.Web/Components/Notifications.razor new file mode 100644 index 00000000..559941bf --- /dev/null +++ b/src/Lumina.Presentation.Web/Components/Notifications.razor @@ -0,0 +1,95 @@ +@using Lumina.Presentation.Web.Common.Enums.Notifications +@using Lumina.Presentation.Web.Common.Models.Common +@using Lumina.Presentation.Web.Common.Services + +@inject INotificationService NotificationService + +@implements IDisposable + +
+ @foreach (var notification in _notifications) + { +
+
+
+ @((MarkupString)notification.Message) + +
+
+ } +
+ +@code { + private List _notifications = []; + + /// + /// Initializes the component. + /// + protected override void OnInitialized() + { + NotificationService.OnNotification += AddNotification; + } + + /// + /// Adds a notification to the list of notifications and updates the UI. + /// + /// The notification to be added. + private void AddNotification(NotificationModel notification) + { + string logMessage = $"{notification.Type}: {notification.Message}"; + + if (notification.Type == NotificationType.Error) + Console.WriteLine($"Error: {notification.Message}"); + else if (notification.Type == NotificationType.Warning) + Console.WriteLine($"Warning: {notification.Message}"); + else if (notification.Type == NotificationType.Info) + Console.WriteLine($"Info: {notification.Message}"); + else if (notification.Type == NotificationType.Success) + Console.WriteLine($"Success: {notification.Message}"); + _notifications.Add(notification); + InvokeAsync(StateHasChanged); + _ = RemoveNotificationAfterDelay(notification.Id); + } + + /// + /// Removes a notification from the list of notifications after a delay of 5 seconds. + /// + /// The Id of the notification to be removed. + private async Task RemoveNotificationAfterDelay(Guid id) + { + await Task.Delay(5000); // 5 seconds delay + RemoveNotification(id); + } + + /// + /// Removes a notification from the list of notifications and updates the UI. + /// + /// The Id of the notification to be removed. + private void RemoveNotification(Guid id) + { + _notifications.RemoveAll(notification => notification.Id == id); + InvokeAsync(StateHasChanged); + } + + /// + /// Gets the CSS class for a notification based on its type. + /// + /// The type of the notification. + /// The CSS class for the notification. + private string GetNotificationClass(NotificationType type) => type switch + { + NotificationType.Info => "info", + NotificationType.Success => "success", + NotificationType.Warning => "warning", + NotificationType.Error => "error", + _ => string.Empty + }; + + /// + /// Disposes the component. + /// + public void Dispose() + { + NotificationService.OnNotification -= AddNotification; + } +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Components/Notifications.razor.css b/src/Lumina.Presentation.Web/Components/Notifications.razor.css new file mode 100644 index 00000000..86097050 --- /dev/null +++ b/src/Lumina.Presentation.Web/Components/Notifications.razor.css @@ -0,0 +1,107 @@ +.notification-container { + position: fixed; + bottom: 10px; + left: 20px; + z-index: 1000; + width: 300px; + max-height: calc(100vh - 40px); /* full viewport height minus 40px */ + overflow-y: auto; /* add vertical scrollbar if content exceeds max-height */ +} + +.notification { + border-radius: 4px; + margin-bottom: 10px; + margin-right: 10px; + display: flex; + flex-direction: row; /* stack content horizontally */ + align-items: flex-start; /* align items to the top */ + background: linear-gradient(135deg, #404040, #232323) !important; + border-top: 1px solid #505050 !important; + border-left: 1px solid #505050 !important; + border-right: 1px solid #000 !important; + border-bottom: 1px solid #000 !important; + box-shadow: 5px 5px 5px var(--shadow-color); + animation: fadeIn 0.3s, fadeOut 0.3s 4.7s; + + flex-direction: column; + flex-wrap: nowrap; + align-items: stretch; + justify-content: flex-start; + position: relative; +} + +.notification-body { + padding: 15px; + display: flex; + flex-direction: row; /* stack content horizontally */ + align-items: flex-start; /* align items to the top */ + word-wrap: break-word; /* allow long words to break and wrap */ + overflow-wrap: break-word; /* modern browsers */ + word-break: break-word; /* for very long words or URLs */ +} + +.notification-message { + flex-grow: 1; /* allow message to take up available space */ + margin-right: 3px; /* add some space between message and close button */ + overflow-wrap: break-word; /* modern browsers */ + word-break: break-word; /* for very long words or URLs */ + hyphens: auto; /* enable automatic hyphenation */ + white-space: pre-wrap; /* preserve line breaks */ + font-family: monospace; +} + +.close-button { + background: none; + color: var(--text-foreground); + border: none; + font-size: 20px; + cursor: pointer; + padding: 0; + line-height: 1; + flex-shrink: 0; /* prevent button from shrinking */ +} + +.shine-effect { + background: linear-gradient(to right, transparent, #fff, transparent); + width: 100%; + top: -1px; + height: 1px; + flex-shrink: 0; + pointer-events: none; /* ensure it doesn't interfere with interactions */ +} + +.info { + color: #2196F3; +} + +.success { + color: #4CAF50; +} + +.warning { + color: #FFC107; +} + +.error { + color: #F44336; +} + +@keyframes fadeIn { + from { + opacity: 0; + } + + to { + opacity: 1; + } +} + +@keyframes fadeOut { + from { + opacity: 1; + } + + to { + opacity: 0; + } +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Layout/MainLayout.razor b/src/Lumina.Presentation.Web/Layout/MainLayout.razor index e97f8f35..cf43eae0 100644 --- a/src/Lumina.Presentation.Web/Layout/MainLayout.razor +++ b/src/Lumina.Presentation.Web/Layout/MainLayout.razor @@ -1,4 +1,6 @@ -@inherits LayoutComponentBase +@using Lumina.Presentation.Web.Components + +@inherits LayoutComponentBase
@@ -8,3 +10,4 @@
+ \ No newline at end of file