From 8330e0bd21b6d4a94feacdc9a7137ba13139ad8a Mon Sep 17 00:00:00 2001
From: Mladen Macanovic
Date: Tue, 6 Oct 2020 11:22:45 +0200
Subject: [PATCH] WIP: Confirmation message implementation and started the
adding options and styles
---
.../WebAssembly/IUiMessageNotifierService.cs | 28 +++++-
.../WebAssembly/UiMessageOptions.cs | 19 ++++
.../BlazoriseUiMessageService.cs | 17 ++--
.../Components/UiMessageAlert.razor | 37 ++++++-
.../Components/UiMessageAlert.razor.cs | 97 +++++++++++++++++--
.../UiMessageNotifierService.cs | 11 ++-
.../Pages/Index.razor | 37 +++----
.../Pages/Index.razor.cs | 62 ++++++++++++
8 files changed, 264 insertions(+), 44 deletions(-)
create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs
create mode 100644 templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs
diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageNotifierService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageNotifierService.cs
index c3f6b917a7..309b73337b 100644
--- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageNotifierService.cs
+++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageNotifierService.cs
@@ -7,19 +7,43 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
event EventHandler MessageReceived;
- Task NotifyMessageReceived(string message, string title = null);
+ Task NotifyMessageReceivedAsync(UiMessageType messageType, string message, string title = null);
+
+ Task NotifyConfirmationReceivedAsync(string message, string title, TaskCompletionSource callback);
}
public class UiMessageEventArgs : EventArgs
{
- public UiMessageEventArgs(string message, string title)
+ public UiMessageEventArgs(UiMessageType messageType, string message, string title)
+ {
+ MessageType = messageType;
+ Message = message;
+ Title = title;
+ }
+
+ public UiMessageEventArgs(UiMessageType messageType, string message, string title, TaskCompletionSource callback)
{
+ MessageType = messageType;
Message = message;
Title = title;
+ Callback = callback;
}
+ public UiMessageType MessageType { get; set; }
+
public string Message { get; }
public string Title { get; }
+
+ public TaskCompletionSource Callback { get; }
+ }
+
+ public enum UiMessageType
+ {
+ Info,
+ Success,
+ Warning,
+ Error,
+ Confirmation,
}
}
diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs
new file mode 100644
index 0000000000..7909911c7e
--- /dev/null
+++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs
@@ -0,0 +1,19 @@
+namespace Volo.Abp.AspNetCore.Components.WebAssembly
+{
+ public class UiMessageOptions
+ {
+ public bool ShowMessageIcon { get; set; } = true;
+
+ public string OkButtonText { get; set; }
+
+ public object OkButtonIcon { get; set; }
+
+ public string ConfirmButtonText { get; set; }
+
+ public object ConfirmButtonIcon { get; set; }
+
+ public string CancelButtonText { get; set; }
+
+ public object CancelButtonIcon { get; set; }
+ }
+}
diff --git a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs
index 66bc231ce7..e7795f928d 100644
--- a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs
+++ b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
-using Blazorise;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.AspNetCore.Components.WebAssembly;
@@ -23,27 +22,31 @@ namespace Volo.Abp.BlazoriseUI
public Task InfoAsync(string message, string title = null)
{
- return uiMessageNotifierService.NotifyMessageReceived(message, title);
+ return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Info, message, title);
}
public Task SuccessAsync(string message, string title = null)
{
- return uiMessageNotifierService.NotifyMessageReceived(message, title);
+ return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Success, message, title);
}
public Task WarnAsync(string message, string title = null)
{
- return uiMessageNotifierService.NotifyMessageReceived(message, title);
+ return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Warning, message, title);
}
public Task ErrorAsync(string message, string title = null)
{
- return uiMessageNotifierService.NotifyMessageReceived(message, title);
+ return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Error, message, title);
}
- public Task ConfirmAsync(string message, string title = null)
+ public async Task ConfirmAsync(string message, string title = null)
{
- return Task.FromResult(true);
+ var callback = new TaskCompletionSource();
+
+ await uiMessageNotifierService.NotifyConfirmationReceivedAsync(message, title, callback);
+
+ return await callback.Task;
}
}
}
diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor
index e041053883..0bba328a51 100644
--- a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor
+++ b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor
@@ -1,17 +1,48 @@
-
+
@Title
+ @if ( ShowMessageIcon )
+ {
+
+
+
+ }
@Message
-
-
+ @if ( IsConfirmation )
+ {
+
+
+ }
+ else
+ {
+
+ }
\ No newline at end of file
diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs
index b5ee799bdd..dadab8135b 100644
--- a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs
+++ b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs
@@ -1,5 +1,4 @@
using System;
-using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Blazorise;
@@ -10,17 +9,21 @@ namespace Volo.Abp.BlazoriseUI.Components
{
public partial class UiMessageAlert : ComponentBase, IDisposable
{
+ private object messageIcon;
+
protected override void OnInitialized()
{
- UiMessageNotifierService.MessageReceived += UiMessageNotifierService_MessageReceived;
+ UiMessageNotifierService.MessageReceived += OnMessageReceived;
base.OnInitialized();
}
- private void UiMessageNotifierService_MessageReceived(object sender, UiMessageEventArgs e)
+ private void OnMessageReceived(object sender, UiMessageEventArgs e)
{
+ MessageType = e.MessageType;
Message = e.Message;
Title = e.Title;
+ Callback = e.Callback;
ModalRef.Show();
}
@@ -29,30 +32,106 @@ namespace Volo.Abp.BlazoriseUI.Components
{
if (UiMessageNotifierService != null)
{
- UiMessageNotifierService.MessageReceived -= UiMessageNotifierService_MessageReceived;
+ UiMessageNotifierService.MessageReceived -= OnMessageReceived;
}
}
- protected Task OnCancelClicked()
+ protected Task OnOkClicked()
{
ModalRef.Hide();
- return Task.CompletedTask;
+ return Okayed.InvokeAsync(null);
+ }
+
+ protected Task OnConfirmClicked()
+ {
+ ModalRef.Hide();
+
+ if (IsConfirmation && Callback != null)
+ {
+ Callback.SetResult(true);
+ }
+
+ return Confirmed.InvokeAsync(null);
}
- protected Task OnOKClicked()
+ protected Task OnCancelClicked()
{
ModalRef.Hide();
- return Task.CompletedTask;
+ if (IsConfirmation && Callback != null)
+ {
+ Callback.SetResult(false);
+ }
+
+ return Canceled.InvokeAsync(null);
}
protected Modal ModalRef { get; set; }
- [Inject] IUiMessageNotifierService UiMessageNotifierService { get; set; }
+ protected virtual bool IsConfirmation
+ => MessageType == UiMessageType.Confirmation;
+
+ protected virtual bool ShowMessageIcon
+ => Options?.ShowMessageIcon ?? true;
+
+ protected virtual object MessageIcon => MessageType switch
+ {
+ UiMessageType.Info => IconName.Info,
+ UiMessageType.Success => IconName.Check,
+ UiMessageType.Warning => IconName.Exclamation,
+ UiMessageType.Error => IconName.Stop,
+ UiMessageType.Confirmation => IconName.QuestionCircle,
+ _ => null,
+ };
+
+ protected virtual string MessageIconColor => MessageType switch
+ {
+ UiMessageType.Info => "#0000ff",
+ UiMessageType.Success => "#00ff00",
+ UiMessageType.Warning => "#ffae00",
+ UiMessageType.Error => "#e8301c",
+ UiMessageType.Confirmation => "#de692f",
+ _ => null,
+ };
+
+ protected virtual string MessageIconStyle
+ {
+ get
+ {
+ var sb = new StringBuilder();
+
+ sb.Append($"color:{MessageIconColor}");
+
+ return sb.ToString();
+ }
+ }
+
+ protected virtual string OkButtonText
+ => Options?.OkButtonText ?? "OK";
+
+ protected virtual string ConfirmButtonText
+ => Options?.ConfirmButtonText ?? "Confirm";
+
+ protected virtual string CancelButtonText
+ => Options?.CancelButtonText ?? "Cancel";
+
+ [Parameter] public UiMessageType MessageType { get; set; }
[Parameter] public string Title { get; set; }
[Parameter] public string Message { get; set; }
+
+ [Parameter] public TaskCompletionSource Callback { get; set; }
+
+ [Parameter] public UiMessageOptions Options { get; set; }
+
+ [Parameter] public EventCallback Okayed { get; set; } // TODO: ?
+
+ [Parameter] public EventCallback Confirmed { get; set; }
+
+ [Parameter] public EventCallback Canceled { get; set; }
+
+ [Inject] protected IUiMessageNotifierService UiMessageNotifierService { get; set; }
}
}
diff --git a/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs b/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs
index cdd86a75f3..3b97045a93 100644
--- a/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs
+++ b/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs
@@ -10,9 +10,16 @@ namespace Volo.Abp.BlazoriseUI
{
public event EventHandler MessageReceived;
- public Task NotifyMessageReceived(string message, string title = null)
+ public Task NotifyMessageReceivedAsync(UiMessageType messageType, string message, string title = null)
{
- MessageReceived?.Invoke(this, new UiMessageEventArgs(message, title));
+ MessageReceived?.Invoke(this, new UiMessageEventArgs(messageType, message, title));
+
+ return Task.CompletedTask;
+ }
+
+ public Task NotifyConfirmationReceivedAsync(string message, string title, TaskCompletionSource callback)
+ {
+ MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Confirmation, message, title, callback));
return Task.CompletedTask;
}
diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor
index 3f5e2f52fc..5a0d3dd08b 100644
--- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor
+++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor
@@ -1,7 +1,6 @@
@page "/"
@using Volo.Abp.Users
@using Volo.Abp.MultiTenancy
-@using System.Security.Claims
@using Microsoft.Extensions.Localization
@using MyCompanyName.MyProjectName.Localization
@using Volo.Abp.BlazoriseUI.Components
@@ -20,19 +19,31 @@
UI Message Tests
-
Welcome to the Application
-
+
@L["LongWelcomeMessage"]
- @if (!CurrentUser.IsAuthenticated)
+ @if ( !CurrentUser.IsAuthenticated )
{
@L["Login"]
@@ -171,20 +182,4 @@
abpframework
-
-
-@code
-{
- [Inject] IUiMessageService UiMessageService { get; set; }
-
- private IEnumerable _claims;
-
- protected override async Task OnInitializedAsync()
- {
- var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
- if (authState.User.Identity.IsAuthenticated)
- {
- _claims = authState.User.Claims;
- }
- }
-}
+
\ No newline at end of file
diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs
new file mode 100644
index 0000000000..8c5440e747
--- /dev/null
+++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Claims;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Components;
+using Volo.Abp.AspNetCore.Components.WebAssembly;
+
+namespace MyCompanyName.MyProjectName.Blazor.Pages
+{
+ public partial class Index
+ {
+ private IEnumerable _claims;
+
+ protected override async Task OnInitializedAsync()
+ {
+ var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+ if ( authState.User.Identity.IsAuthenticated )
+ {
+ _claims = authState.User.Claims;
+ }
+ }
+
+ Task OnInfoTestClicked()
+ {
+ return UiMessageService.InfoAsync( "This is the Info message", "Info" );
+ }
+
+ Task OnSuccessTestClicked()
+ {
+ return UiMessageService.SuccessAsync( "This is the Success message", "Success" );
+ }
+
+ Task OnWarningTestClicked()
+ {
+ return UiMessageService.WarnAsync( "This is the Warning message", "Warning" );
+ }
+
+ Task OnErrorTestClicked()
+ {
+ return UiMessageService.ErrorAsync( "This is the Error message", "Error" );
+ }
+
+ Task OnConfirmTestClicked()
+ {
+ return UiMessageService.ConfirmAsync( "This is the Confirm message", "Confirm" )
+ .ContinueWith( result =>
+ {
+ if ( result.Result )
+ {
+ Console.WriteLine( "Confirmed" );
+ }
+ else
+ {
+ Console.WriteLine( "Cancelled" );
+ }
+ } );
+ }
+
+ [Inject] IUiMessageService UiMessageService { get; set; }
+ }
+}