| | | 1 | | using Microsoft.AspNetCore.Components; |
| | | 2 | | using Microsoft.AspNetCore.Components.Web; |
| | | 3 | | using Pomodoro.Web.Models; |
| | | 4 | | using Pomodoro.Web.Services; |
| | | 5 | | |
| | | 6 | | namespace Pomodoro.Web.Components; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Code-behind for ConsentModal component |
| | | 10 | | /// Separates business logic from view |
| | | 11 | | /// </summary> |
| | | 12 | | public class ConsentModalBase : ComponentBase |
| | | 13 | | { |
| | | 14 | | #region Parameters (Model) |
| | | 15 | | |
| | | 16 | | [Parameter] |
| | 691 | 17 | | public bool IsVisible { get; set; } |
| | | 18 | | |
| | | 19 | | [Parameter] |
| | 413 | 20 | | public SessionType CompletedSessionType { get; set; } |
| | | 21 | | |
| | | 22 | | [Parameter] |
| | 1105 | 23 | | public int CountdownSeconds { get; set; } |
| | | 24 | | |
| | | 25 | | [Parameter] |
| | 639 | 26 | | public List<ConsentOption> Options { get; set; } = new(); |
| | | 27 | | |
| | | 28 | | [Parameter] |
| | 327 | 29 | | public EventCallback<SessionType> OnOptionSelected { get; set; } |
| | | 30 | | |
| | | 31 | | #endregion |
| | | 32 | | |
| | | 33 | | #region Constants |
| | | 34 | | |
| | | 35 | | /// <summary>Maximum countdown seconds for the consent modal (used for progress bar calculation)</summary> |
| | | 36 | | private int _initialCountdownSeconds; |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Lifecycle Methods |
| | | 41 | | |
| | | 42 | | protected override void OnParametersSet() |
| | 346 | 43 | | { |
| | | 44 | | // Track the initial countdown value for progress bar calculation |
| | | 45 | | // Only update if the countdown was reset (back to a higher value) |
| | 346 | 46 | | if (CountdownSeconds > _initialCountdownSeconds) |
| | 23 | 47 | | { |
| | 23 | 48 | | _initialCountdownSeconds = CountdownSeconds; |
| | 23 | 49 | | } |
| | | 50 | | |
| | | 51 | | // If countdown is 0, reset for next time |
| | 346 | 52 | | if (CountdownSeconds <= 0) |
| | 323 | 53 | | { |
| | 323 | 54 | | _initialCountdownSeconds = 0; |
| | 323 | 55 | | } |
| | 346 | 56 | | } |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region Business Logic Methods |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the icon for the completed session type |
| | | 64 | | /// </summary> |
| | | 65 | | protected string GetIcon() |
| | 23 | 66 | | { |
| | 23 | 67 | | return CompletedSessionType switch |
| | 23 | 68 | | { |
| | 16 | 69 | | SessionType.Pomodoro => Constants.SessionTypes.PomodoroEmoji, |
| | 3 | 70 | | SessionType.ShortBreak => Constants.SessionTypes.ShortBreakEmoji, |
| | 3 | 71 | | SessionType.LongBreak => Constants.SessionTypes.LongBreakEmoji, |
| | 1 | 72 | | _ => Constants.SessionTypes.PomodoroEmoji |
| | 23 | 73 | | }; |
| | 23 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the title for the completed session type |
| | | 78 | | /// </summary> |
| | | 79 | | protected string GetTitle() |
| | 23 | 80 | | { |
| | 23 | 81 | | return CompletedSessionType switch |
| | 23 | 82 | | { |
| | 16 | 83 | | SessionType.Pomodoro => Constants.Messages.PomodoroCompleteTitle, |
| | 3 | 84 | | SessionType.ShortBreak => Constants.Messages.BreakCompleteTitle, |
| | 3 | 85 | | SessionType.LongBreak => Constants.Messages.LongBreakCompleteTitle, |
| | 1 | 86 | | _ => Constants.Messages.SessionCompleteTitle |
| | 23 | 87 | | }; |
| | 23 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets the message for the completed session type |
| | | 92 | | /// </summary> |
| | | 93 | | protected string GetMessage() |
| | 23 | 94 | | { |
| | 23 | 95 | | return CompletedSessionType switch |
| | 23 | 96 | | { |
| | 16 | 97 | | SessionType.Pomodoro => Constants.Messages.PomodoroCompleteMessage, |
| | 3 | 98 | | SessionType.ShortBreak => Constants.Messages.BreakCompleteMessage, |
| | 3 | 99 | | SessionType.LongBreak => Constants.Messages.BreakCompleteMessage, |
| | 1 | 100 | | _ => Constants.Messages.SessionCompleteMessage |
| | 23 | 101 | | }; |
| | 23 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Calculates the progress bar percentage |
| | | 106 | | /// </summary> |
| | | 107 | | protected double GetProgressPercentage() |
| | 23 | 108 | | { |
| | 23 | 109 | | if (_initialCountdownSeconds <= 0) |
| | 2 | 110 | | return 0; |
| | | 111 | | |
| | 21 | 112 | | return ((double)CountdownSeconds / _initialCountdownSeconds) * Constants.UI.PercentageMultiplier; |
| | 23 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Handles option selection |
| | | 117 | | /// </summary> |
| | | 118 | | protected async Task HandleOptionSelect(SessionType sessionType) |
| | 1 | 119 | | { |
| | 1 | 120 | | await OnOptionSelected.InvokeAsync(sessionType); |
| | 1 | 121 | | } |
| | | 122 | | |
| | 6 | 123 | | protected RenderFragment RenderOptions => builder => |
| | 6 | 124 | | { |
| | 6 | 125 | | int seq = 0; |
| | 38 | 126 | | foreach (var option in Options) |
| | 10 | 127 | | { |
| | 10 | 128 | | var cssClass = $"btn btn-option {(option.IsDefault ? "default" : "")}"; |
| | 10 | 129 | | builder.OpenElement(seq++, "button"); |
| | 10 | 130 | | builder.AddAttribute(seq++, "class", cssClass); |
| | 11 | 131 | | builder.AddAttribute(seq++, "onclick", EventCallback.Factory.Create<MouseEventArgs>(this, _ => HandleOptionS |
| | 6 | 132 | | |
| | 10 | 133 | | builder.OpenElement(seq++, "span"); |
| | 10 | 134 | | builder.AddAttribute(seq++, "class", "option-label"); |
| | 10 | 135 | | builder.AddContent(seq++, option.Label); |
| | 10 | 136 | | builder.CloseElement(); |
| | 6 | 137 | | |
| | 10 | 138 | | builder.OpenElement(seq++, "span"); |
| | 10 | 139 | | builder.AddAttribute(seq++, "class", "option-duration"); |
| | 10 | 140 | | builder.AddContent(seq++, option.Duration); |
| | 10 | 141 | | builder.CloseElement(); |
| | 6 | 142 | | |
| | 10 | 143 | | builder.CloseElement(); |
| | 10 | 144 | | } |
| | 12 | 145 | | }; |
| | | 146 | | |
| | | 147 | | #endregion |
| | | 148 | | } |