Browse Source

Implemented UiNotificationService

pull/5973/head
Mladen Macanovic 6 years ago
parent
commit
b4f6ef6d03
  1. 1
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/MainLayout.razor
  2. 11
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiNotificationService.cs
  3. 11
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiNotificationService.cs
  4. 23
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationEventArgs.cs
  5. 18
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationOptions.cs
  6. 10
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationType.cs
  7. 62
      framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiNotificationService.cs
  8. 1
      framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor
  9. 71
      framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor.cs
  10. 1
      framework/src/Volo.Abp.BlazoriseUI/Volo.Abp.BlazoriseUI.csproj
  11. 3
      framework/src/Volo.Abp.BlazoriseUI/_Imports.razor
  12. 1
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html

1
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/MainLayout.razor

@ -22,4 +22,5 @@
@Body
<UiMessageAlert />
<UiNotificationAlert />
</div>

11
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiNotificationService.cs

@ -1,12 +1,13 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public interface IUiNotificationService
{
Task Info(string message);
Task Success(string message);
Task Warn(string message);
Task Error(string message);
Task Info(string message, string title = null, Action<UiNotificationOptions> options = null);
Task Success(string message, string title = null, Action<UiNotificationOptions> options = null);
Task Warn(string message, string title = null, Action<UiNotificationOptions> options = null);
Task Error(string message, string title = null, Action<UiNotificationOptions> options = null);
}
}

11
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiNotificationService.cs

@ -1,25 +1,26 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public class NullUiNotificationService : IUiNotificationService, ITransientDependency
{
public Task Info(string message)
public Task Info(string message, string title = null, Action<UiNotificationOptions> options = null)
{
return Task.CompletedTask;
}
public Task Success(string message)
public Task Success(string message, string title = null, Action<UiNotificationOptions> options = null)
{
return Task.CompletedTask;
}
public Task Warn(string message)
public Task Warn(string message, string title = null, Action<UiNotificationOptions> options = null)
{
return Task.CompletedTask;
}
public Task Error(string message)
public Task Error(string message, string title = null, Action<UiNotificationOptions> options = null)
{
return Task.CompletedTask;
}

23
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationEventArgs.cs

@ -0,0 +1,23 @@
using System;
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public class UiNotificationEventArgs : EventArgs
{
public UiNotificationEventArgs(UiNotificationType notificationType, string message, string title, UiNotificationOptions options)
{
NotificationType = notificationType;
Message = message;
Title = title;
Options = options;
}
public UiNotificationType NotificationType { get; set; }
public string Message { get; }
public string Title { get; }
public UiNotificationOptions Options { get; }
}
}

18
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationOptions.cs

@ -0,0 +1,18 @@
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
/// <summary>
/// Options to override notification appearance.
/// </summary>
public class UiNotificationOptions
{
/// <summary>
/// Custom text for the Ok button.
/// </summary>
public string OkButtonText { get; set; }
/// <summary>
/// Custom icon for the Ok button.
/// </summary>
public object OkButtonIcon { get; set; }
}
}

10
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationType.cs

@ -0,0 +1,10 @@
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public enum UiNotificationType
{
Info,
Success,
Warning,
Error,
}
}

62
framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiNotificationService.cs

@ -1,4 +1,7 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Localization.Resources.AbpUi;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.AspNetCore.Components.WebAssembly;
@ -7,34 +10,79 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.BlazoriseUI
{
[Dependency(ReplaceServices = true)]
public class BlazoriseUiNotificationService : IUiNotificationService, ITransientDependency
public class BlazoriseUiNotificationService : IUiNotificationService, IScopedDependency
{
/// <summary>
/// An event raised after the notification is received.
/// </summary>
public event EventHandler<UiNotificationEventArgs> NotificationReceived;
private readonly IStringLocalizer<AbpUiResource> localizer;
public ILogger<BlazoriseUiNotificationService> Logger { get; set; }
public BlazoriseUiNotificationService()
public BlazoriseUiNotificationService(
IStringLocalizer<AbpUiResource> localizer)
{
this.localizer = localizer;
Logger = NullLogger<BlazoriseUiNotificationService>.Instance;
}
public Task Info(string message)
public Task Info(string message, string title = null, Action<UiNotificationOptions> options = null)
{
var uiNotificationOptions = CreateDefaultOptions();
options?.Invoke(uiNotificationOptions);
Logger.LogInformation(message);
NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Info, message, title, uiNotificationOptions));
return Task.CompletedTask;
}
public Task Success(string message)
public Task Success(string message, string title = null, Action<UiNotificationOptions> options = null)
{
var uiNotificationOptions = CreateDefaultOptions();
options?.Invoke(uiNotificationOptions);
Logger.LogInformation(message);
NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Success, message, title, uiNotificationOptions));
return Task.CompletedTask;
}
public Task Warn(string message)
public Task Warn(string message, string title = null, Action<UiNotificationOptions> options = null)
{
var uiNotificationOptions = CreateDefaultOptions();
options?.Invoke(uiNotificationOptions);
Logger.LogWarning(message);
NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Warning, message, title, uiNotificationOptions));
return Task.CompletedTask;
}
public Task Error(string message)
public Task Error(string message, string title = null, Action<UiNotificationOptions> options = null)
{
var uiNotificationOptions = CreateDefaultOptions();
options?.Invoke(uiNotificationOptions);
Logger.LogError(message);
NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Error, message, title, uiNotificationOptions));
return Task.CompletedTask;
}
protected virtual UiNotificationOptions CreateDefaultOptions()
{
return new UiNotificationOptions
{
OkButtonText = localizer["Ok"],
};
}
}
}

