33using System . Collections . Generic ;
44using System . Threading ;
55using System . Threading . Tasks ;
6+ using Coder . Desktop . App . Views ;
67using Microsoft . Extensions . Logging ;
78using Microsoft . Windows . AppNotifications ;
89using Microsoft . Windows . AppNotifications . Builder ;
@@ -20,17 +21,40 @@ public interface IUserNotifier : INotificationHandler, IAsyncDisposable
2021 public void UnregisterHandler ( string name ) ;
2122
2223 public Task ShowErrorNotification ( string title , string message , CancellationToken ct = default ) ;
23- public Task ShowActionNotification ( string title , string message , string handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default ) ;
24+ /// <summary>
25+ /// This method allows to display a Windows-native notification with an action defined in
26+ /// <paramref name="handlerName"/> and provided <paramref name="args"/>.
27+ /// </summary>
28+ /// <param name="title">Title of the notification.</param>
29+ /// <param name="message">Message to be displayed in the notification body.</param>
30+ /// <param name="handlerName">Handler should be e.g. <c>nameof(Handler)</c> where <c>Handler</c>
31+ /// implements <see cref="Coder.Desktop.App.Services.INotificationHandler" />.
32+ /// If handler is <c>null</c> the action will open Coder Desktop.</param>
33+ /// <param name="args">Arguments to be provided to the handler when executing the action.</param>
34+ public Task ShowActionNotification ( string title , string message , string ? handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default ) ;
2435}
2536
26- public class UserNotifier ( ILogger < UserNotifier > logger , IDispatcherQueueManager dispatcherQueueManager ) : IUserNotifier
37+ public class UserNotifier : IUserNotifier
2738{
2839 private const string CoderNotificationHandler = "CoderNotificationHandler" ;
40+ private const string DefaultNotificationHandler = "DefaultNotificationHandler" ;
2941
3042 private readonly AppNotificationManager _notificationManager = AppNotificationManager . Default ;
43+ private readonly ILogger < UserNotifier > _logger ;
44+ private readonly IDispatcherQueueManager _dispatcherQueueManager ;
3145
3246 private ConcurrentDictionary < string , INotificationHandler > Handlers { get ; } = new ( ) ;
3347
48+ public UserNotifier ( ILogger < UserNotifier > logger , IDispatcherQueueManager dispatcherQueueManager ,
49+ INotificationHandler notificationHandler )
50+ {
51+ _logger = logger ;
52+ _dispatcherQueueManager = dispatcherQueueManager ;
53+ var defaultHandlerAdded = Handlers . TryAdd ( DefaultNotificationHandler , notificationHandler ) ;
54+ if ( ! defaultHandlerAdded )
55+ throw new Exception ( $ "UserNotifier failed to be initialized with { nameof ( DefaultNotificationHandler ) } ") ;
56+ }
57+
3458 public ValueTask DisposeAsync ( )
3559 {
3660 return ValueTask . CompletedTask ;
@@ -50,6 +74,8 @@ public void RegisterHandler(string name, INotificationHandler handler)
5074
5175 public void UnregisterHandler ( string name )
5276 {
77+ if ( name == nameof ( DefaultNotificationHandler ) )
78+ throw new InvalidOperationException ( $ "You cannot remove '{ name } '.") ;
5379 if ( ! Handlers . TryRemove ( name , out _ ) )
5480 throw new InvalidOperationException ( $ "No handler with the name '{ name } ' is registered.") ;
5581 }
@@ -61,8 +87,11 @@ public Task ShowErrorNotification(string title, string message, CancellationToke
6187 return Task . CompletedTask ;
6288 }
6389
64- public Task ShowActionNotification ( string title , string message , string handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default )
90+ public Task ShowActionNotification ( string title , string message , string ? handlerName , IDictionary < string , string > ? args = null , CancellationToken ct = default )
6591 {
92+ if ( handlerName == null )
93+ handlerName = nameof ( DefaultNotificationHandler ) ; // Use default handler if no handler name is provided
94+
6695 if ( ! Handlers . TryGetValue ( handlerName , out _ ) )
6796 throw new InvalidOperationException ( $ "No action handler with the name '{ handlerName } ' is registered.") ;
6897
@@ -90,19 +119,19 @@ public void HandleNotificationActivation(IDictionary<string, string> args)
90119
91120 if ( ! Handlers . TryGetValue ( handlerName , out var handler ) )
92121 {
93- logger . LogWarning ( "no action handler '{HandlerName}' found for notification activation, ignoring" , handlerName ) ;
122+ _logger . LogWarning ( "no action handler '{HandlerName}' found for notification activation, ignoring" , handlerName ) ;
94123 return ;
95124 }
96125
97- dispatcherQueueManager . RunInUiThread ( ( ) =>
126+ _dispatcherQueueManager . RunInUiThread ( ( ) =>
98127 {
99128 try
100129 {
101130 handler . HandleNotificationActivation ( args ) ;
102131 }
103132 catch ( Exception ex )
104133 {
105- logger . LogWarning ( ex , "could not handle activation for notification with handler '{HandlerName}" , handlerName ) ;
134+ _logger . LogWarning ( ex , "could not handle activation for notification with handler '{HandlerName}" , handlerName ) ;
106135 }
107136 } ) ;
108137 }
0 commit comments