Skip to content

Commit

Permalink
Merge pull request #5740 from abpframework/blazor-message-refactor
Browse files Browse the repository at this point in the history
Blazor message refactor
  • Loading branch information
hikalkan authored Oct 8, 2020
2 parents 5db7124 + ec186a4 commit 0319cdc
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.JSInterop;
using Volo.Abp.DependencyInjection;

Expand All @@ -14,27 +15,27 @@ public UiMessageService(IJSRuntime jsRuntime)
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);
}
Expand Down
72 changes: 58 additions & 14 deletions framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"],
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Modal @ref="@ModalRef">
<ModalBackdrop />
<ModalContent Centered="true">
<ModalContent Centered="@CenterMessage">
<ModalHeader>
<ModalTitle>
@Title
Expand Down
121 changes: 62 additions & 59 deletions framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -126,12 +72,69 @@ protected virtual string CancelButtonText

[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);
}
}
}
Loading

0 comments on commit 0319cdc

Please sign in to comment.