|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Concurrent;
|
| 3 | +using System.Collections.Generic; |
3 | 4 | using System.Threading;
|
4 | 5 | using System.Threading.Tasks;
|
5 | 6 | using Microsoft.Extensions.Logging;
|
6 |
| -using Microsoft.UI.Xaml; |
7 | 7 | using Microsoft.Windows.AppNotifications;
|
8 | 8 | using Microsoft.Windows.AppNotifications.Builder;
|
9 | 9 |
|
10 | 10 | namespace Coder.Desktop.App.Services;
|
11 | 11 |
|
12 |
| -public interface IUserNotifier : IAsyncDisposable |
| 12 | +public interface INotificationHandler |
13 | 13 | {
|
14 |
| - public Task ShowErrorNotification(string title, string message, CancellationToken ct = default); |
15 |
| - public Task ShowActionNotification(string title, string message, Action action, CancellationToken ct = default); |
| 14 | + public void HandleNotificationActivation(IDictionary<string, string> args); |
| 15 | +} |
| 16 | + |
| 17 | +public interface IUserNotifier : INotificationHandler, IAsyncDisposable |
| 18 | +{ |
| 19 | + public void RegisterHandler(string name, INotificationHandler handler); |
16 | 20 |
|
17 |
| - public void HandleActivation(AppNotificationActivatedEventArgs args); |
| 21 | + public Task ShowErrorNotification(string title, string message, CancellationToken ct = default); |
| 22 | + public Task ShowActionNotification(string title, string message, string handlerName, IDictionary<string, string>? args = null, CancellationToken ct = default); |
18 | 23 | }
|
19 | 24 |
|
20 |
| -public class UserNotifier(ILogger<UserNotifier> logger) : IUserNotifier |
| 25 | +public class UserNotifier(ILogger<UserNotifier> logger, IDispatcherQueueManager dispatcherQueueManager) : IUserNotifier |
21 | 26 | {
|
22 |
| - private const string CoderNotificationId = "CoderNotificationId"; |
| 27 | + private const string CoderNotificationHandler = "CoderNotificationHandler"; |
23 | 28 |
|
24 | 29 | private readonly AppNotificationManager _notificationManager = AppNotificationManager.Default;
|
25 | 30 |
|
26 |
| - public ConcurrentDictionary<string, Action> ActionHandlers { get; } = new(); |
| 31 | + private ConcurrentDictionary<string, INotificationHandler> Handlers { get; } = new(); |
27 | 32 |
|
28 | 33 | public ValueTask DisposeAsync()
|
29 | 34 | {
|
30 | 35 | return ValueTask.CompletedTask;
|
31 | 36 | }
|
32 | 37 |
|
| 38 | + public void RegisterHandler(string name, INotificationHandler handler) |
| 39 | + { |
| 40 | + if (handler is null) |
| 41 | + throw new ArgumentNullException(nameof(handler)); |
| 42 | + if (handler is IUserNotifier) |
| 43 | + throw new ArgumentException("Handler cannot be an IUserNotifier", nameof(handler)); |
| 44 | + if (string.IsNullOrWhiteSpace(name)) |
| 45 | + throw new ArgumentException("Name cannot be null or whitespace", nameof(name)); |
| 46 | + if (!Handlers.TryAdd(name, handler)) |
| 47 | + throw new InvalidOperationException($"A handler with the name '{name}' is already registered."); |
| 48 | + } |
| 49 | + |
33 | 50 | public Task ShowErrorNotification(string title, string message, CancellationToken ct = default)
|
34 | 51 | {
|
35 | 52 | var builder = new AppNotificationBuilder().AddText(title).AddText(message);
|
36 | 53 | _notificationManager.Show(builder.BuildNotification());
|
37 | 54 | return Task.CompletedTask;
|
38 | 55 | }
|
39 | 56 |
|
40 |
| - public Task ShowActionNotification(string title, string message, Action action, CancellationToken ct = default) |
| 57 | + public Task ShowActionNotification(string title, string message, string handlerName, IDictionary<string, string>? args = null, CancellationToken ct = default) |
41 | 58 | {
|
42 |
| - var id = Guid.NewGuid().ToString(); |
43 |
| - var notification = new AppNotificationBuilder() |
| 59 | + if (!Handlers.TryGetValue(handlerName, out _)) |
| 60 | + { |
| 61 | + logger.LogWarning("no action handler found for notification with name {HandlerName}, ignoring", handlerName); |
| 62 | + return Task.CompletedTask; |
| 63 | + } |
| 64 | + |
| 65 | + var builder = new AppNotificationBuilder() |
44 | 66 | .AddText(title)
|
45 | 67 | .AddText(message)
|
46 |
| - .AddArgument(CoderNotificationId, id) |
47 |
| - .BuildNotification(); |
48 |
| - ActionHandlers[id] = action; |
49 |
| - _notificationManager.Show(notification); |
| 68 | + .AddArgument(CoderNotificationHandler, handlerName); |
| 69 | + if (args != null) |
| 70 | + foreach (var arg in args) |
| 71 | + { |
| 72 | + if (arg.Key == CoderNotificationHandler) |
| 73 | + continue; |
| 74 | + builder.AddArgument(arg.Key, arg.Value); |
| 75 | + } |
| 76 | + |
| 77 | + _notificationManager.Show(builder.BuildNotification()); |
50 | 78 | return Task.CompletedTask;
|
51 | 79 | }
|
52 | 80 |
|
53 |
| - public void HandleActivation(AppNotificationActivatedEventArgs args) |
| 81 | + public void HandleNotificationActivation(IDictionary<string, string> args) |
54 | 82 | {
|
55 |
| - // Must not be an Action notification. |
56 |
| - if (!args.Arguments.TryGetValue(CoderNotificationId, out var id)) |
| 83 | + if (!args.TryGetValue(CoderNotificationHandler, out var handlerName)) |
| 84 | + // Not an action notification, ignore |
57 | 85 | return;
|
58 | 86 |
|
59 |
| - if (!ActionHandlers.TryRemove(id, out var action)) |
| 87 | + if (!Handlers.TryGetValue(handlerName, out var handler)) |
60 | 88 | {
|
61 |
| - logger.LogWarning("no action handler found for notification with ID {NotificationId}, ignoring", id); |
| 89 | + logger.LogWarning("no action handler '{HandlerName}' found for notification activation, ignoring", handlerName); |
62 | 90 | return;
|
63 | 91 | }
|
64 | 92 |
|
65 |
| - var dispatcherQueue = ((App)Application.Current).TrayWindow?.DispatcherQueue; |
66 |
| - if (dispatcherQueue == null) |
67 |
| - { |
68 |
| - logger.LogError("could not acquire DispatcherQueue for notification event handling, is TrayWindow active?"); |
69 |
| - return; |
70 |
| - } |
71 |
| - if (!dispatcherQueue.HasThreadAccess) |
72 |
| - { |
73 |
| - dispatcherQueue.TryEnqueue(RunAction); |
74 |
| - return; |
75 |
| - } |
76 |
| - |
77 |
| - RunAction(); |
78 |
| - |
79 |
| - return; |
80 |
| - |
81 |
| - void RunAction() |
| 93 | + dispatcherQueueManager.RunInUiThread(() => |
82 | 94 | {
|
83 | 95 | try
|
84 | 96 | {
|
85 |
| - action(); |
| 97 | + handler.HandleNotificationActivation(args); |
86 | 98 | }
|
87 | 99 | catch (Exception ex)
|
88 | 100 | {
|
89 |
| - logger.LogWarning(ex, "could not handle activation for notification with ID {NotificationId}", id); |
| 101 | + logger.LogWarning(ex, "could not handle activation for notification with handler '{HandlerName}", handlerName); |
90 | 102 | }
|
91 |
| - } |
| 103 | + }); |
92 | 104 | }
|
93 |
| - |
94 | 105 | }
|
0 commit comments