Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions source/INotificationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Microsoft.JSInterop;
using System.Threading.Tasks;

namespace Append.Blazor.Notifications
{
Expand Down Expand Up @@ -26,15 +27,15 @@ public interface INotificationService
/// </summary>
/// <param name="title"></param>
/// <param name="options"></param>
/// <returns></returns>
ValueTask CreateAsync(string title, NotificationOptions options);
/// <returns>The created <see cref="Notification"/>.</returns>
ValueTask<Notification> CreateAsync(string title, NotificationOptions options);
/// <summary>
/// Creates a Notifcation with the supplied parameters.
/// </summary>
/// <param name="title">The title of the notification.</param>
/// <param name="description">The body or description of the notification.</param>
/// <param name="iconUrl">Link to a image, can be remote or served from the filesystem.</param>
/// <returns></returns>
ValueTask CreateAsync(string title, string description, string iconUrl = null);
/// <returns>The created <see cref="Notification"/>.</returns>
ValueTask<Notification> CreateAsync(string title, string description, string iconUrl = null);
}
}
31 changes: 27 additions & 4 deletions source/Notification.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using Microsoft.JSInterop;
using System;
using System.Threading.Tasks;

namespace Append.Blazor.Notifications
{
/// <summary>
/// A notification is an abstract representation of something that happened, such as the delivery of a message.
/// </summary>
public class Notification
public class Notification : IAsyncDisposable
{
public Guid Id { get; set; } = Guid.NewGuid();

/// <summary>
/// Defines a title for the notification, which will be shown at the top of the notification window when it is fired.
/// </summary>
public Guid Id { get; set; } = Guid.NewGuid();
public string Title { get; private set; }

/// <summary>
Expand Down Expand Up @@ -75,10 +78,14 @@ public class Notification
/// </summary>
public int TimeOut { get; private set; } = 5;

public Notification(string title, NotificationOptions options = null)
private readonly IJSObjectReference _jsObject;

internal Notification(string title, IJSObjectReference jsObject, NotificationOptions options = null)
{
if (string.IsNullOrWhiteSpace(title))
throw new ArgumentNullException($"{nameof(title)}, cannot be null or empty.");
if (jsObject is null)
throw new ArgumentNullException(nameof(jsObject));

Title = title;

Expand All @@ -101,6 +108,22 @@ public Notification(string title, NotificationOptions options = null)
NoScreen = options.NoScreen;
Sticky = options.Sticky;
TimeOut = options.TimeOut ?? TimeOut;

_jsObject = jsObject;
}

/// <summary>
/// Dismiss the notification.
/// </summary>
/// <returns></returns>
public ValueTask Close()
{
return _jsObject.InvokeVoidAsync("close");
}

public async ValueTask DisposeAsync()
{
await _jsObject.DisposeAsync();
}
}
}
10 changes: 6 additions & 4 deletions source/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public async ValueTask<PermissionType> RequestPermissionAsync()
}

/// <inheritdoc/>
public async ValueTask CreateAsync(string title, NotificationOptions options)
public async ValueTask<Notification> CreateAsync(string title, NotificationOptions options)
{
var module = await _moduleTask.Value;
await module.InvokeVoidAsync("create", title, options);
var jsObject = await module.InvokeAsync<IJSObjectReference>("create", title, options);
return new Notification(title, jsObject, options);
}

/// <inheritdoc/>
public async ValueTask CreateAsync(string title, string body, string icon = null)
public async ValueTask<Notification> CreateAsync(string title, string body, string icon = null)
{
var module = await _moduleTask.Value;

Expand All @@ -54,7 +55,8 @@ public async ValueTask CreateAsync(string title, string body, string icon = null
Icon = icon,
};

await module.InvokeVoidAsync("create", title, options);
var jsObject = await module.InvokeAsync<IJSObjectReference>("create", title, options);
return new Notification(title, jsObject, options);
}

public async ValueTask DisposeAsync()
Expand Down