using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows.Input; using System.Windows.Threading; namespace ClassicAssist.Launcher { public class BaseViewModel : INotifyPropertyChanged { protected Dispatcher _dispatcher; public BaseViewModel() { _dispatcher = Dispatcher.CurrentDispatcher; } public event PropertyChangedEventHandler PropertyChanged; // ReSharper disable once RedundantAssignment public virtual void SetProperty( ref T obj, T value, [CallerMemberName] string propertyName = "" ) { obj = value; NotifyPropertyChanged( propertyName ); CommandManager.InvalidateRequerySuggested(); } protected void NotifyPropertyChanged( [CallerMemberName] string propertyName = "" ) { PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) ); } } public class RelayCommand : ICommand { private readonly Func _canExecute; private readonly Action _execute; public RelayCommand( Action execute, Func canExecute = null ) { _execute = execute; _canExecute = canExecute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; CanExecuteChangedInternal += value; } remove { CommandManager.RequerySuggested -= value; CanExecuteChangedInternal -= value; } } public bool CanExecute( object parameter ) { return _canExecute == null || _canExecute( parameter ); } public void Execute( object parameter ) { _execute( parameter ); } private event EventHandler CanExecuteChangedInternal; // ReSharper disable once UnusedMember.Global public void RaiseCanExecuteChanged() { CanExecuteChangedInternal?.Invoke( this, EventArgs.Empty ); } } public class RelayCommandAsync : ICommand { private readonly Func canExecuteMethod; private readonly Func executedMethod; public RelayCommandAsync( Func execute, Func canExecute ) { executedMethod = execute ?? throw new ArgumentNullException( nameof( execute ) ); canExecuteMethod = canExecute; } public event EventHandler CanExecuteChanged { add => CommandManager.RequerySuggested += value; remove => CommandManager.RequerySuggested -= value; } public bool CanExecute( object parameter ) { return canExecuteMethod == null || canExecuteMethod( parameter ); } public async void Execute( object parameter ) { await executedMethod( parameter ); } } }