| | | 1 | | using System.Text.Json; |
| | | 2 | | using Microsoft.JSInterop; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Pomodoro.Web.Models; |
| | | 5 | | |
| | | 6 | | namespace Pomodoro.Web.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Service for Picture-in-Picture timer functionality |
| | | 10 | | /// Provides a floating, always-on-top timer window |
| | | 11 | | /// </summary> |
| | | 12 | | public class PipTimerService : IPipTimerService |
| | | 13 | | { |
| | | 14 | | private readonly IJSRuntime _jsRuntime; |
| | | 15 | | private readonly ITimerService _timerService; |
| | | 16 | | private readonly ITaskService _taskService; |
| | | 17 | | private readonly AppState _appState; |
| | | 18 | | private readonly ILogger<PipTimerService> _logger; |
| | | 19 | | private DotNetObjectReference<PipTimerService>? _dotNetRef; |
| | | 20 | | private bool _isInitialized; |
| | | 21 | | private bool _isDisposed; |
| | | 22 | | |
| | 22 | 23 | | public bool IsSupported { get; private set; } |
| | 46 | 24 | | public bool IsOpen { get; private set; } |
| | | 25 | | |
| | | 26 | | public event Action? OnPipOpened; |
| | | 27 | | public event Action? OnPipClosed; |
| | | 28 | | |
| | 49 | 29 | | public PipTimerService( |
| | 49 | 30 | | IJSRuntime jsRuntime, |
| | 49 | 31 | | ITimerService timerService, |
| | 49 | 32 | | ITaskService taskService, |
| | 49 | 33 | | AppState appState, |
| | 49 | 34 | | ILogger<PipTimerService> logger) |
| | 49 | 35 | | { |
| | 49 | 36 | | _jsRuntime = jsRuntime; |
| | 49 | 37 | | _timerService = timerService; |
| | 49 | 38 | | _taskService = taskService; |
| | 49 | 39 | | _appState = appState; |
| | 49 | 40 | | _logger = logger; |
| | 49 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task InitializeAsync() |
| | 10 | 44 | | { |
| | 11 | 45 | | if (_isInitialized) return; |
| | | 46 | | |
| | | 47 | | try |
| | 9 | 48 | | { |
| | | 49 | | // Check if PiP API is supported |
| | 9 | 50 | | IsSupported = await _jsRuntime.InvokeAsync<bool>(Constants.PipJsFunctions.IsSupported); |
| | | 51 | | |
| | | 52 | | // Create .NET reference for JS callbacks |
| | 8 | 53 | | _dotNetRef = DotNetObjectReference.Create(this); |
| | 8 | 54 | | await _jsRuntime.InvokeVoidAsync(Constants.PipJsFunctions.RegisterDotNetRef, _dotNetRef); |
| | | 55 | | |
| | | 56 | | // Subscribe to timer events to update PiP window |
| | 8 | 57 | | _timerService.OnTick += OnTimerTick; |
| | 8 | 58 | | _timerService.OnStateChanged += OnTimerStateChanged; |
| | | 59 | | |
| | 8 | 60 | | _isInitialized = true; |
| | | 61 | | |
| | 8 | 62 | | _logger.LogDebug(Constants.Messages.LogPipInitialized, IsSupported); |
| | 8 | 63 | | } |
| | 1 | 64 | | catch (Exception ex) |
| | 1 | 65 | | { |
| | 1 | 66 | | _logger.LogError(ex, Constants.Messages.LogPipInitializationFailed); |
| | 1 | 67 | | } |
| | 10 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task<bool> OpenAsync() |
| | 15 | 71 | | { |
| | 18 | 72 | | if (_isDisposed) return false; |
| | | 73 | | |
| | | 74 | | try |
| | 12 | 75 | | { |
| | 12 | 76 | | var timerState = GetTimerState(); |
| | 12 | 77 | | var success = await _jsRuntime.InvokeAsync<bool>(Constants.PipJsFunctions.Open, timerState); |
| | | 78 | | |
| | 11 | 79 | | if (success) |
| | 10 | 80 | | { |
| | 10 | 81 | | IsOpen = true; |
| | 10 | 82 | | OnPipOpened?.Invoke(); |
| | 10 | 83 | | } |
| | | 84 | | |
| | 11 | 85 | | return success; |
| | | 86 | | } |
| | 1 | 87 | | catch (Exception ex) |
| | 1 | 88 | | { |
| | 1 | 89 | | _logger.LogError(ex, Constants.Messages.LogPipOpenFailed); |
| | 1 | 90 | | return false; |
| | | 91 | | } |
| | 15 | 92 | | } |
| | | 93 | | |
| | | 94 | | public async Task CloseAsync() |
| | 13 | 95 | | { |
| | 23 | 96 | | if (_isDisposed) return; |
| | | 97 | | |
| | | 98 | | try |
| | 3 | 99 | | { |
| | 3 | 100 | | await _jsRuntime.InvokeVoidAsync(Constants.PipJsFunctions.Close); |
| | 2 | 101 | | IsOpen = false; |
| | 2 | 102 | | OnPipClosed?.Invoke(); |
| | 2 | 103 | | } |
| | 1 | 104 | | catch (Exception ex) |
| | 1 | 105 | | { |
| | 1 | 106 | | _logger.LogError(ex, Constants.Messages.LogPipCloseFailed); |
| | 1 | 107 | | } |
| | 13 | 108 | | } |
| | | 109 | | |
| | | 110 | | public async Task UpdateTimerAsync() |
| | 16 | 111 | | { |
| | 28 | 112 | | if (_isDisposed || !IsOpen) return; |
| | | 113 | | |
| | | 114 | | try |
| | 4 | 115 | | { |
| | 4 | 116 | | var timerState = GetTimerState(); |
| | 4 | 117 | | await _jsRuntime.InvokeVoidAsync(Constants.PipJsFunctions.Update, timerState); |
| | 3 | 118 | | } |
| | 1 | 119 | | catch (Exception ex) |
| | 1 | 120 | | { |
| | 1 | 121 | | _logger.LogError(ex, Constants.Messages.LogPipUpdateFailed); |
| | 1 | 122 | | } |
| | 16 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Starts the current session type timer. |
| | | 127 | | /// Only starts pomodoro if a task is selected. |
| | | 128 | | /// </summary> |
| | | 129 | | private async Task StartCurrentSessionAsync() |
| | 4 | 130 | | { |
| | 4 | 131 | | var sessionType = _timerService.CurrentSessionType; |
| | 4 | 132 | | if (sessionType == SessionType.Pomodoro) |
| | 2 | 133 | | { |
| | 2 | 134 | | if (_taskService.CurrentTaskId.HasValue) |
| | 1 | 135 | | { |
| | 1 | 136 | | await _timerService.StartPomodoroAsync(_taskService.CurrentTaskId.Value); |
| | 1 | 137 | | } |
| | 2 | 138 | | } |
| | 2 | 139 | | else if (sessionType == SessionType.ShortBreak) |
| | 1 | 140 | | { |
| | 1 | 141 | | await _timerService.StartShortBreakAsync(); |
| | 1 | 142 | | } |
| | 1 | 143 | | else if (sessionType == SessionType.LongBreak) |
| | 1 | 144 | | { |
| | 1 | 145 | | await _timerService.StartLongBreakAsync(); |
| | 1 | 146 | | } |
| | 4 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Gets the current timer state for the PiP window. |
| | | 151 | | /// Single source of truth for UI state - PiP uses pre-computed values. |
| | | 152 | | /// </summary> |
| | | 153 | | private object GetTimerState() |
| | 16 | 154 | | { |
| | 16 | 155 | | var isRunning = _timerService.IsRunning; |
| | 16 | 156 | | var isStarted = _timerService.IsStarted; |
| | | 157 | | |
| | 16 | 158 | | return new |
| | 16 | 159 | | { |
| | 16 | 160 | | remainingSeconds = _timerService.RemainingSeconds, |
| | 16 | 161 | | sessionType = (int)_timerService.CurrentSessionType, |
| | 16 | 162 | | isRunning = isRunning, |
| | 16 | 163 | | isStarted = isStarted, |
| | 16 | 164 | | // Single source of truth: showReset when timer is running OR was started |
| | 16 | 165 | | showReset = isRunning || isStarted, |
| | 16 | 166 | | taskName = _taskService.CurrentTask?.Name |
| | 16 | 167 | | }; |
| | 16 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Called from JavaScript when timer toggle is clicked in PiP window. |
| | | 172 | | /// Handles play/pause toggle and ensures PiP window state is synchronized. |
| | | 173 | | /// </summary> |
| | | 174 | | [JSInvokable(Constants.JsInvokableMethods.OnPipToggleTimer)] |
| | | 175 | | public async Task OnPipToggleTimer() |
| | 7 | 176 | | { |
| | | 177 | | try |
| | 7 | 178 | | { |
| | | 179 | | // Capture the operation type for logging |
| | 7 | 180 | | var operation = _timerService.IsRunning ? "pause" : |
| | 7 | 181 | | _timerService.IsPaused ? "resume" : "start"; |
| | | 182 | | |
| | 7 | 183 | | if (_timerService.IsRunning) |
| | 2 | 184 | | { |
| | 2 | 185 | | await _timerService.PauseAsync(); |
| | 1 | 186 | | } |
| | 5 | 187 | | else if (_timerService.IsPaused) |
| | 1 | 188 | | { |
| | 1 | 189 | | await _timerService.ResumeAsync(); |
| | 1 | 190 | | } |
| | | 191 | | else |
| | 4 | 192 | | { |
| | 4 | 193 | | await StartCurrentSessionAsync(); |
| | 4 | 194 | | } |
| | | 195 | | |
| | | 196 | | // Force immediate update with fresh state to ensure UI consistency. |
| | | 197 | | // This is critical because the event-based update via OnTimerStateChanged |
| | | 198 | | // uses SafeTaskRunner.RunAndForget which may complete before state is fully propagated. |
| | | 199 | | // We explicitly await here to ensure the PiP window receives the correct state. |
| | 6 | 200 | | await UpdateTimerAsync(); |
| | | 201 | | |
| | 6 | 202 | | _logger.LogDebug("PiP toggle operation '{Operation}' completed. IsRunning: {IsRunning}, IsStarted: {IsStarte |
| | 6 | 203 | | operation, _timerService.IsRunning, _timerService.IsStarted); |
| | 6 | 204 | | } |
| | 1 | 205 | | catch (Exception ex) |
| | 1 | 206 | | { |
| | 1 | 207 | | _logger.LogError(ex, Constants.Messages.LogPipToggleTimerError); |
| | 1 | 208 | | } |
| | 7 | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Called from JavaScript when reset is clicked in PiP window |
| | | 213 | | /// </summary> |
| | | 214 | | [JSInvokable(Constants.JsInvokableMethods.OnPipResetTimer)] |
| | | 215 | | public async Task OnPipResetTimer() |
| | 2 | 216 | | { |
| | | 217 | | try |
| | 2 | 218 | | { |
| | 2 | 219 | | await _timerService.ResetAsync(); |
| | 1 | 220 | | await UpdateTimerAsync(); |
| | 1 | 221 | | } |
| | 1 | 222 | | catch (Exception ex) |
| | 1 | 223 | | { |
| | 1 | 224 | | _logger.LogError(ex, Constants.Messages.LogPipResetTimerError); |
| | 1 | 225 | | } |
| | 2 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Called from JavaScript when session switch is clicked in PiP window |
| | | 230 | | /// </summary> |
| | | 231 | | [JSInvokable(Constants.JsInvokableMethods.OnPipSwitchSession)] |
| | | 232 | | public async Task OnPipSwitchSession(int sessionType) |
| | 4 | 233 | | { |
| | | 234 | | try |
| | 4 | 235 | | { |
| | 4 | 236 | | var type = (SessionType)sessionType; |
| | 4 | 237 | | await _timerService.SwitchSessionTypeAsync(type); |
| | 3 | 238 | | await UpdateTimerAsync(); |
| | 3 | 239 | | } |
| | 1 | 240 | | catch (Exception ex) |
| | 1 | 241 | | { |
| | 1 | 242 | | _logger.LogError(ex, Constants.Messages.LogPipSwitchSessionError); |
| | 1 | 243 | | } |
| | 4 | 244 | | } |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// Called from JavaScript when PiP window is closed |
| | | 248 | | /// </summary> |
| | | 249 | | [JSInvokable(Constants.JsInvokableMethods.OnPipClosed)] |
| | | 250 | | public void OnPipClosedJs() |
| | 4 | 251 | | { |
| | 4 | 252 | | IsOpen = false; |
| | 4 | 253 | | OnPipClosed?.Invoke(); |
| | 4 | 254 | | } |
| | | 255 | | |
| | | 256 | | private void OnTimerTick() |
| | 1 | 257 | | { |
| | 1 | 258 | | SafeTaskRunner.RunAndForget( |
| | 1 | 259 | | UpdateTimerAsync, |
| | 1 | 260 | | _logger, |
| | 1 | 261 | | Constants.SafeTaskOperations.PipTimerTick |
| | 1 | 262 | | ); |
| | 1 | 263 | | } |
| | | 264 | | |
| | | 265 | | private void OnTimerStateChanged() |
| | 1 | 266 | | { |
| | 1 | 267 | | SafeTaskRunner.RunAndForget( |
| | 1 | 268 | | UpdateTimerAsync, |
| | 1 | 269 | | _logger, |
| | 1 | 270 | | Constants.SafeTaskOperations.PipTimerStateChanged |
| | 1 | 271 | | ); |
| | 1 | 272 | | } |
| | | 273 | | |
| | | 274 | | public async ValueTask DisposeAsync() |
| | 11 | 275 | | { |
| | 12 | 276 | | if (_isDisposed) return; |
| | 10 | 277 | | _isDisposed = true; |
| | | 278 | | |
| | | 279 | | try |
| | 10 | 280 | | { |
| | 10 | 281 | | _timerService.OnTick -= OnTimerTick; |
| | 10 | 282 | | _timerService.OnStateChanged -= OnTimerStateChanged; |
| | | 283 | | |
| | 10 | 284 | | await _jsRuntime.InvokeVoidAsync(Constants.PipJsFunctions.UnregisterDotNetRef); |
| | 9 | 285 | | await CloseAsync(); |
| | 9 | 286 | | } |
| | 1 | 287 | | catch |
| | 1 | 288 | | { |
| | | 289 | | // Ignore errors during disposal |
| | 1 | 290 | | } |
| | | 291 | | |
| | 10 | 292 | | _dotNetRef?.Dispose(); |
| | 11 | 293 | | } |
| | | 294 | | } |