-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from happtim/main
notification center toolbar webassembly supports
- Loading branch information
Showing
9 changed files
with
191 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...bp.NotificationCenter.Blazor.WebAssembly/Pages/NotificationCenter/NotificationsTool.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@using Dignite.Abp.NotificationCenter.Blazor.Pages.NotificationCenter | ||
@using Volo.Abp.AspNetCore.Components | ||
@using Microsoft.AspNetCore.Components.Web | ||
|
||
@inherits AbpComponentBase | ||
|
||
<AuthorizeView> | ||
<Authorized> | ||
<Dropdown Display="Display.InlineBlock" RightAligned Margin="Margin.Is2.FromStart.Is2.FromEnd" VisibleChanged="OnVisibleChanged"> | ||
<DropdownToggle Color="Color.Light"> | ||
<BarIcon IconName="IconName.Bell"></BarIcon> | ||
@if (notificationCount > 0) | ||
{ | ||
<Badge Color="Color.Danger" Pill Position="Position.Absolute.Top.Is0.Start.Is100.Translate.Middle"> | ||
@notificationCount | ||
</Badge> | ||
} | ||
</DropdownToggle> | ||
<DropdownMenu Shadow="Shadow.Large"> | ||
<Div Flex="Flex.JustifyContent.Between.AlignItems.Center" Padding="Padding.Is3.OnX.Is2.OnY" Style="min-width:300px;"> | ||
@L["NotificationCenter"] | ||
<Icon Name="IconName.Bell" IconStyle="IconStyle.Regular" Clicked="OpenSubscribeModalAsync" title="@L["Subscribe"]"></Icon> | ||
</Div> | ||
<DropdownDivider></DropdownDivider> | ||
<Div Overflow="Overflow.Auto" Position="Position.Relative" Style="max-height:70vh;"> | ||
<NotificationsComponent @ref="NotificationsComponentRef" OnSetNotificationRead="SetNotificationRead"></NotificationsComponent> | ||
</Div> | ||
</DropdownMenu> | ||
</Dropdown> | ||
<SubscribeModal @ref="SubscribeModalRef"></SubscribeModal> | ||
</Authorized> | ||
|
||
<NotAuthorized> | ||
</NotAuthorized> | ||
|
||
</AuthorizeView> |
103 changes: 103 additions & 0 deletions
103
...NotificationCenter.Blazor.WebAssembly/Pages/NotificationCenter/NotificationsTool.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Dignite.Abp.NotificationCenter.Blazor.Pages.NotificationCenter; | ||
using Dignite.Abp.Notifications; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; | ||
using Microsoft.AspNetCore.SignalR.Client; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Dignite.Abp.NotificationCenter.Blazor.WebAssembly.Pages.NotificationCenter; | ||
public partial class NotificationsTool : IAsyncDisposable | ||
{ | ||
[Inject] | ||
public IAccessTokenProvider AccessTokenProvider { get; set; } = default!; | ||
|
||
[Inject] | ||
public IConfiguration Configuration { get; set; } = default!; | ||
|
||
[Inject] INotificationAppService NotificationAppService { get; set; } | ||
|
||
/// <summary> | ||
/// Hub Connection | ||
/// </summary> | ||
private HubConnection hubConnection; | ||
|
||
/// <summary> | ||
/// notification count | ||
/// </summary> | ||
private int notificationCount = 0; | ||
private bool hasNewNotifications = true; | ||
|
||
private SubscribeModal SubscribeModalRef; | ||
private NotificationsComponent NotificationsComponentRef; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
var baseUrl = Configuration.GetValue<string>("RemoteServices:NotificationCenter:BaseUrl"); | ||
|
||
if (baseUrl.IsNullOrEmpty()) | ||
{ | ||
baseUrl = Configuration.GetValue<string>("RemoteServices:Default:BaseUrl"); | ||
} | ||
|
||
if (CurrentUser.IsAuthenticated) | ||
{ | ||
(await AccessTokenProvider.RequestAccessToken()).TryGetToken(out var accessToken); | ||
|
||
hubConnection = new HubConnectionBuilder() | ||
.WithUrl(baseUrl.EnsureEndsWith('/') + "signalr-hubs/notifications", options => | ||
{ | ||
options.AccessTokenProvider = () => Task.FromResult((string?)accessToken!.Value); | ||
}) | ||
.Build(); | ||
|
||
notificationCount = await NotificationAppService.GetCountAsync(UserNotificationState.Unread); | ||
|
||
hubConnection.On<RealTimeNotifyEto>("ReceiveNotifications", async (eto) => | ||
{ | ||
notificationCount++; | ||
hasNewNotifications = true; | ||
await InvokeAsync(StateHasChanged); | ||
}); | ||
|
||
|
||
await hubConnection.StartAsync(); | ||
} | ||
|
||
await base.OnInitializedAsync(); | ||
} | ||
|
||
private async Task OpenSubscribeModalAsync() | ||
{ | ||
await SubscribeModalRef.OpenCreateModalAsync(); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (hubConnection is not null) | ||
{ | ||
await hubConnection.DisposeAsync(); | ||
} | ||
} | ||
|
||
private async Task OnVisibleChanged(bool isVisible) | ||
{ | ||
if (isVisible && hasNewNotifications) | ||
{ | ||
await NotificationsComponentRef.LoadNewNotificationsAsync(); | ||
hasNewNotifications = false; | ||
await InvokeAsync(StateHasChanged); | ||
} | ||
} | ||
|
||
private async Task SetNotificationRead() | ||
{ | ||
notificationCount--; | ||
await InvokeAsync(StateHasChanged); | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
.../src/Dignite.Abp.NotificationCenter.Blazor.WebAssembly/Toolbar/NotificationReminder.razor
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
...c/Dignite.Abp.NotificationCenter.Blazor.WebAssembly/Toolbar/NotificationReminder.razor.cs
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ion-center/src/Dignite.Abp.NotificationCenter.HttpApi.Client/NotificationDataConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Dignite.Abp.Notifications; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
|
||
namespace Dignite.Abp.NotificationCenter; | ||
|
||
public class NotificationDataConverter : JsonConverter<NotificationData> | ||
{ | ||
public override NotificationData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
using (JsonDocument doc = JsonDocument.ParseValue(ref reader)) | ||
{ | ||
JsonElement root = doc.RootElement; | ||
|
||
if (root.TryGetProperty("type", out JsonElement typeElement)) | ||
{ | ||
string fullTypeName = typeElement.GetString(); | ||
|
||
switch (fullTypeName) | ||
{ | ||
case "Dignite.Abp.Notifications.MessageNotificationData": | ||
return JsonSerializer.Deserialize<MessageNotificationData>(root.GetRawText(), options); | ||
case "Dignite.Abp.Notifications.LocalizableMessageNotificationData": | ||
return JsonSerializer.Deserialize<LocalizableMessageNotificationData>(root.GetRawText(), options); | ||
// TODO Other types of notifications should use a serialized derived class approach. | ||
default: | ||
throw new JsonException($"Unknown notification data type: {fullTypeName}"); | ||
} | ||
} | ||
|
||
throw new JsonException("Type property not found in notification data"); | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, NotificationData value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value, value.GetType(), options); | ||
} | ||
} | ||
|