| | | 1 | | namespace Pomodoro.Web.Models; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Central application state container |
| | | 5 | | /// Thread-safe implementation for access from multiple services and timer callbacks |
| | | 6 | | /// </summary> |
| | | 7 | | public class AppState |
| | | 8 | | { |
| | 988 | 9 | | private readonly object _tasksLock = new(); |
| | 988 | 10 | | private List<TaskItem> _tasks = new(); |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Event raised when state properties change |
| | | 14 | | /// </summary> |
| | | 15 | | public event Action? OnStateChanged; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Thread-safe access to tasks list |
| | | 19 | | /// Returns a copy to prevent external modifications |
| | | 20 | | /// </summary> |
| | | 21 | | public IReadOnlyList<TaskItem> Tasks |
| | | 22 | | { |
| | | 23 | | get |
| | 101 | 24 | | { |
| | 101 | 25 | | lock (_tasksLock) |
| | 101 | 26 | | { |
| | 101 | 27 | | return _tasks.ToList(); |
| | | 28 | | } |
| | 101 | 29 | | } |
| | | 30 | | set |
| | 77 | 31 | | { |
| | 77 | 32 | | lock (_tasksLock) |
| | 77 | 33 | | { |
| | 77 | 34 | | _tasks = (value as List<TaskItem>) ?? value?.ToList() ?? new List<TaskItem>(); |
| | 77 | 35 | | } |
| | 77 | 36 | | NotifyStateChanged(); |
| | 77 | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Inserts a task at the specified position (thread-safe) |
| | | 42 | | /// </summary> |
| | | 43 | | public void InsertTask(TaskItem task, int position = 0) |
| | 17 | 44 | | { |
| | 17 | 45 | | lock (_tasksLock) |
| | 17 | 46 | | { |
| | 17 | 47 | | _tasks.Insert(position, task); |
| | 17 | 48 | | } |
| | 17 | 49 | | NotifyStateChanged(); |
| | 17 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Finds a task by ID and performs an update action (thread-safe) |
| | | 54 | | /// Returns true if the task was found and updated |
| | | 55 | | /// </summary> |
| | | 56 | | public bool UpdateTask(Guid taskId, Action<TaskItem> updateAction) |
| | 25 | 57 | | { |
| | 25 | 58 | | lock (_tasksLock) |
| | 25 | 59 | | { |
| | 50 | 60 | | var task = _tasks.FirstOrDefault(t => t.Id == taskId); |
| | 25 | 61 | | if (task != null) |
| | 19 | 62 | | { |
| | 19 | 63 | | updateAction(task); |
| | 19 | 64 | | return true; |
| | | 65 | | } |
| | 6 | 66 | | return false; |
| | | 67 | | } |
| | 25 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Finds a task by ID (thread-safe, returns a copy) |
| | | 72 | | /// </summary> |
| | | 73 | | public TaskItem? FindTaskById(Guid taskId) |
| | 25 | 74 | | { |
| | 25 | 75 | | lock (_tasksLock) |
| | 25 | 76 | | { |
| | 49 | 77 | | return _tasks.FirstOrDefault(t => t.Id == taskId); |
| | | 78 | | } |
| | 25 | 79 | | } |
| | | 80 | | |
| | | 81 | | private Guid? _currentTaskId; |
| | | 82 | | public Guid? CurrentTaskId |
| | | 83 | | { |
| | 45 | 84 | | get => _currentTaskId; |
| | | 85 | | set |
| | 25 | 86 | | { |
| | 25 | 87 | | if (_currentTaskId != value) |
| | 21 | 88 | | { |
| | 21 | 89 | | _currentTaskId = value; |
| | 21 | 90 | | NotifyStateChanged(); |
| | 21 | 91 | | } |
| | 25 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 988 | 95 | | private TimerSettings _settings = new(); |
| | | 96 | | public TimerSettings Settings |
| | | 97 | | { |
| | 916 | 98 | | get => _settings; |
| | | 99 | | set |
| | 233 | 100 | | { |
| | 233 | 101 | | if (_settings != value) |
| | 193 | 102 | | { |
| | 193 | 103 | | _settings = value ?? new TimerSettings(); |
| | 193 | 104 | | NotifyStateChanged(); |
| | 193 | 105 | | } |
| | 233 | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private TimerSession? _currentSession; |
| | | 110 | | public TimerSession? CurrentSession |
| | | 111 | | { |
| | 744 | 112 | | get => _currentSession; |
| | | 113 | | set |
| | 281 | 114 | | { |
| | 281 | 115 | | if (_currentSession != value) |
| | 274 | 116 | | { |
| | 274 | 117 | | _currentSession = value; |
| | 274 | 118 | | NotifyStateChanged(); |
| | 274 | 119 | | } |
| | 281 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // Today's statistics |
| | | 124 | | private int _todayTotalFocusMinutes; |
| | | 125 | | public int TodayTotalFocusMinutes |
| | | 126 | | { |
| | 81 | 127 | | get => _todayTotalFocusMinutes; |
| | | 128 | | set |
| | 160 | 129 | | { |
| | 160 | 130 | | if (_todayTotalFocusMinutes != value) |
| | 42 | 131 | | { |
| | 42 | 132 | | _todayTotalFocusMinutes = value; |
| | 42 | 133 | | NotifyStateChanged(); |
| | 42 | 134 | | } |
| | 160 | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | private int _todayPomodoroCount; |
| | | 139 | | public int TodayPomodoroCount |
| | | 140 | | { |
| | 89 | 141 | | get => _todayPomodoroCount; |
| | | 142 | | set |
| | 160 | 143 | | { |
| | 160 | 144 | | if (_todayPomodoroCount != value) |
| | 42 | 145 | | { |
| | 42 | 146 | | _todayPomodoroCount = value; |
| | 42 | 147 | | NotifyStateChanged(); |
| | 42 | 148 | | } |
| | 160 | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | 988 | 152 | | private readonly object _todayTaskIdsLock = new(); |
| | 988 | 153 | | private List<Guid> _todayTaskIdsWorkedOn = new(); |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Thread-safe access to today's task IDs |
| | | 157 | | /// </summary> |
| | | 158 | | public List<Guid> TodayTaskIdsWorkedOn |
| | | 159 | | { |
| | | 160 | | get |
| | 84 | 161 | | { |
| | 84 | 162 | | lock (_todayTaskIdsLock) |
| | 84 | 163 | | { |
| | 84 | 164 | | return _todayTaskIdsWorkedOn.ToList(); |
| | | 165 | | } |
| | 84 | 166 | | } |
| | | 167 | | set |
| | 21 | 168 | | { |
| | 21 | 169 | | lock (_todayTaskIdsLock) |
| | 21 | 170 | | { |
| | 21 | 171 | | _todayTaskIdsWorkedOn = value ?? new List<Guid>(); |
| | 21 | 172 | | } |
| | 21 | 173 | | NotifyStateChanged(); |
| | 21 | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | private DateTime? _lastResetDate; |
| | | 178 | | public DateTime? LastResetDate |
| | | 179 | | { |
| | 92 | 180 | | get => _lastResetDate; |
| | | 181 | | set |
| | 140 | 182 | | { |
| | 140 | 183 | | if (_lastResetDate != value) |
| | 138 | 184 | | { |
| | 138 | 185 | | _lastResetDate = value; |
| | 138 | 186 | | NotifyStateChanged(); |
| | 138 | 187 | | } |
| | 140 | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Gets the currently selected task (thread-safe) |
| | | 193 | | /// </summary> |
| | | 194 | | public TaskItem? CurrentTask |
| | | 195 | | { |
| | | 196 | | get |
| | 6 | 197 | | { |
| | 6 | 198 | | lock (_tasksLock) |
| | 6 | 199 | | { |
| | 6 | 200 | | return CurrentTaskId.HasValue |
| | 4 | 201 | | ? _tasks.FirstOrDefault(t => t.Id == CurrentTaskId.Value) |
| | 6 | 202 | | : null; |
| | | 203 | | } |
| | 6 | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Gets the count of unique tasks worked on today (thread-safe) |
| | | 209 | | /// </summary> |
| | | 210 | | public int TodayTasksWorkedOn |
| | | 211 | | { |
| | | 212 | | get |
| | 4 | 213 | | { |
| | 4 | 214 | | lock (_todayTaskIdsLock) |
| | 4 | 215 | | { |
| | 4 | 216 | | return _todayTaskIdsWorkedOn.Distinct().Count(); |
| | | 217 | | } |
| | 4 | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Adds a task ID to today's worked-on list (thread-safe) |
| | | 223 | | /// </summary> |
| | | 224 | | public void AddTodayTaskId(Guid taskId) |
| | 36 | 225 | | { |
| | 36 | 226 | | lock (_todayTaskIdsLock) |
| | 36 | 227 | | { |
| | 36 | 228 | | if (!_todayTaskIdsWorkedOn.Contains(taskId)) |
| | 33 | 229 | | { |
| | 33 | 230 | | _todayTaskIdsWorkedOn.Add(taskId); |
| | 33 | 231 | | } |
| | 36 | 232 | | } |
| | 36 | 233 | | NotifyStateChanged(); |
| | 36 | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Gets the current "day" key for daily stats. |
| | | 238 | | /// Uses local midnight for daily reset (e.g., midnight in the user's timezone). |
| | | 239 | | /// Note: For users in Bangladesh (UTC+6), this means stats reset at 6 PM UTC. |
| | | 240 | | /// </summary> |
| | | 241 | | public static DateTime GetCurrentDayKey() |
| | 540 | 242 | | { |
| | | 243 | | // Use local midnight for daily reset |
| | 540 | 244 | | return DateTime.Now.Date; |
| | 540 | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Checks if today's stats need to be reset. |
| | | 249 | | /// Returns true when the local date has changed (crossed local midnight). |
| | | 250 | | /// </summary> |
| | | 251 | | public bool NeedsDailyReset() |
| | 46 | 252 | | { |
| | 46 | 253 | | if (LastResetDate == null) |
| | 4 | 254 | | return true; |
| | | 255 | | |
| | | 256 | | // Reset when the local date changes (at local midnight) |
| | 42 | 257 | | return LastResetDate.Value < GetCurrentDayKey(); |
| | 46 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Resets daily statistics (thread-safe) |
| | | 262 | | /// </summary> |
| | | 263 | | public void ResetDailyStats() |
| | 118 | 264 | | { |
| | 118 | 265 | | TodayTotalFocusMinutes = 0; |
| | 118 | 266 | | TodayPomodoroCount = 0; |
| | 118 | 267 | | lock (_todayTaskIdsLock) |
| | 118 | 268 | | { |
| | 118 | 269 | | _todayTaskIdsWorkedOn = new List<Guid>(); |
| | 118 | 270 | | } |
| | 118 | 271 | | LastResetDate = GetCurrentDayKey(); |
| | 118 | 272 | | NotifyStateChanged(); |
| | 118 | 273 | | } |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Raises the OnStateChanged event |
| | | 277 | | /// </summary> |
| | | 278 | | public void NotifyStateChanged() |
| | 981 | 279 | | { |
| | 981 | 280 | | OnStateChanged?.Invoke(); |
| | 981 | 281 | | } |
| | | 282 | | } |