using System.Windows.Input; namespace TeamsISO.App.ViewModels; /// /// Synchronous command that delegates execution to an . /// public sealed class RelayCommand : ICommand { private readonly Action _execute; private readonly Func? _canExecute; public RelayCommand(Action execute, Func? canExecute = null) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object? parameter) => _canExecute?.Invoke() ?? true; public void Execute(object? parameter) => _execute(); public event EventHandler? CanExecuteChanged; public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); } /// /// Synchronous command that accepts a typed parameter. Used by hotkeys /// that need to pass an index (e.g. NumPad1..NumPad9 → 1..9). The /// parameter is converted from object via Convert.ChangeType so XAML /// CommandParameter="1" works for int T. /// public sealed class RelayCommand : ICommand { private readonly Action _execute; private readonly Func? _canExecute; public RelayCommand(Action execute, Func? canExecute = null) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object? parameter) => _canExecute?.Invoke(Convert(parameter)) ?? true; public void Execute(object? parameter) => _execute(Convert(parameter)); public event EventHandler? CanExecuteChanged; public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); private static TValue Convert(object? value) { if (value is null) return default!; if (value is TValue typed) return typed; try { return (TValue)System.Convert.ChangeType(value, typeof(TValue)); } catch { return default!; } } } /// /// Async command that suppresses re-entrancy while running. /// public sealed class AsyncRelayCommand : ICommand { private readonly Func _execute; private readonly Func? _canExecute; private bool _isRunning; public AsyncRelayCommand(Func execute, Func? canExecute = null) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object? parameter) => !_isRunning && (_canExecute?.Invoke() ?? true); public async void Execute(object? parameter) { if (_isRunning) return; _isRunning = true; RaiseCanExecuteChanged(); try { await _execute(); } finally { _isRunning = false; RaiseCanExecuteChanged(); } } public event EventHandler? CanExecuteChanged; public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); }