using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows.Input; namespace CRAWLER.Infrastructure; public abstract class ObservableObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected bool SetProperty(ref T field, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(field, value)) { return false; } field = value; OnPropertyChanged(propertyName); return true; } protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } internal sealed class RelayCommand : ICommand { private readonly Action _execute; private readonly Predicate _canExecute; public RelayCommand(Action execute, Predicate canExecute = null) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } public void RaiseCanExecuteChanged() { var handler = CanExecuteChanged; if (handler != null) { handler(this, EventArgs.Empty); } } } internal sealed class AsyncRelayCommand : ICommand { private readonly Func _executeAsync; private readonly Predicate _canExecute; private bool _isExecuting; public AsyncRelayCommand(Func executeAsync, Func canExecute = null) : this(_ => executeAsync(), canExecute == null ? null : new Predicate(_ => canExecute())) { } public AsyncRelayCommand(Func executeAsync, Predicate canExecute = null) { _executeAsync = executeAsync ?? throw new ArgumentNullException(nameof(executeAsync)); _canExecute = canExecute; } public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return !_isExecuting && (_canExecute == null || _canExecute(parameter)); } public async void Execute(object parameter) { await ExecuteAsync(parameter); } public async Task ExecuteAsync(object parameter = null) { if (!CanExecute(parameter)) { return; } try { _isExecuting = true; RaiseCanExecuteChanged(); await _executeAsync(parameter); } finally { _isExecuting = false; RaiseCanExecuteChanged(); } } public void RaiseCanExecuteChanged() { var handler = CanExecuteChanged; if (handler != null) { handler(this, EventArgs.Empty); } } }