Skip to content

Commit 37faa46

Browse files
committed
Refactor ViewModelBase and improve documentation
- Updated `using` directives: removed `SharedCode.Windows.WPF` and added `SharedCode.Linq`. - Reformatted and expanded XML documentation for `RunCommandAsync` for clarity. - Simplified logic in `RunCommandAsync` to improve readability while maintaining functionality. - Enhanced comments and formatting for better understanding and maintainability.
1 parent cf9bd33 commit 37faa46

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
namespace SharedCode.Windows.WPF.ViewModels;
1+
using SharedCode.Linq;
22

3-
using SharedCode.Linq;
4-
using SharedCode.Windows.WPF;
5-
6-
using System;
73
using System.Linq.Expressions;
8-
using System.Threading.Tasks;
4+
5+
namespace SharedCode.Windows.WPF.ViewModels;
96

107
/// <summary>
118
/// An abstract view model class.
129
/// </summary>
1310
public abstract class ViewModelBase : BindableBase
1411
{
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));
2825

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+
}
3331

34-
updatingFlag.SetPropertyValue(true);
32+
updatingFlag.SetPropertyValue(true);
3533

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+
}
4543
}

0 commit comments

Comments
 (0)