127 lines
3.2 KiB
C#
127 lines
3.2 KiB
C#
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<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
|
|
{
|
|
if (EqualityComparer<T>.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<object> _execute;
|
|
private readonly Predicate<object> _canExecute;
|
|
|
|
public RelayCommand(Action<object> execute, Predicate<object> 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<object, Task> _executeAsync;
|
|
private readonly Predicate<object> _canExecute;
|
|
private bool _isExecuting;
|
|
|
|
public AsyncRelayCommand(Func<Task> executeAsync, Func<bool> canExecute = null)
|
|
: this(_ => executeAsync(), canExecute == null ? null : new Predicate<object>(_ => canExecute()))
|
|
{
|
|
}
|
|
|
|
public AsyncRelayCommand(Func<object, Task> executeAsync, Predicate<object> 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);
|
|
}
|
|
}
|
|
}
|