using System; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using OneOf; namespace AntDesign { /// /// AntNotification Service /// public class NotificationService { internal event Action OnConfiging; internal event Func OnNoticing; internal event Func, OneOf?, Task> OnUpdating; internal event Func OnClosing; internal event Action OnDestroying; public void Config(NotificationGlobalConfig config) { OnConfiging?.Invoke(config); } /// /// just create a NotificationRef without open it /// /// /// public Task CreateRefAsync([NotNull] NotificationConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } var notificationRef = new NotificationRef(this, config); return Task.FromResult(notificationRef); } internal async Task InternalOpen(NotificationConfig config) { if (OnNoticing != null) { if (string.IsNullOrWhiteSpace(config.Key)) { config.Key = Guid.NewGuid().ToString(); } await OnNoticing.Invoke(config); } } /// /// update a existed notification box /// /// /// /// /// public async Task UpdateAsync(string key, OneOf description, OneOf? message = null) { if (OnUpdating != null && !string.IsNullOrWhiteSpace(key)) { await OnUpdating.Invoke(key, description, message); } } /// /// Open a notification box /// /// public async Task Open([NotNull] NotificationConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } var notificationRef = await CreateRefAsync(config); await notificationRef.OpenAsync(); return notificationRef; } #region Api /// /// open a notification box with NotificationType.Success style /// /// public async Task Success(NotificationConfig config) { if (config == null) { return null; } config.NotificationType = NotificationType.Success; return await Open(config); } /// /// open a notification box with NotificationType.Error style /// /// public async Task Error(NotificationConfig config) { if (config == null) { return null; } config.NotificationType = NotificationType.Error; return await Open(config); } /// /// open a notification box with NotificationType.Info style /// /// public async Task Info(NotificationConfig config) { if (config == null) { return null; } config.NotificationType = NotificationType.Info; return await Open(config); } /// /// open a notification box with NotificationType.Warning style /// /// public async Task Warning(NotificationConfig config) { if (config == null) { return null; } config.NotificationType = NotificationType.Warning; await Open(config); return null; } /// /// Equivalent to Warning method /// /// public async Task Warn(NotificationConfig config) { return await Warning(config); } /// /// close notification by key /// /// /// public async Task Close(string key) { if (OnClosing != null) { await OnClosing.Invoke(key); } } /// /// destroy all Notification box /// public void Destroy() { OnDestroying?.Invoke(); } #endregion } }