| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Pomodoro.Web.Models; |
| | | 4 | | |
| | | 5 | | namespace Pomodoro.Web.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Implementation of notification service using browser APIs |
| | | 9 | | /// </summary> |
| | | 10 | | public class NotificationService : INotificationService, IAsyncDisposable |
| | | 11 | | { |
| | | 12 | | private readonly IJSRuntime _jsRuntime; |
| | | 13 | | private readonly AppState _appState; |
| | | 14 | | private readonly ILogger<NotificationService> _logger; |
| | | 15 | | private DotNetObjectReference<NotificationService>? _dotNetRef; |
| | | 16 | | private Task? _initializationTask; |
| | 27 | 17 | | private readonly SemaphoreSlim _initSemaphore = new(1, 1); |
| | | 18 | | |
| | 48 | 19 | | public bool IsNotificationPermitted { get; private set; } |
| | | 20 | | |
| | | 21 | | public event Action<string>? OnNotificationAction; |
| | | 22 | | |
| | 27 | 23 | | public NotificationService(IJSRuntime jsRuntime, AppState appState, ILogger<NotificationService> logger) |
| | 27 | 24 | | { |
| | 27 | 25 | | _jsRuntime = jsRuntime; |
| | 27 | 26 | | _appState = appState; |
| | 27 | 27 | | _logger = logger; |
| | 27 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task InitializeAsync() |
| | 7 | 31 | | { |
| | | 32 | | // Use async semaphore to ensure only one initialization runs at a time |
| | | 33 | | // and concurrent callers wait for the same initialization to complete |
| | 7 | 34 | | await _initSemaphore.WaitAsync(); |
| | | 35 | | try |
| | 7 | 36 | | { |
| | | 37 | | // If initialization is already in progress or completed, return the existing task |
| | 7 | 38 | | if (_initializationTask != null) |
| | 1 | 39 | | { |
| | 1 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // Start initialization and store the task |
| | 6 | 44 | | _initializationTask = InitializeCoreAsync(); |
| | 6 | 45 | | await _initializationTask; |
| | 6 | 46 | | } |
| | | 47 | | finally |
| | 7 | 48 | | { |
| | 7 | 49 | | _initSemaphore.Release(); |
| | 7 | 50 | | } |
| | 7 | 51 | | } |
| | | 52 | | |
| | | 53 | | private async Task InitializeCoreAsync() |
| | 7 | 54 | | { |
| | | 55 | | try |
| | 7 | 56 | | { |
| | | 57 | | // Dispose existing reference if any (shouldn't happen, but safety check) |
| | 7 | 58 | | _dotNetRef?.Dispose(); |
| | | 59 | | |
| | | 60 | | // Create dotnet reference for JS callbacks |
| | 7 | 61 | | _dotNetRef = DotNetObjectReference.Create(this); |
| | 7 | 62 | | await _jsRuntime.InvokeVoidAsync(Constants.NotificationJsFunctions.RegisterDotNetRef, _dotNetRef); |
| | | 63 | | |
| | 6 | 64 | | await RequestPermissionAsync(); |
| | 6 | 65 | | } |
| | 1 | 66 | | catch (Exception ex) |
| | 1 | 67 | | { |
| | 1 | 68 | | _logger.LogError(ex, Constants.Messages.ErrorInitializingNotificationService); |
| | | 69 | | // Reset initialization task on failure to allow retry |
| | 1 | 70 | | _initializationTask = null; |
| | 1 | 71 | | } |
| | 7 | 72 | | } |
| | | 73 | | |
| | | 74 | | public async Task<bool> RequestPermissionAsync() |
| | 17 | 75 | | { |
| | | 76 | | try |
| | 17 | 77 | | { |
| | 17 | 78 | | var permission = await _jsRuntime.InvokeAsync<string>(Constants.NotificationJsFunctions.RequestPermission); |
| | 16 | 79 | | IsNotificationPermitted = permission == Constants.NotificationPermissions.Granted; |
| | 16 | 80 | | return IsNotificationPermitted; |
| | | 81 | | } |
| | 1 | 82 | | catch (Exception ex) |
| | 1 | 83 | | { |
| | 1 | 84 | | _logger.LogError(ex, Constants.Messages.ErrorRequestingNotificationPermission); |
| | 1 | 85 | | return false; |
| | | 86 | | } |
| | 17 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Refreshes the notification permission state from the browser. |
| | | 91 | | /// Call this when settings page opens to ensure UI shows current permission status. |
| | | 92 | | /// Note: This does not affect ShowNotificationAsync which checks permission directly in JS. |
| | | 93 | | /// </summary> |
| | | 94 | | public async Task RefreshPermissionStateAsync() |
| | 3 | 95 | | { |
| | | 96 | | try |
| | 3 | 97 | | { |
| | 3 | 98 | | var permission = await _jsRuntime.InvokeAsync<string>(Constants.NotificationJsFunctions.RequestPermission); |
| | 2 | 99 | | IsNotificationPermitted = permission == Constants.NotificationPermissions.Granted; |
| | 2 | 100 | | } |
| | 1 | 101 | | catch (Exception ex) |
| | 1 | 102 | | { |
| | 1 | 103 | | _logger.LogError(ex, Constants.Messages.ErrorRequestingNotificationPermission); |
| | 1 | 104 | | } |
| | 3 | 105 | | } |
| | | 106 | | |
| | | 107 | | public async Task ShowNotificationAsync(string title, string body, SessionType sessionType, string? icon = null) |
| | 7 | 108 | | { |
| | | 109 | | // Fast-fail if we know permission isn't granted (avoids unnecessary JS interop) |
| | 8 | 110 | | if (!IsNotificationPermitted) return; |
| | | 111 | | |
| | | 112 | | try |
| | 6 | 113 | | { |
| | | 114 | | // Pass session type as int (0 = Pomodoro, 1 = ShortBreak, 2 = LongBreak) |
| | 6 | 115 | | await _jsRuntime.InvokeVoidAsync(Constants.NotificationJsFunctions.ShowNotification, title, body, icon, (int |
| | 5 | 116 | | } |
| | 1 | 117 | | catch (Exception ex) |
| | 1 | 118 | | { |
| | 1 | 119 | | _logger.LogError(ex, Constants.Messages.ErrorShowingNotification); |
| | 1 | 120 | | } |
| | 7 | 121 | | } |
| | | 122 | | |
| | | 123 | | // Called from JavaScript when notification action is clicked |
| | | 124 | | [JSInvokable(Constants.JsInvokableMethods.OnNotificationActionClick)] |
| | | 125 | | public void OnNotificationActionClick(string action) |
| | 2 | 126 | | { |
| | 2 | 127 | | OnNotificationAction?.Invoke(action); |
| | 2 | 128 | | } |
| | | 129 | | |
| | | 130 | | public async Task PlayTimerCompleteSoundAsync() |
| | 2 | 131 | | { |
| | | 132 | | try |
| | 2 | 133 | | { |
| | 2 | 134 | | await _jsRuntime.InvokeVoidAsync(Constants.NotificationJsFunctions.PlayTimerCompleteSound); |
| | 1 | 135 | | } |
| | 1 | 136 | | catch (Exception ex) |
| | 1 | 137 | | { |
| | 1 | 138 | | _logger.LogWarning(ex, Constants.Messages.ErrorPlayingTimerCompleteSound); |
| | 1 | 139 | | } |
| | 2 | 140 | | } |
| | | 141 | | |
| | | 142 | | public async Task PlayBreakCompleteSoundAsync() |
| | 2 | 143 | | { |
| | | 144 | | try |
| | 2 | 145 | | { |
| | 2 | 146 | | await _jsRuntime.InvokeVoidAsync(Constants.NotificationJsFunctions.PlayBreakCompleteSound); |
| | 1 | 147 | | } |
| | 1 | 148 | | catch (Exception ex) |
| | 1 | 149 | | { |
| | 1 | 150 | | _logger.LogWarning(ex, Constants.Messages.ErrorPlayingBreakCompleteSound); |
| | 1 | 151 | | } |
| | 2 | 152 | | } |
| | | 153 | | |
| | | 154 | | public async ValueTask DisposeAsync() |
| | 3 | 155 | | { |
| | | 156 | | // Unregister the .NET reference from JavaScript |
| | | 157 | | try |
| | 3 | 158 | | { |
| | 3 | 159 | | await _jsRuntime.InvokeVoidAsync(Constants.NotificationJsFunctions.UnregisterDotNetRef); |
| | 2 | 160 | | } |
| | 1 | 161 | | catch |
| | 1 | 162 | | { |
| | | 163 | | // Ignore errors during disposal |
| | 1 | 164 | | } |
| | | 165 | | |
| | 3 | 166 | | _dotNetRef?.Dispose(); |
| | 3 | 167 | | _initSemaphore.Dispose(); |
| | 3 | 168 | | } |
| | | 169 | | } |