1
framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor

@ -0,0 +1 @@
<SnackbarStack @ref="SnackbarStack" Location="SnackbarStackLocation.Right" Closed="@OnSnackbarClosed" />

71
framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor.cs

@ -0,0 +1,71 @@
using System;
using System.Threading.Tasks;
using Blazorise.Snackbar;
using Microsoft.AspNetCore.Components;
using Volo.Abp.AspNetCore.Components.WebAssembly;
namespace Volo.Abp.BlazoriseUI.Components
{
public partial class UiNotificationAlert : ComponentBase, IDisposable
{
protected SnackbarStack SnackbarStack { get; set; }
[Parameter] public UiNotificationType NotificationType { get; set; }
[Parameter] public string Message { get; set; }
[Parameter] public string Title { get; set; }
[Parameter] public UiNotificationOptions Options { get; set; }
[Parameter] public EventCallback Okayed { get; set; }
[Parameter] public EventCallback Closed { get; set; }
[Inject] protected BlazoriseUiNotificationService UiNotificationService { get; set; }
protected virtual SnackbarColor GetSnackbarColor(UiNotificationType notificationType)
{
return notificationType switch
{
UiNotificationType.Info => SnackbarColor.Info,
UiNotificationType.Success => SnackbarColor.Success,
UiNotificationType.Warning => SnackbarColor.Warning,
UiNotificationType.Error => SnackbarColor.Danger,
_ => SnackbarColor.None,
};
}
protected override void OnInitialized()
{
base.OnInitialized();
UiNotificationService.NotificationReceived += OnNotificationReceived;
}
protected virtual void OnNotificationReceived(object sender, UiNotificationEventArgs e)
{
NotificationType = e.NotificationType;
Message = e.Message;
Title = e.Title;
Options = e.Options;
SnackbarStack.Push(Message, GetSnackbarColor(e.NotificationType));
}
public virtual void Dispose()
{
if (UiNotificationService != null)
{
UiNotificationService.NotificationReceived -= OnNotificationReceived;
}
}
protected virtual Task OnSnackbarClosed(SnackbarClosedEventArgs eventArgs)
{
return eventArgs.CloseReason == SnackbarCloseReason.UserClosed
? Okayed.InvokeAsync()
: Closed.InvokeAsync();
}
}
}

1
framework/src/Volo.Abp.BlazoriseUI/Volo.Abp.BlazoriseUI.csproj

@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Blazorise" Version="0.9.2-preview14" />
<PackageReference Include="Blazorise.DataGrid" Version="0.9.2-preview14" />
<PackageReference Include="Blazorise.Snackbar" Version="0.9.2-preview14" />
</ItemGroup>
</Project>

3
framework/src/Volo.Abp.BlazoriseUI/_Imports.razor

@ -1,2 +1,3 @@
@using Microsoft.AspNetCore.Components.Web
@using Blazorise
@using Blazorise
@using Blazorise.Snackbar

1
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html

@ -12,6 +12,7 @@
<link href="_content/Blazorise/blazorise.css" rel="stylesheet" />
<link href="_content/Blazorise.Bootstrap/blazorise.bootstrap.css" rel="stylesheet" />
<link href="_content/Blazorise.Snackbar/blazorise.snackbar.css" rel="stylesheet" />
<link href="_content/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/libs/abp/css/theme.css" rel="stylesheet" />
<link href="main.css" rel="stylesheet" />
</head>

Loading…
Cancel
Save