1e21e33ade
Move SecureStoreManager project and tests to Deprecated folder and remove from solution. SecureStore functionality is now integrated into ConfigManager.
84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using System.Windows.Input;
|
|
|
|
namespace JdeScoping.SecureStoreManager.ViewModels;
|
|
|
|
/// <summary>
|
|
/// An async command implementation that properly handles async operations.
|
|
/// </summary>
|
|
public class AsyncRelayCommand : ICommand
|
|
{
|
|
private readonly Func<Task> _execute;
|
|
private readonly Func<bool>? _canExecute;
|
|
private bool _isExecuting;
|
|
private EventHandler? _canExecuteChanged;
|
|
|
|
/// <summary>
|
|
/// Raised when the result of CanExecute() may have changed.
|
|
/// </summary>
|
|
public event EventHandler? CanExecuteChanged
|
|
{
|
|
add => _canExecuteChanged += value;
|
|
remove => _canExecuteChanged -= value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new AsyncRelayCommand that can always execute.
|
|
/// </summary>
|
|
/// <param name="execute">The async action to execute.</param>
|
|
public AsyncRelayCommand(Func<Task> execute)
|
|
: this(execute, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new AsyncRelayCommand with a CanExecute predicate.
|
|
/// </summary>
|
|
/// <param name="execute">The async action to execute.</param>
|
|
/// <param name="canExecute">The predicate to determine if the command can execute.</param>
|
|
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute)
|
|
{
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the command can be executed in its current state.
|
|
/// </summary>
|
|
/// <param name="parameter">The command parameter (unused).</param>
|
|
public bool CanExecute(object? parameter)
|
|
{
|
|
return !_isExecuting && (_canExecute?.Invoke() ?? true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the async command if it can execute.
|
|
/// </summary>
|
|
/// <param name="parameter">The command parameter (unused).</param>
|
|
public async void Execute(object? parameter)
|
|
{
|
|
if (!CanExecute(parameter))
|
|
return;
|
|
|
|
_isExecuting = true;
|
|
RaiseCanExecuteChanged();
|
|
|
|
try
|
|
{
|
|
await _execute();
|
|
}
|
|
finally
|
|
{
|
|
_isExecuting = false;
|
|
RaiseCanExecuteChanged();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the CanExecuteChanged event.
|
|
/// </summary>
|
|
public void RaiseCanExecuteChanged()
|
|
{
|
|
_canExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|