6 changed files
WPE.App.Services | ||
Push | ||
INotificationActionService.cs + | ||
IPlaygroundPush.cs + | ||
NotificationActionService.cs + | ||
ServiceCollectionExtensions.cs | ||
WPE.App.Services.csproj | ||
ReadMe.md | ||
INotificationActionService.cs
/WPE.App.Services/Push/INotificationActionService.cs+32/WPE.App.Services/Push/INotificationActionService.cs
Add comment 1 Plus namespace Infsoft.WPE.App.Services.Push
Add comment 2 Plus {
Add comment 3 Plus /// <summary>
Add comment 4 Plus /// Provides callbacks for actions triggered via notifications
Add comment 5 Plus /// </summary>
Add comment 6 Plus public interface INotificationActionService
Add comment 7 Plus {
Add comment 8 Plus /// <summary>
Add comment 9 Plus /// Subscribe for a push action type defined by <paramref name="action"/>
Add comment 10 Plus /// </summary>
Add comment 11 Plus /// <remarks>
Add comment 12 Plus /// Will override an existing callback
Add comment 13 Plus /// </remarks>
Add comment 14 Plus /// <param name="action">Action to subscribe for</param>
Add comment 15 Plus /// <param name="callback">Method to invoke, when the defined action is received</param>
Add comment 16 Plus void Subscribe(string action, Action<string?> callback);
Add comment 17 Plus
Add comment 18 Plus /// <summary>
Add comment 19 Plus /// Unsubscribe for a push action type
Add comment 20 Plus /// </summary>
Add comment 21 Plus /// <param name="action">Action to remove registered callback for</param>
Add comment 22 Plus void Unsubscribe(string action);
Add comment 23 Plus
Add comment 24 Plus /// <summary>
Add comment 25 Plus /// Call to invoke the corresponding event handlers
Add comment 26 Plus /// </summary>
Add comment 27 Plus /// <param name="action">Action to be performed</param>
Add comment 28 Plus /// <param name="payload">Payload of action, might be empty</param>
Add comment 29 Plus internal void TriggerAction(string action, string? payload);
Add comment 30 Plus }
Add comment 31 Plus }
Add comment 32 Plus
IPlaygroundPush.cs
/WPE.App.Services/Push/IPlaygroundPush.cs/WPE.App.Services/Push/IPlaygroundPush.cs
NotificationActionService.cs
/WPE.App.Services/Push/NotificationActionService.cs+48/WPE.App.Services/Push/NotificationActionService.cs
Add comment 1 Plus using Microsoft.Extensions.Logging;
Add comment 2 Plus
Add comment 3 Plus namespace Infsoft.WPE.App.Services.Push
Add comment 4 Plus {
Add comment 5 Plus /// <summary>
Add comment 6 Plus /// Implements <see cref="INotificationActionService"/>
Add comment 7 Plus /// </summary>
Add comment 8 Plus internal class NotificationActionService(ILogger<NotificationActionService> Logger) : INotificationActionService
Add comment 9 Plus {
Add comment 10 Plus private Dictionary<string, Action<string?>> ActionCallbacks { get; } = [];
Add comment 11 Plus
Add comment 12 Plus /// <inheritdoc/>
Add comment 13 Plus public void Subscribe(string action, Action<string?> callback)
Add comment 14 Plus {
Add comment 15 Plus if (ActionCallbacks.ContainsKey(action))
Add comment 16 Plus Logger.LogWarning("Action {ActionName} was already registered.", action);
Add comment 17 Plus
Add comment 18 Plus ActionCallbacks[action] = callback;
Add comment 19 Plus }
Add comment 20 Plus
Add comment 21 Plus /// <inheritdoc/>
Add comment 22 Plus public void Unsubscribe(string action)
Add comment 23 Plus {
Add comment 24 Plus ActionCallbacks.Remove(action);
Add comment 25 Plus }
Add comment 26 Plus
Add comment 27 Plus /// <inheritdoc/>
Add comment 28 Plus public void TriggerAction(string action, string? payload)
Add comment 29 Plus {
Add comment 30 Plus if (!ActionCallbacks.TryGetValue(action, out var callback))
Add comment 31 Plus {
Add comment 32 Plus Logger.LogDebug("No action callback found for {ActionName}", action);
Add comment 33 Plus return;
Add comment 34 Plus }
Add comment 35 Plus
Add comment 36 Plus try
Add comment 37 Plus {
Add comment 38 Plus Logger.LogDebug("Invoking action callback {ActionName}", action);
Add comment 39 Plus callback.DynamicInvoke(payload);
Add comment 40 Plus }
Add comment 41 Plus catch (Exception ex)
Add comment 42 Plus {
Add comment 43 Plus Logger.LogError(ex, "Failed invoking action callback");
Add comment 44 Plus }
Add comment 45 Plus }
Add comment 46 Plus }
Add comment 47 Plus }
Add comment 48 Plus
ServiceCollectionExtensions.cs
/WPE.App.Services/ServiceCollectionExtensions.cs/WPE.App.Services/ServiceCollectionExtensions.cs
WPE.App.Services.csproj
/WPE.App.Services/WPE.App.Services.csproj/WPE.App.Services/WPE.App.Services.csproj