Merge pull request #5740 from abpframework/blazor-message-refactor

Blazor message refactor
pull/5750/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit 0319cdc3bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,17 +1,18 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public interface IUiMessageService
{
Task InfoAsync(string message, string title = null, UiMessageOptions options = null);
Task InfoAsync(string message, string title = null, Action<UiMessageOptions> options = null);
Task SuccessAsync(string message, string title = null, UiMessageOptions options = null);
Task SuccessAsync(string message, string title = null, Action<UiMessageOptions> options = null);
Task WarnAsync(string message, string title = null, UiMessageOptions options = null);
Task WarnAsync(string message, string title = null, Action<UiMessageOptions> options = null);
Task ErrorAsync(string message, string title = null, UiMessageOptions options = null);
Task ErrorAsync(string message, string title = null, Action<UiMessageOptions> options = null);
Task<bool> ConfirmAsync(string message, string title = null, UiMessageOptions options = null);
Task<bool> ConfirmAsync(string message, string title = null, Action<UiMessageOptions> options = null);
}
}

@ -1,31 +1,32 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public class NullUiMessageService : IUiMessageService, ITransientDependency
{
public Task InfoAsync(string message, string title = null, UiMessageOptions options = null)
public Task InfoAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return Task.CompletedTask;
}
public Task SuccessAsync(string message, string title = null, UiMessageOptions options = null)
public Task SuccessAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return Task.CompletedTask;
}
public Task WarnAsync(string message, string title = null, UiMessageOptions options = null)
public Task WarnAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return Task.CompletedTask;
}
public Task ErrorAsync(string message, string title = null, UiMessageOptions options = null)
public Task ErrorAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return Task.CompletedTask;
}
public Task<bool> ConfirmAsync(string message, string title = null, UiMessageOptions options = null)
public Task<bool> ConfirmAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return Task.FromResult(true);
}

@ -6,9 +6,14 @@
public class UiMessageOptions
{
/// <summary>
/// If true, the message dialog will show the large icon for the current message type.
/// If true, the message dialogue will be centered on the screen.
/// </summary>
public bool ShowMessageIcon { get; set; } = true;
public bool CenterMessage { get; set; }
/// <summary>
/// If true, the message dialogue will show the large icon for the current message type.
/// </summary>
public bool ShowMessageIcon { get; set; }
/// <summary>
/// Overrides the build-in message icon.

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.JSInterop;
using Volo.Abp.DependencyInjection;
@ -14,27 +15,27 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly
JsRuntime = jsRuntime;
}
public async Task InfoAsync(string message, string title = null, UiMessageOptions options = null)
public async Task InfoAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
await JsRuntime.InvokeVoidAsync("alert", message);
}
public async Task SuccessAsync(string message, string title = null, UiMessageOptions options = null)
public async Task SuccessAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
await JsRuntime.InvokeVoidAsync("alert", message);
}
public async Task WarnAsync(string message, string title = null, UiMessageOptions options = null)
public async Task WarnAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
await JsRuntime.InvokeVoidAsync("alert", message);
}
public async Task ErrorAsync(string message, string title = null, UiMessageOptions options = null)
public async Task ErrorAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
await JsRuntime.InvokeVoidAsync("alert", message);
}
public async Task<bool> ConfirmAsync(string message, string title = null, UiMessageOptions options = null)
public async Task<bool> ConfirmAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return await JsRuntime.InvokeAsync<bool>("confirm", message);
}

@ -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;
@ -9,44 +12,85 @@ namespace Volo.Abp.BlazoriseUI
[Dependency(ReplaceServices = true)]
public class BlazoriseUiMessageService : IUiMessageService, IScopedDependency
{
private readonly UiMessageNotifierService uiMessageNotifierService;
/// <summary>
/// An event raised after the message is received. Used to notify the message dialog.
/// </summary>
public event EventHandler<UiMessageEventArgs> MessageReceived;
private readonly IStringLocalizer<AbpUiResource> localizer;
public ILogger<BlazoriseUiMessageService> Logger { get; set; }
public BlazoriseUiMessageService(UiMessageNotifierService uiMessageNotifierService)
public BlazoriseUiMessageService(
IStringLocalizer<AbpUiResource> localizer)
{
this.uiMessageNotifierService = uiMessageNotifierService;
this.localizer = localizer;
Logger = NullLogger<BlazoriseUiMessageService>.Instance;
}
public Task InfoAsync(string message, string title = null, UiMessageOptions options = null)
public Task InfoAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Info, message, title, options);
var uiMessageOptions = CreateDefaultOptions();
options?.Invoke(uiMessageOptions);
MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Info, message, title, uiMessageOptions));
return Task.CompletedTask;
}
public Task SuccessAsync(string message, string title = null, UiMessageOptions options = null)
public Task SuccessAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Success, message, title, options);
var uiMessageOptions = CreateDefaultOptions();
options?.Invoke(uiMessageOptions);
MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Success, message, title, uiMessageOptions));
return Task.CompletedTask;
}
public Task WarnAsync(string message, string title = null, UiMessageOptions options = null)
public Task WarnAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Warning, message, title, options);
var uiMessageOptions = CreateDefaultOptions();
options?.Invoke(uiMessageOptions);
MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Warning, message, title, uiMessageOptions));
return Task.CompletedTask;
}
public Task ErrorAsync(string message, string title = null, UiMessageOptions options = null)
public Task ErrorAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Error, message, title, options);
var uiMessageOptions = CreateDefaultOptions();
options?.Invoke(uiMessageOptions);
MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Error, message, title, uiMessageOptions));
return Task.CompletedTask;
}
public async Task<bool> ConfirmAsync(string message, string title = null, UiMessageOptions options = null)
public async Task<bool> ConfirmAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
var uiMessageOptions = CreateDefaultOptions();
options?.Invoke(uiMessageOptions);
var callback = new TaskCompletionSource<bool>();
await uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Confirmation, message, title, options, callback);
MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Confirmation, message, title, uiMessageOptions));
return await callback.Task;
}
protected virtual UiMessageOptions CreateDefaultOptions()
{
return new UiMessageOptions
{
CenterMessage = true,
ShowMessageIcon = true,
OkButtonText = localizer["Ok"],
CancelButtonText = localizer["Cancel"],
ConfirmButtonText = localizer["Yes"],
};
}
}
}

