Skip to content

Commit

Permalink
notification center toolbar webassembly supports
Browse files Browse the repository at this point in the history
  • Loading branch information
happtim committed Aug 3, 2024
1 parent d9ae0c9 commit aa8c61f
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.0" />
<PackageReference Include="Volo.Abp.AspNetCore.Components.WebAssembly.Theming" Version="7.4.5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Threading.Tasks;
using Dignite.Abp.NotificationCenter.Blazor.WebAssembly.Toolbar;
using Dignite.Abp.NotificationCenter.Blazor.WebAssembly.Pages.NotificationCenter;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Components.Web.Theming.Toolbars;
Expand All @@ -18,7 +18,7 @@ public Task ConfigureToolbarAsync(IToolbarConfigurationContext context)

if (authenticationStateProvider != null)
{
context.Toolbar.Items.Insert(0, new ToolbarItem(typeof(NotificationReminder)));
context.Toolbar.Items.Insert(0, new ToolbarItem(typeof(NotificationsTool)));
}
}

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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Http.Client;
using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;

Expand All @@ -21,5 +22,10 @@ public override void ConfigureServices(ServiceConfigurationContext context)
{
options.FileSets.AddEmbedded<DigniteAbpNotificationCenterHttpApiClientModule>();
});

Configure<AbpSystemTextJsonSerializerOptions>(options =>
{
options.JsonSerializerOptions.Converters.Add(new NotificationDataConverter());
});
}
}
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);
}
}

0 comments on commit aa8c61f

Please sign in to comment.