|
2 | 2 |
|
3 | 3 | using System.Windows.Input; |
4 | 4 | using AutoMapper; |
| 5 | +using Bible.Alarm.Common.Messenger; |
5 | 6 | using Bible.Alarm.Shared.Models.Enums; |
6 | 7 | using Bible.Alarm.Stores; |
7 | 8 | using Bible.Alarm.Stores.Actions.Schedule; |
8 | 9 | using Bible.Alarm.Stores.Models; |
9 | 10 | using CommunityToolkit.Mvvm.ComponentModel; |
10 | 11 | using CommunityToolkit.Mvvm.Input; |
| 12 | +using CommunityToolkit.Mvvm.Messaging; |
11 | 13 | using Fluxor; |
12 | 14 | using Serilog; |
13 | 15 | using IDispatcher = Fluxor.IDispatcher; |
@@ -42,6 +44,8 @@ public ScheduleDetailsContainerViewModel( |
42 | 44 | this.mapper = mapper; |
43 | 45 |
|
44 | 46 | state.StateChanged += OnStateChanged; |
| 47 | + // Subscribe to theme changes to update day button colors |
| 48 | + WeakReferenceMessenger.Default.Register<ThemeChangedMessage>(this, (r, m) => OnThemeChanged()); |
45 | 49 | InitializeCommands(); |
46 | 50 | InitializeFromState(); |
47 | 51 | } |
@@ -114,67 +118,81 @@ private void SignalContainerReady() |
114 | 118 |
|
115 | 119 | private void OnStateChanged(object? sender, EventArgs e) |
116 | 120 | { |
117 | | - var stateValue = state.Value; |
118 | | - var currentSchedule = stateValue.CurrentSchedule; |
119 | | - |
120 | | - // If ContainerReadiness was reset to NotReady but we've already signaled ready, reset our flag |
121 | | - // This handles the case where ViewScheduleAction resets ContainerReadiness after containers signaled ready |
122 | | - if (hasSignaledReady && !stateValue.ContainerReadiness.ScheduleDetails && currentSchedule != null) |
| 121 | + // Prevent re-entrant calls to avoid cycles |
| 122 | + if (isProcessingStateChange) |
123 | 123 | { |
124 | | - hasSignaledReady = false; |
125 | | - isReadyActionQueued = false; // Reset queued flag as well |
126 | | - // Re-initialize and signal ready again |
127 | | - InitializeFromState(); |
128 | 124 | return; |
129 | 125 | } |
130 | 126 |
|
131 | | - // If we don't have a scheduleId yet (initial state), initialize when CurrentSchedule is set |
132 | | - // But only if we haven't already signaled ready (prevents infinite loop for new schedules with Id=0) |
133 | | - if (scheduleId == 0 && currentSchedule != null && !hasSignaledReady) |
| 127 | + isProcessingStateChange = true; |
| 128 | + try |
134 | 129 | { |
135 | | - InitializeFromState(); |
136 | | - return; |
137 | | - } |
| 130 | + var stateValue = state.Value; |
| 131 | + var currentSchedule = stateValue.CurrentSchedule; |
138 | 132 |
|
139 | | - // Reset hasSignaledReady when schedule ID changes to a different positive ID (existing schedule opened) |
140 | | - if (currentSchedule != null && currentSchedule.Id != scheduleId && currentSchedule.Id > 0) |
141 | | - { |
142 | | - hasSignaledReady = false; |
143 | | - isReadyActionQueued = false; // Reset queued flag as well |
144 | | - InitializeFromState(); |
145 | | - return; |
146 | | - } |
| 133 | + // If ContainerReadiness was reset to NotReady but we've already signaled ready, reset our flag |
| 134 | + // This handles the case where ViewScheduleAction resets ContainerReadiness after containers signaled ready |
| 135 | + if (hasSignaledReady && !stateValue.ContainerReadiness.ScheduleDetails && currentSchedule != null) |
| 136 | + { |
| 137 | + hasSignaledReady = false; |
| 138 | + isReadyActionQueued = false; // Reset queued flag as well |
| 139 | + // Re-initialize and signal ready again |
| 140 | + InitializeFromState(); |
| 141 | + return; |
| 142 | + } |
147 | 143 |
|
148 | | - // Only update properties if they changed (don't re-initialize) |
149 | | - if (currentSchedule != null && !hasSignaledReady) |
150 | | - { |
151 | | - // Handle case where InitializeFromState hasn't been called yet |
152 | | - InitializeFromState(); |
153 | | - } |
154 | | - else if (currentSchedule != null && hasSignaledReady) |
155 | | - { |
156 | | - // Update individual properties when they change (after initialization) |
157 | | - if (isEnabled != currentSchedule.IsEnabled) |
| 144 | + // If we don't have a scheduleId yet (initial state), initialize when CurrentSchedule is set |
| 145 | + // But only if we haven't already signaled ready (prevents infinite loop for new schedules with Id=0) |
| 146 | + if (scheduleId == 0 && currentSchedule != null && !hasSignaledReady) |
158 | 147 | { |
159 | | - isEnabled = currentSchedule.IsEnabled; |
160 | | - OnPropertyChanged(nameof(IsEnabled)); |
| 148 | + InitializeFromState(); |
| 149 | + return; |
161 | 150 | } |
162 | | - if (time != new TimeSpan(currentSchedule.Hour, currentSchedule.Minute, currentSchedule.Second)) |
| 151 | + |
| 152 | + // Reset hasSignaledReady when schedule ID changes to a different positive ID (existing schedule opened) |
| 153 | + if (currentSchedule != null && currentSchedule.Id != scheduleId && currentSchedule.Id > 0) |
163 | 154 | { |
164 | | - time = new TimeSpan(currentSchedule.Hour, currentSchedule.Minute, currentSchedule.Second); |
165 | | - OnPropertyChanged(nameof(Time)); |
| 155 | + hasSignaledReady = false; |
| 156 | + isReadyActionQueued = false; // Reset queued flag as well |
| 157 | + InitializeFromState(); |
| 158 | + return; |
166 | 159 | } |
167 | | - if (daysOfWeek != currentSchedule.DaysOfWeek) |
| 160 | + |
| 161 | + // Only update properties if they changed (don't re-initialize) |
| 162 | + if (currentSchedule != null && !hasSignaledReady) |
168 | 163 | { |
169 | | - daysOfWeek = currentSchedule.DaysOfWeek; |
170 | | - OnPropertyChanged(nameof(DaysOfWeek)); |
| 164 | + // Handle case where InitializeFromState hasn't been called yet |
| 165 | + InitializeFromState(); |
171 | 166 | } |
172 | | - if (name != currentSchedule.Name) |
| 167 | + else if (currentSchedule != null && hasSignaledReady) |
173 | 168 | { |
174 | | - name = currentSchedule.Name; |
175 | | - OnPropertyChanged(nameof(Name)); |
| 169 | + // Update individual properties when they change (after initialization) |
| 170 | + if (isEnabled != currentSchedule.IsEnabled) |
| 171 | + { |
| 172 | + isEnabled = currentSchedule.IsEnabled; |
| 173 | + OnPropertyChanged(nameof(IsEnabled)); |
| 174 | + } |
| 175 | + if (time != new TimeSpan(currentSchedule.Hour, currentSchedule.Minute, currentSchedule.Second)) |
| 176 | + { |
| 177 | + time = new TimeSpan(currentSchedule.Hour, currentSchedule.Minute, currentSchedule.Second); |
| 178 | + OnPropertyChanged(nameof(Time)); |
| 179 | + } |
| 180 | + if (daysOfWeek != currentSchedule.DaysOfWeek) |
| 181 | + { |
| 182 | + daysOfWeek = currentSchedule.DaysOfWeek; |
| 183 | + OnPropertyChanged(nameof(DaysOfWeek)); |
| 184 | + } |
| 185 | + if (name != currentSchedule.Name) |
| 186 | + { |
| 187 | + name = currentSchedule.Name; |
| 188 | + OnPropertyChanged(nameof(Name)); |
| 189 | + } |
176 | 190 | } |
177 | 191 | } |
| 192 | + finally |
| 193 | + { |
| 194 | + isProcessingStateChange = false; |
| 195 | + } |
178 | 196 | } |
179 | 197 |
|
180 | 198 | public ICommand ToggleDayCommand { get; private set; } = null!; |
@@ -326,9 +344,20 @@ private void NotifyScheduleDetailsPropertiesChanged() |
326 | 344 | OnPropertyChanged(nameof(IsEnabled)); |
327 | 345 | } |
328 | 346 |
|
| 347 | + private void OnThemeChanged() |
| 348 | + { |
| 349 | + // Notify DaysOfWeek and IsEnabled properties to trigger converters that bind to them |
| 350 | + // This causes day button colors to update when theme changes |
| 351 | + // Note: We're already on the main thread (message is sent from main thread), |
| 352 | + // but we batch the notifications to ensure MultiBinding converters see both values updated together |
| 353 | + OnPropertyChanged(nameof(DaysOfWeek)); |
| 354 | + OnPropertyChanged(nameof(IsEnabled)); |
| 355 | + } |
| 356 | + |
329 | 357 | public void Dispose() |
330 | 358 | { |
331 | 359 | state.StateChanged -= OnStateChanged; |
| 360 | + WeakReferenceMessenger.Default.Unregister<ThemeChangedMessage>(this); |
332 | 361 | } |
333 | 362 | } |
334 | 363 |
|
0 commit comments