1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-26 05:03:39 +00:00
Files
mobile/src/Core/Utilities/AsyncCommand.cs
2023-09-29 11:02:19 -03:00

71 lines
2.7 KiB
C#

using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
namespace Bit.App.Utilities
{
// TODO: [MAUI-Migration] DELETE WHEN MIGRATION IS DONE
/// <summary>
/// Wrapper of <see cref="AsyncRelayCommand"/> just to ease with the MAUI migration process.
/// After the process is done, remove this and use AsyncRelayCommand directly
/// </summary>
public class AsyncCommand : ICommand
{
readonly AsyncRelayCommand _relayCommand;
public AsyncCommand(Func<Task> execute, Func<bool> canExecute = null, Action<Exception> onException = null, bool allowsMultipleExecutions = true)
{
async Task doAsync()
{
try
{
await execute?.Invoke();
}
catch (Exception ex)
{
onException?.Invoke(ex);
}
}
_relayCommand = new AsyncRelayCommand(doAsync, canExecute, allowsMultipleExecutions ? AsyncRelayCommandOptions.AllowConcurrentExecutions : AsyncRelayCommandOptions.None);
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => _relayCommand.CanExecute(parameter);
public void Execute(object parameter) => _relayCommand.Execute(parameter);
public void RaiseCanExecuteChanged() => _relayCommand.NotifyCanExecuteChanged();
}
/// Wrapper of <see cref="AsyncRelayCommand"/> just to ease with the MAUI migration process.
/// After the process is done, remove this and use AsyncRelayCommand directly
/// </summary>
public class AsyncCommand<T> : ICommand
{
readonly AsyncRelayCommand<T> _relayCommand;
public AsyncCommand(Func<T, Task> execute, Predicate<T?> canExecute = null, Action<Exception> onException = null, bool allowsMultipleExecutions = true)
{
async Task doAsync(T foo)
{
try
{
await execute?.Invoke(foo);
}
catch (Exception ex)
{
onException?.Invoke(ex);
}
}
_relayCommand = new AsyncRelayCommand<T>(doAsync, canExecute, allowsMultipleExecutions ? AsyncRelayCommandOptions.AllowConcurrentExecutions : AsyncRelayCommandOptions.None);
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => _relayCommand.CanExecute(parameter);
public void Execute(object parameter) => _relayCommand.Execute(parameter);
public void RaiseCanExecuteChanged() => _relayCommand.NotifyCanExecuteChanged();
}
}