@ -1,6 +1,6 @@
<Modal @ref="@ModalRef">
<ModalBackdrop />
<ModalContent Centered="true">
<ModalContent Centered="@CenterMessage">
<ModalHeader>
<ModalTitle>
@Title

@ -9,68 +9,14 @@ namespace Volo.Abp.BlazoriseUI.Components
{
public partial class UiMessageAlert : ComponentBase, IDisposable
{
protected override void OnInitialized()
{
UiMessageNotifierService.MessageReceived += OnMessageReceived;
base.OnInitialized();
}
private void OnMessageReceived(object sender, UiMessageEventArgs e)
{
MessageType = e.MessageType;
Message = e.Message;
Title = e.Title;
Options = e.Options;
Callback = e.Callback;
ModalRef.Show();
}
public void Dispose()
{
if (UiMessageNotifierService != null)
{
UiMessageNotifierService.MessageReceived -= OnMessageReceived;
}
}
protected Task OnOkClicked()
{
ModalRef.Hide();
return Okayed.InvokeAsync(null);
}
protected Task OnConfirmClicked()
{
ModalRef.Hide();
if (IsConfirmation && Callback != null)
{
Callback.SetResult(true);
}
return Confirmed.InvokeAsync(null);
}
protected Task OnCancelClicked()
{
ModalRef.Hide();
if (IsConfirmation && Callback != null)
{
Callback.SetResult(false);
}
return Canceled.InvokeAsync(null);
}
protected Modal ModalRef { get; set; }
protected virtual bool IsConfirmation
=> MessageType == UiMessageType.Confirmation;
protected virtual bool CenterMessage
=> Options?.CenterMessage ?? true;
protected virtual bool ShowMessageIcon
=> Options?.ShowMessageIcon ?? true;
@ -126,12 +72,69 @@ namespace Volo.Abp.BlazoriseUI.Components
[Parameter] public UiMessageOptions Options { get; set; }
[Parameter] public EventCallback Okayed { get; set; } // TODO: ?
[Parameter] public EventCallback Okayed { get; set; }
[Parameter] public EventCallback Confirmed { get; set; }
[Parameter] public EventCallback Canceled { get; set; }
[Inject] protected UiMessageNotifierService UiMessageNotifierService { get; set; }
[Inject] protected BlazoriseUiMessageService UiMessageService { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
UiMessageService.MessageReceived += OnMessageReceived;
}
private void OnMessageReceived(object sender, UiMessageEventArgs e)
{
MessageType = e.MessageType;
Message = e.Message;
Title = e.Title;
Options = e.Options;
Callback = e.Callback;
ModalRef.Show();
}
public void Dispose()
{
if (UiMessageService != null)
{
UiMessageService.MessageReceived -= OnMessageReceived;
}
}
protected Task OnOkClicked()
{
ModalRef.Hide();
return Okayed.InvokeAsync(null);
}
protected Task OnConfirmClicked()
{
ModalRef.Hide();
if (IsConfirmation && Callback != null)
{
Callback.SetResult(true);
}
return Confirmed.InvokeAsync(null);
}
protected Task OnCancelClicked()
{
ModalRef.Hide();
if (IsConfirmation && Callback != null)
{
Callback.SetResult(false);
}
return Canceled.InvokeAsync(null);
}
}
}

@ -1,20 +0,0 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Components.WebAssembly;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.BlazoriseUI
{
[Dependency(ReplaceServices = true)]
public class UiMessageNotifierService : IScopedDependency
{
public event EventHandler<UiMessageEventArgs> MessageReceived;
public Task NotifyMessageReceivedAsync(UiMessageType messageType, string message, string title, UiMessageOptions options, TaskCompletionSource<bool> callback = null)
{
MessageReceived?.Invoke(this, new UiMessageEventArgs(messageType, message, title, options, callback));
return Task.CompletedTask;
}
}
}

@ -23,10 +23,10 @@ namespace MyCompanyName.MyProjectName.Blazor.Pages
Task OnInfoTestClicked()
{
return UiMessageService.InfoAsync( "This is the Info message", "Info", new UiMessageOptions
return UiMessageService.InfoAsync( "This is the Info message", "Info", options =>
{
OkButtonIcon = IconName.InfoCircle,
OkButtonText = "Hello info"
options.OkButtonIcon = IconName.InfoCircle;
options.OkButtonText = "Hello info";
} );
}
@ -47,7 +47,11 @@ namespace MyCompanyName.MyProjectName.Blazor.Pages
Task OnConfirmTestClicked()
{
return UiMessageService.ConfirmAsync( "This is the Confirm message", "Confirm" )
return UiMessageService.ConfirmAsync( "Are you sure you want to delete the item?", "Confirm", options =>
{
options.CancelButtonText = "Do not delete it";
options.ConfirmButtonText = "Yes I'm sure";
} )
.ContinueWith( result =>
{
if ( result.Result )

Loading…
Cancel
Save