55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using System.Windows.Input;
|
|
|
|
namespace CloudPrint.Client.Infrastructure;
|
|
|
|
public sealed class AsyncRelayCommand : ICommand
|
|
{
|
|
private readonly Func<Task> _execute;
|
|
private readonly Func<bool>? _canExecute;
|
|
private bool _isExecuting;
|
|
|
|
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
|
|
{
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public event EventHandler? CanExecuteChanged;
|
|
|
|
public bool IsExecuting
|
|
{
|
|
get => _isExecuting;
|
|
private set
|
|
{
|
|
if (_isExecuting == value)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_isExecuting = value;
|
|
RaiseCanExecuteChanged();
|
|
}
|
|
}
|
|
|
|
public bool CanExecute(object? parameter) => !IsExecuting && (_canExecute?.Invoke() ?? true);
|
|
|
|
public async void Execute(object? parameter)
|
|
{
|
|
if (!CanExecute(parameter))
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
IsExecuting = true;
|
|
await _execute().ConfigureAwait(true);
|
|
}
|
|
finally
|
|
{
|
|
IsExecuting = false;
|
|
}
|
|
}
|
|
|
|
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
} |