| | | 1 | | using Microsoft.AspNetCore.Components; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Pomodoro.Web.Models; |
| | | 4 | | using Pomodoro.Web.Services; |
| | | 5 | | |
| | | 6 | | namespace Pomodoro.Web.Components; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Code-behind for TimerDisplay component |
| | | 10 | | /// Reads directly from TimerService for real-time updates |
| | | 11 | | /// </summary> |
| | | 12 | | public class TimerDisplayBase : ComponentBase, IDisposable |
| | | 13 | | { |
| | | 14 | | #region Dependencies |
| | | 15 | | |
| | | 16 | | [Inject] |
| | 3192 | 17 | | protected ITimerService TimerService { get; set; } = default!; |
| | | 18 | | |
| | | 19 | | [Inject] |
| | 566 | 20 | | protected ILogger<TimerDisplayBase> Logger { get; set; } = default!; |
| | | 21 | | |
| | | 22 | | #endregion |
| | | 23 | | |
| | | 24 | | #region Parameters (for initial/override values only) |
| | | 25 | | |
| | | 26 | | [Parameter] |
| | 325 | 27 | | public TimeSpan? RemainingTime { get; set; } |
| | | 28 | | |
| | | 29 | | [Parameter] |
| | 325 | 30 | | public SessionType? SessionType { get; set; } |
| | | 31 | | |
| | | 32 | | [Parameter] |
| | 325 | 33 | | public bool? IsRunning { get; set; } |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Private Fields |
| | | 38 | | |
| | | 39 | | #endregion |
| | | 40 | | |
| | | 41 | | #region Properties - Always read from service for real-time updates |
| | | 42 | | |
| | 374 | 43 | | protected TimeSpan CurrentRemainingTime => TimerService.RemainingTime; |
| | 756 | 44 | | protected SessionType CurrentSessionType => TimerService.CurrentSessionType; |
| | 378 | 45 | | protected bool CurrentIsRunning => TimerService.IsRunning; |
| | | 46 | | |
| | | 47 | | #endregion |
| | | 48 | | |
| | | 49 | | #region Lifecycle Methods |
| | | 50 | | |
| | | 51 | | protected override void OnInitialized() |
| | 280 | 52 | | { |
| | | 53 | | // Subscribe to timer service events |
| | 280 | 54 | | TimerService.OnTick += OnTimerTick; |
| | 280 | 55 | | TimerService.OnStateChanged += OnTimerStateChanged; |
| | 280 | 56 | | } |
| | | 57 | | |
| | | 58 | | protected virtual void UpdateDisplay() |
| | 1 | 59 | | { |
| | 1 | 60 | | StateHasChanged(); |
| | 1 | 61 | | } |
| | | 62 | | |
| | 2 | 63 | | protected virtual void HandleStateChangeError() { } |
| | | 64 | | |
| | | 65 | | private async void OnTimerTick() |
| | 5 | 66 | | { |
| | | 67 | | try |
| | 5 | 68 | | { |
| | 5 | 69 | | await InvokeAsync(StateHasChanged); |
| | 4 | 70 | | } |
| | 1 | 71 | | catch (Exception ex) |
| | 1 | 72 | | { |
| | 1 | 73 | | Logger.LogError(ex, "Error in OnTimerTick"); |
| | 1 | 74 | | } |
| | 5 | 75 | | } |
| | | 76 | | |
| | | 77 | | private async void OnTimerStateChanged() |
| | 8 | 78 | | { |
| | | 79 | | try |
| | 8 | 80 | | { |
| | 8 | 81 | | await InvokeAsync(StateHasChanged); |
| | 7 | 82 | | } |
| | 1 | 83 | | catch (Exception ex) |
| | 1 | 84 | | { |
| | 1 | 85 | | Logger.LogError(ex, "Error in OnTimerStateChanged"); |
| | 1 | 86 | | } |
| | 8 | 87 | | } |
| | | 88 | | |
| | | 89 | | public void Dispose() |
| | 280 | 90 | | { |
| | 280 | 91 | | TimerService.OnTick -= OnTimerTick; |
| | 280 | 92 | | TimerService.OnStateChanged -= OnTimerStateChanged; |
| | 280 | 93 | | } |
| | | 94 | | |
| | | 95 | | #endregion |
| | | 96 | | |
| | | 97 | | #region Business Logic Methods |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Formats the remaining time as MM:SS |
| | | 101 | | /// </summary> |
| | | 102 | | protected string FormatTime(TimeSpan time) |
| | 374 | 103 | | { |
| | 374 | 104 | | return string.Format(Constants.TimeFormats.TimerFormat, (int)time.TotalMinutes, time.Seconds); |
| | 374 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets the display label for the current session type |
| | | 109 | | /// </summary> |
| | | 110 | | protected string GetSessionTypeLabel() |
| | 378 | 111 | | { |
| | 378 | 112 | | var sessionType = CurrentSessionType; |
| | 378 | 113 | | return sessionType switch |
| | 378 | 114 | | { |
| | 355 | 115 | | Models.SessionType.Pomodoro => Constants.SessionTypes.PomodoroUppercase, |
| | 12 | 116 | | Models.SessionType.ShortBreak => Constants.SessionTypes.ShortBreakUppercase, |
| | 8 | 117 | | Models.SessionType.LongBreak => Constants.SessionTypes.LongBreakUppercase, |
| | 3 | 118 | | _ => Constants.SessionTypes.PomodoroUppercase |
| | 378 | 119 | | }; |
| | 378 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets the CSS class based on timer state and session type |
| | | 124 | | /// Returns both session class and paused state to allow session color with reduced opacity |
| | | 125 | | /// </summary> |
| | | 126 | | protected string GetTimerClass() |
| | 378 | 127 | | { |
| | | 128 | | // Get session class (matches PIP window behavior) |
| | 378 | 129 | | var sessionClass = CurrentSessionType switch |
| | 378 | 130 | | { |
| | 355 | 131 | | Models.SessionType.Pomodoro => Constants.SessionTypes.PomodoroClass, |
| | 12 | 132 | | Models.SessionType.ShortBreak => Constants.SessionTypes.ShortBreakClass, |
| | 8 | 133 | | Models.SessionType.LongBreak => Constants.SessionTypes.LongBreakClass, |
| | 3 | 134 | | _ => Constants.SessionTypes.PomodoroClass |
| | 378 | 135 | | }; |
| | | 136 | | |
| | | 137 | | // Add paused class if not running (allows session color + reduced opacity) |
| | 378 | 138 | | if (!CurrentIsRunning) |
| | 357 | 139 | | { |
| | 357 | 140 | | return $"{sessionClass} {Constants.SessionTypes.PausedState}"; |
| | | 141 | | } |
| | | 142 | | |
| | 21 | 143 | | return sessionClass; |
| | 378 | 144 | | } |
| | | 145 | | |
| | | 146 | | #endregion |
| | | 147 | | } |