Skip to content

Commit

Permalink
Refactored UIMessageService and UIMessageAlert
Browse files Browse the repository at this point in the history
  • Loading branch information
stsrki committed Oct 8, 2020
1 parent de34016 commit ec186a4
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 102 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
74 changes: 51 additions & 23 deletions framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,85 @@ namespace Volo.Abp.BlazoriseUI
[Dependency(ReplaceServices = true)]
public class BlazoriseUiMessageService : IUiMessageService, IScopedDependency
{
private readonly UiMessageNotifierService uiMessageNotifierService;
private readonly IStringLocalizer<AbpUiResource> _localizer;
/// <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,
IStringLocalizer<AbpUiResource> localizer)
{
this.uiMessageNotifierService = uiMessageNotifierService;
_localizer = localizer;
this.localizer = localizer;

Logger = NullLogger<BlazoriseUiMessageService>.Instance;
}

public Task InfoAsync(string message, string title = null, Action<UiMessageOptions> optionsActions = null)
public Task InfoAsync(string message, string title = null, Action<UiMessageOptions> options = null)
{
var options = CreateDefaultOptions();
optionsActions?.Invoke(options);
return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Info, message, title, options);
}
var uiMessageOptions = CreateDefaultOptions();
options?.Invoke(uiMessageOptions);

protected virtual UiMessageOptions CreateDefaultOptions()
{
return new UiMessageOptions {
ConfirmButtonText = _localizer["Yes"]
};
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ namespace Volo.Abp.BlazoriseUI.Components
{
public partial class UiMessageAlert : ComponentBase, IDisposable
{
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;

protected virtual object MessageIcon => Options?.MessageIcon ?? MessageType switch
{
UiMessageType.Info => IconName.Info,
UiMessageType.Success => IconName.Check,
UiMessageType.Warning => IconName.Exclamation,
UiMessageType.Error => IconName.Times,
UiMessageType.Confirmation => IconName.QuestionCircle,
_ => null,
};

protected virtual string MessageIconColor => MessageType switch
{
// gets the color in the order of importance: Blazorise > Bootstrap > fallback color
UiMessageType.Info => "var(--b-theme-info, var(--info, #17a2b8))",
UiMessageType.Success => "var(--b-theme-success, var(--success, #28a745))",
UiMessageType.Warning => "var(--b-theme-warning, var(--warning, #ffc107))",
UiMessageType.Error => "var(--b-theme-danger, var(--danger, #dc3545))",
UiMessageType.Confirmation => "var(--b-theme-secondary, var(--secondary, #6c757d))",
_ => null,
};

protected virtual string MessageIconStyle
{
get
Expand Down Expand Up @@ -40,19 +72,19 @@ 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();

UiMessageNotifierService.MessageReceived += OnMessageReceived;
UiMessageService.MessageReceived += OnMessageReceived;
}

private void OnMessageReceived(object sender, UiMessageEventArgs e)
Expand All @@ -68,9 +100,9 @@ private void OnMessageReceived(object sender, UiMessageEventArgs e)

public void Dispose()
{
if (UiMessageNotifierService != null)
if (UiMessageService != null)
{
UiMessageNotifierService.MessageReceived -= OnMessageReceived;
UiMessageService.MessageReceived -= OnMessageReceived;
}
}

Expand Down Expand Up @@ -104,34 +136,5 @@ protected Task OnCancelClicked()

return Canceled.InvokeAsync(null);
}

protected Modal ModalRef { get; set; }

protected virtual bool IsConfirmation
=> MessageType == UiMessageType.Confirmation;

protected virtual bool ShowMessageIcon
=> Options?.ShowMessageIcon ?? true;

protected virtual object MessageIcon => Options?.MessageIcon ?? MessageType switch
{
UiMessageType.Info => IconName.Info,
UiMessageType.Success => IconName.Check,
UiMessageType.Warning => IconName.Exclamation,
UiMessageType.Error => IconName.Times,
UiMessageType.Confirmation => IconName.QuestionCircle,
_ => null,
};

protected virtual string MessageIconColor => MessageType switch
{
// gets the color in the order of importance: Blazorise > Bootstrap > fallback color
UiMessageType.Info => "var(--b-theme-info, var(--info, #17a2b8))",
UiMessageType.Success => "var(--b-theme-success, var(--success, #28a745))",
UiMessageType.Warning => "var(--b-theme-warning, var(--warning, #ffc107))",
UiMessageType.Error => "var(--b-theme-danger, var(--danger, #dc3545))",
UiMessageType.Confirmation => "var(--b-theme-secondary, var(--secondary, #6c757d))",
_ => null,
};
}
}
20 changes: 0 additions & 20 deletions framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ protected override async Task OnInitializedAsync()

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";
} );
}

Expand All @@ -47,7 +47,11 @@ Task OnErrorTestClicked()

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 )
Expand Down

0 comments on commit ec186a4

Please sign in to comment.