Adds basic notification system

This commit is contained in:
BinaryAlley committed 2024-09-20 18:18:22 +03:00
1 parent 493f34c883
commit dc47386726
8 files changed
+304 -1

No files matched your search

@@ -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<ComboboxService>();
services.AddScoped<INotificationService, NotificationService>();
return services;
}
@@ -0,0 +1,27 @@
namespace Lumina.Presentation.Web.Common.Enums.Notifications;
/// <summary>
/// Enum for the types of notifications.
/// </summary>
public enum NotificationType
{
/// <summary>
/// Informational notification.
/// </summary>
Info,
/// <summary>
/// Success notification.
/// </summary>
Success,
/// <summary>
/// Warning notification.
/// </summary>
Warning,
/// <summary>
/// Error notification.
/// </summary>
Error
}
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Enums.Notifications;
using System;
#endregion
namespace Lumina.Presentation.Web.Common.Models.Common;
/// <summary>
/// Model for notifications.
/// </summary>
/// <param name="Id">The id of the notification.</param>
/// <param name="Message">The message of the notification.</param>
/// <param name="Type">The type of the notification.</param>
public record struct NotificationModel(Guid Id, string Message, NotificationType Type);
@@ -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;
/// <summary>
/// Interface for the service for showing notifications.
/// </summary>
public interface INotificationService
{
#region ================================================================== FIELD MEMBERS ================================================================================
event Action<NotificationModel>? OnNotification;
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Shows a notification.
/// </summary>
/// <param name="message">The message of the notification.</param>
/// <param name="type">The type of the notification.</param>
void Show(string message, NotificationType type);
#endregion
}
@@ -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;
/// <summary>
/// Service for showing notifications.
/// </summary>
public class NotificationService : INotificationService
{
#region ================================================================== FIELD MEMBERS ================================================================================
public event Action<NotificationModel>? OnNotification;
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Shows a notification.
/// </summary>
/// <param name="message">The message of the notification.</param>
/// <param name="type">The type of the notification.</param>
public void Show(string message, NotificationType type)
{
OnNotification?.Invoke(new NotificationModel { Message = message, Type = type, Id = Guid.NewGuid() });
}
#endregion
}
@@ -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
<div class="notification-container">
@foreach (var notification in _notifications)
{
<div class="notification @GetNotificationClass(notification.Type)" @key="notification.Id">
<div class="shine-effect"></div>
<div class="notification-body">
<span class="notification-message">@((MarkupString)notification.Message)</span>
<button class="close-button" @onclick="() => RemoveNotification(notification.Id)">×</button>
</div>
</div>
}
</div>
@code {
private List<NotificationModel> _notifications = [];
/// <summary>
/// Initializes the component.
/// </summary>
protected override void OnInitialized()
{
NotificationService.OnNotification += AddNotification;
}
/// <summary>
/// Adds a notification to the list of notifications and updates the UI.
/// </summary>
/// <param name="notification">The notification to be added.</param>
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);
}
/// <summary>
/// Removes a notification from the list of notifications after a delay of 5 seconds.
/// </summary>
/// <param name="id">The Id of the notification to be removed.</param>
private async Task RemoveNotificationAfterDelay(Guid id)
{
await Task.Delay(5000); // 5 seconds delay
RemoveNotification(id);
}
/// <summary>
/// Removes a notification from the list of notifications and updates the UI.
/// </summary>
/// <param name="id">The Id of the notification to be removed.</param>
private void RemoveNotification(Guid id)
{
_notifications.RemoveAll(notification => notification.Id == id);
InvokeAsync(StateHasChanged);
}
/// <summary>
/// Gets the CSS class for a notification based on its type.
/// </summary>
/// <param name="type">The type of the notification.</param>
/// <returns>The CSS class for the notification.</returns>
private string GetNotificationClass(NotificationType type) => type switch
{
NotificationType.Info => "info",
NotificationType.Success => "success",
NotificationType.Warning => "warning",
NotificationType.Error => "error",
_ => string.Empty
};
/// <summary>
/// Disposes the component.
/// </summary>
public void Dispose()
{
NotificationService.OnNotification -= AddNotification;
}
}
@@ -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;
}
}
@@ -1,4 +1,6 @@
@inherits LayoutComponentBase
@using Lumina.Presentation.Web.Components
@inherits LayoutComponentBase
<div id="page">
<NavMenu />
@@ -8,3 +10,4 @@
</article>
</main>
</div>
<Notifications />