|
1 | | -namespace SharedCode.Windows.WPF.ViewModels; |
| 1 | +using SharedCode.Linq; |
2 | 2 |
|
3 | | -using SharedCode.Linq; |
4 | | -using SharedCode.Windows.WPF; |
5 | | - |
6 | | -using System; |
7 | 3 | using System.Linq.Expressions; |
8 | | -using System.Threading.Tasks; |
| 4 | + |
| 5 | +namespace SharedCode.Windows.WPF.ViewModels; |
9 | 6 |
|
10 | 7 | /// <summary> |
11 | 8 | /// An abstract view model class. |
12 | 9 | /// </summary> |
13 | 10 | public abstract class ViewModelBase : BindableBase |
14 | 11 | { |
15 | | - /// <summary> |
16 | | - /// Runs a command if the updating flag is not set. |
17 | | - /// If the flag is true, indicating the function is already running, then the action is not run. |
18 | | - /// If the flag is false, indicating the function is not running, then the action is run. |
19 | | - /// Once the action is finished, if it was run, then the flag is reset to false. |
20 | | - /// </summary> |
21 | | - /// <param name="updatingFlag">The boolean value indicating whether the function is running.</param> |
22 | | - /// <param name="action">The action to run, if it is not already running.</param> |
23 | | - /// <returns>A <see cref="Task"/>.</returns> |
24 | | - /// <exception cref="ArgumentNullException">action</exception> |
25 | | - protected static async Task RunCommandAsync(Expression<Func<bool>> updatingFlag, Func<Task> action) |
26 | | - { |
27 | | - _ = action ?? throw new ArgumentNullException(nameof(action)); |
| 12 | + /// <summary> |
| 13 | + /// Runs a command if the updating flag is not set. If the flag is true, indicating the function |
| 14 | + /// is already running, then the action is not run. If the flag is false, indicating the |
| 15 | + /// function is not running, then the action is run. Once the action is finished, if it was run, |
| 16 | + /// then the flag is reset to false. |
| 17 | + /// </summary> |
| 18 | + /// <param name="updatingFlag">The boolean value indicating whether the function is running.</param> |
| 19 | + /// <param name="action">The action to run, if it is not already running.</param> |
| 20 | + /// <returns>A <see cref="Task"/>.</returns> |
| 21 | + /// <exception cref="ArgumentNullException">action</exception> |
| 22 | + protected static async Task RunCommandAsync(Expression<Func<bool>> updatingFlag, Func<Task> action) |
| 23 | + { |
| 24 | + _ = action ?? throw new ArgumentNullException(nameof(action)); |
28 | 25 |
|
29 | | - if (updatingFlag is null || updatingFlag.GetPropertyValue()) |
30 | | - { |
31 | | - return; |
32 | | - } |
| 26 | + // If flag is null or already true, do nothing |
| 27 | + if (updatingFlag?.GetPropertyValue() != false) |
| 28 | + { |
| 29 | + return; |
| 30 | + } |
33 | 31 |
|
34 | | - updatingFlag.SetPropertyValue(true); |
| 32 | + updatingFlag.SetPropertyValue(true); |
35 | 33 |
|
36 | | - try |
37 | | - { |
38 | | - await action().ConfigureAwait(true); |
39 | | - } |
40 | | - finally |
41 | | - { |
42 | | - updatingFlag.SetPropertyValue(false); |
43 | | - } |
44 | | - } |
| 34 | + try |
| 35 | + { |
| 36 | + await action().ConfigureAwait(true); |
| 37 | + } |
| 38 | + finally |
| 39 | + { |
| 40 | + updatingFlag.SetPropertyValue(false); |
| 41 | + } |
| 42 | + } |
45 | 43 | } |
0 commit comments