| | | 1 | | using Microsoft.AspNetCore.Components; |
| | | 2 | | using Microsoft.AspNetCore.Components.Forms; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.JSInterop; |
| | | 5 | | using Pomodoro.Web.Models; |
| | | 6 | | using Pomodoro.Web.Services; |
| | | 7 | | |
| | | 8 | | namespace Pomodoro.Web.Pages; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Code-behind for Settings page |
| | | 12 | | /// </summary> |
| | | 13 | | public class SettingsPageBase : ComponentBase |
| | | 14 | | { |
| | | 15 | | #region Services (Dependency Injection) |
| | | 16 | | |
| | | 17 | | [Inject] |
| | 430 | 18 | | protected ITimerService TimerService { get; set; } = default!; |
| | | 19 | | |
| | | 20 | | [Inject] |
| | 294 | 21 | | protected IExportService ExportService { get; set; } = default!; |
| | | 22 | | |
| | | 23 | | [Inject] |
| | 281 | 24 | | protected ITaskService TaskService { get; set; } = default!; |
| | | 25 | | |
| | | 26 | | [Inject] |
| | 281 | 27 | | protected IActivityService ActivityService { get; set; } = default!; |
| | | 28 | | |
| | | 29 | | [Inject] |
| | 280 | 30 | | protected IJSInteropService JSInteropService { get; set; } = default!; |
| | | 31 | | |
| | | 32 | | [Inject] |
| | 294 | 33 | | protected ILogger<SettingsPageBase> Logger { get; set; } = default!; |
| | | 34 | | |
| | | 35 | | [Inject] |
| | 458 | 36 | | protected SettingsPresenterService SettingsPresenterService { get; set; } = null!; |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region State |
| | | 41 | | |
| | 1336 | 42 | | protected TimerSettings Settings { get; set; } = new TimerSettings(); |
| | | 43 | | |
| | 468 | 44 | | protected TimerSettings OriginalSettings { get; set; } = new TimerSettings(); |
| | | 45 | | |
| | 215 | 46 | | protected bool ShowToast { get; set; } |
| | | 47 | | |
| | 57 | 48 | | protected string? ToastMessage { get; set; } |
| | | 49 | | |
| | | 50 | | // Export/Import state |
| | 188 | 51 | | protected bool IsExporting { get; set; } |
| | 185 | 52 | | protected bool IsImporting { get; set; } |
| | 189 | 53 | | protected bool IsClearing { get; set; } |
| | 200 | 54 | | protected bool ShowClearConfirmation { get; set; } |
| | 193 | 55 | | protected string? ImportResult { get; set; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Indicates whether current settings differ from the original saved settings |
| | | 59 | | /// </summary> |
| | 178 | 60 | | protected bool HasChanges => !Settings.Equals(OriginalSettings); |
| | | 61 | | |
| | 4 | 62 | | protected void MarkDirty() => StateHasChanged(); |
| | | 63 | | |
| | | 64 | | protected async Task ShowTemporaryToastAsync(string message) |
| | 15 | 65 | | { |
| | 15 | 66 | | ToastMessage = message; |
| | 15 | 67 | | ShowToast = true; |
| | 15 | 68 | | StateHasChanged(); |
| | | 69 | | |
| | 15 | 70 | | SafeTaskRunner.RunAndForget( |
| | 15 | 71 | | async () => |
| | 15 | 72 | | { |
| | 15 | 73 | | await Task.Delay(Constants.UI.ToastDurationMs); |
| | 15 | 74 | | ShowToast = false; |
| | 15 | 75 | | ToastMessage = null; |
| | 15 | 76 | | await InvokeAsync(StateHasChanged); |
| | 15 | 77 | | }, |
| | 15 | 78 | | Logger, |
| | 15 | 79 | | Constants.SafeTaskOperations.ToastHide |
| | 15 | 80 | | ); |
| | 15 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Indicates whether current settings differ from default values |
| | | 85 | | /// </summary> |
| | 178 | 86 | | protected bool IsAtDefaults => SettingsPresenterService.IsAtDefaults(Settings); |
| | | 87 | | |
| | | 88 | | #endregion |
| | | 89 | | |
| | | 90 | | #region Lifecycle Methods |
| | | 91 | | |
| | | 92 | | protected override void OnInitialized() |
| | 138 | 93 | | { |
| | | 94 | | // Clone settings from TimerService for local editing |
| | 138 | 95 | | Settings = TimerService.Settings.Clone(); |
| | | 96 | | // Store original settings for comparison |
| | 138 | 97 | | OriginalSettings = Settings.Clone(); |
| | 138 | 98 | | } |
| | | 99 | | |
| | | 100 | | #endregion |
| | | 101 | | |
| | | 102 | | #region Actions |
| | | 103 | | |
| | | 104 | | public async Task HandleSave() |
| | 8 | 105 | | { |
| | 8 | 106 | | await TimerService.UpdateSettingsAsync(Settings); |
| | | 107 | | |
| | | 108 | | // Update original settings after save using Clone() for consistency |
| | 6 | 109 | | OriginalSettings = Settings.Clone(); |
| | | 110 | | |
| | 6 | 111 | | await ShowTemporaryToastAsync("Settings saved successfully!"); |
| | 6 | 112 | | } |
| | | 113 | | |
| | | 114 | | public void ResetToDefaults() |
| | 2 | 115 | | { |
| | | 116 | | // Use model's default values - single source of truth |
| | 2 | 117 | | Settings = new TimerSettings(); |
| | 2 | 118 | | } |
| | | 119 | | |
| | | 120 | | #endregion |
| | | 121 | | |
| | | 122 | | #region Export/Import Actions |
| | | 123 | | |
| | | 124 | | public async Task ExportJson() |
| | 5 | 125 | | { |
| | 5 | 126 | | IsExporting = true; |
| | 5 | 127 | | StateHasChanged(); |
| | | 128 | | |
| | | 129 | | try |
| | 5 | 130 | | { |
| | 5 | 131 | | var json = await ExportService.ExportToJsonAsync(); |
| | 3 | 132 | | var filename = $"pomodoro-backup-{DateTime.Today:yyyy-MM-dd}.json"; |
| | 3 | 133 | | await SettingsPresenterService.DownloadFileAsync(JSInteropService, filename, json, "application/json"); |
| | | 134 | | |
| | 3 | 135 | | await ShowTemporaryToastAsync("JSON backup exported successfully!"); |
| | 3 | 136 | | } |
| | 2 | 137 | | catch (Exception ex) |
| | 2 | 138 | | { |
| | 2 | 139 | | Logger.LogError(ex, "Failed to export JSON"); |
| | 2 | 140 | | ToastMessage = "Failed to export JSON backup. Please try again."; |
| | 2 | 141 | | ShowToast = true; |
| | 2 | 142 | | StateHasChanged(); |
| | 2 | 143 | | } |
| | | 144 | | finally |
| | 5 | 145 | | { |
| | 5 | 146 | | IsExporting = false; |
| | 5 | 147 | | StateHasChanged(); |
| | 5 | 148 | | } |
| | 5 | 149 | | } |
| | | 150 | | |
| | | 151 | | public async Task HandleImport(InputFileChangeEventArgs e) |
| | 8 | 152 | | { |
| | 8 | 153 | | var file = e.File; |
| | 8 | 154 | | if (file is null || file.Size == 0) |
| | 2 | 155 | | { |
| | 2 | 156 | | ImportResult = "No file selected"; |
| | 2 | 157 | | return; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | // Validate file size to prevent memory issues |
| | 6 | 161 | | if (file.Size > Constants.Validation.MaxImportFileSizeBytes) |
| | 1 | 162 | | { |
| | 1 | 163 | | ImportResult = $"File too large. Maximum size is {Constants.Validation.MaxImportFileSizeBytes / (1024 * 1024 |
| | 1 | 164 | | return; |
| | | 165 | | } |
| | | 166 | | |
| | 5 | 167 | | IsImporting = true; |
| | 5 | 168 | | ImportResult = null; |
| | 5 | 169 | | StateHasChanged(); |
| | | 170 | | |
| | | 171 | | try |
| | 5 | 172 | | { |
| | | 173 | | // Use explicit max size to ensure it matches our validation |
| | 5 | 174 | | using var stream = file.OpenReadStream(Constants.Validation.MaxImportFileSizeBytes); |
| | 5 | 175 | | using var reader = new StreamReader(stream); |
| | 5 | 176 | | var jsonData = await reader.ReadToEndAsync(); |
| | | 177 | | |
| | 5 | 178 | | var result = await ExportService.ImportFromJsonAsync(jsonData); |
| | | 179 | | |
| | 4 | 180 | | if (result.Success) |
| | 2 | 181 | | { |
| | | 182 | | // Build success message with detailed statistics |
| | 2 | 183 | | var message = SettingsPresenterService.BuildImportSuccessMessage(result.TotalImported, result.TotalSkipp |
| | | 184 | | |
| | | 185 | | // Reload all services to reflect imported data without page reload |
| | 2 | 186 | | await TaskService.ReloadAsync(); |
| | 2 | 187 | | await ActivityService.ReloadAsync(); |
| | | 188 | | |
| | | 189 | | // Update settings from the newly imported data |
| | 2 | 190 | | Settings = TimerService.Settings.Clone(); |
| | 2 | 191 | | OriginalSettings = Settings.Clone(); |
| | | 192 | | |
| | 2 | 193 | | IsImporting = false; |
| | 2 | 194 | | ImportResult = null; |
| | 2 | 195 | | await ShowTemporaryToastAsync(message); |
| | 2 | 196 | | } |
| | | 197 | | else |
| | 2 | 198 | | { |
| | | 199 | | // Show error message without reloading |
| | 2 | 200 | | ImportResult = result.ErrorMessage ?? "Import failed. Please check the file format."; |
| | 2 | 201 | | IsImporting = false; |
| | 2 | 202 | | StateHasChanged(); |
| | 2 | 203 | | } |
| | 4 | 204 | | } |
| | 1 | 205 | | catch (Exception ex) |
| | 1 | 206 | | { |
| | 1 | 207 | | Logger.LogError(ex, "Failed to import JSON"); |
| | 1 | 208 | | ImportResult = "Import failed. Please check the file format."; |
| | 1 | 209 | | IsImporting = false; |
| | 1 | 210 | | StateHasChanged(); |
| | 1 | 211 | | } |
| | 8 | 212 | | } |
| | | 213 | | |
| | | 214 | | public void ConfirmClearData() |
| | 16 | 215 | | { |
| | 16 | 216 | | ShowClearConfirmation = true; |
| | 16 | 217 | | } |
| | | 218 | | |
| | | 219 | | public async Task ClearData() |
| | 6 | 220 | | { |
| | 6 | 221 | | ShowClearConfirmation = false; |
| | 6 | 222 | | IsClearing = true; |
| | 6 | 223 | | StateHasChanged(); |
| | | 224 | | |
| | | 225 | | try |
| | 6 | 226 | | { |
| | 6 | 227 | | await ExportService.ClearAllDataAsync(); |
| | | 228 | | |
| | | 229 | | // Reset settings to defaults |
| | 4 | 230 | | Settings = new TimerSettings(); |
| | 4 | 231 | | await TimerService.UpdateSettingsAsync(Settings); |
| | | 232 | | |
| | 4 | 233 | | OriginalSettings = new TimerSettings(); |
| | | 234 | | |
| | | 235 | | // Reload all services to reflect cleared data without page reload |
| | 4 | 236 | | await TaskService.ReloadAsync(); |
| | 4 | 237 | | await ActivityService.ReloadAsync(); |
| | | 238 | | |
| | 4 | 239 | | IsClearing = false; |
| | 4 | 240 | | await ShowTemporaryToastAsync("All data cleared successfully!"); |
| | 4 | 241 | | } |
| | 2 | 242 | | catch (Exception ex) |
| | 2 | 243 | | { |
| | 2 | 244 | | Logger.LogError(ex, "Failed to clear data"); |
| | 2 | 245 | | IsClearing = false; |
| | 2 | 246 | | StateHasChanged(); |
| | 2 | 247 | | } |
| | 6 | 248 | | } |
| | | 249 | | |
| | | 250 | | #endregion |
| | | 251 | | } |