Skip to content

Commit

Permalink
Add Support for TaskRelayCommand and IBusyViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Nov 13, 2023
1 parent 5f2a060 commit 67f9547
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/CSharp/EasyMicroservices.UI.Core.Mvvm/BaseViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.ServiceContracts;
using EasyMicroservices.UI.Core.Interfaces;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
Expand All @@ -8,8 +9,13 @@ namespace EasyMicroservices.UI.Core;
/// <summary>
///
/// </summary>
public abstract class BaseViewModel : INotifyPropertyChanged
public abstract class BaseViewModel : IBusyViewModel, INotifyPropertyChanged
{
/// <summary>
///
/// </summary>
public Action<bool> OnBusyChanged { get; set; }

bool _IsBusy;
/// <summary>
///
Expand All @@ -20,6 +26,7 @@ public virtual bool IsBusy
set
{
_IsBusy = value;
OnBusyChanged?.Invoke(value);
OnPropertyChanged(nameof(IsBusy));
}
}
Expand Down Expand Up @@ -128,7 +135,7 @@ public virtual async Task ExecuteApi(Func<Task<object>> getServerResult, Func<Ta
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public virtual Task DisplayError(Exception exception)
public virtual Task OnError(Exception exception)
{
return Task.CompletedTask;
}
Expand Down
63 changes: 57 additions & 6 deletions src/CSharp/EasyMicroservices.UI.Core.Mvvm/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using EasyMicroservices.UI.Core.Interfaces;
using System;
using System.Windows.Input;

namespace EasyMicroservices.UI.Core.Commands;
Expand All @@ -12,8 +13,18 @@ public abstract class BaseCommand : ICommand
///
/// </summary>
public event EventHandler CanExecuteChanged;
private readonly Action<object> _execute = null;
private readonly Func<object, bool> _canExecute = null;
/// <summary>
///
/// </summary>
protected readonly Action<object> _execute = null;
/// <summary>
///
/// </summary>
protected readonly Func<object, bool> _canExecute = null;
/// <summary>
/// /
/// </summary>
protected readonly IBusyViewModel _busyViewModel = null;

/// <summary>
///
Expand All @@ -26,15 +37,35 @@ public BaseCommand(Action<object> execute, Func<object, bool> canExecute)
_canExecute = canExecute;
}

/// <summary>
///
/// </summary>
/// <param name="busyViewModel"></param>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public BaseCommand(IBusyViewModel busyViewModel, Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
_busyViewModel = busyViewModel;
}

/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public virtual bool CanExecute(object parameter = null)
{
CanExecuteChanged?.Invoke(this, new EventArgs());
return _canExecute == null || _canExecute(parameter);
try
{
return _canExecute == null || _canExecute(parameter);
}
catch (Exception ex)
{
_busyViewModel?.OnError(ex);
throw;
}
}

/// <summary>
Expand All @@ -43,6 +74,26 @@ public virtual bool CanExecute(object parameter = null)
/// <param name="parameter"></param>
public virtual void Execute(object parameter)
{
_execute(parameter);
try
{
if (_busyViewModel != null)
{
_busyViewModel.Busy();
CanExecuteChanged?.Invoke(this, new EventArgs());
}
_execute(parameter);
}
catch (Exception ex)
{
_busyViewModel?.OnError(ex);
}
finally
{
if (_busyViewModel != null)
{
_busyViewModel.UnBusy();
CanExecuteChanged?.Invoke(this, new EventArgs());
}
}
}
}
106 changes: 106 additions & 0 deletions src/CSharp/EasyMicroservices.UI.Core.Mvvm/Commands/TaskBaseCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using EasyMicroservices.UI.Core.Interfaces;
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace EasyMicroservices.UI.Core.Commands
{
/// <summary>
///
/// </summary>
public class TaskBaseCommand : ICommand
{
/// <summary>
///
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
///
/// </summary>
protected readonly Func<object, Task> _execute = null;
/// <summary>
///
/// </summary>
protected readonly Func<object, bool> _canExecute = null;
/// <summary>
/// /
/// </summary>
protected readonly IBusyViewModel _busyViewModel = null;

/// <summary>
///
/// </summary>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public TaskBaseCommand(Func<object, Task> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}

/// <summary>
///
/// </summary>
/// <param name="busyViewModel"></param>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public TaskBaseCommand(IBusyViewModel busyViewModel, Func<object, Task> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
_busyViewModel = busyViewModel;
}

/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public virtual bool CanExecute(object parameter = null)
{
try
{
return _canExecute == null || _canExecute(parameter);
}
catch (Exception ex)
{
_busyViewModel?.OnError(ex);
throw;
}
}

/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
public virtual void Execute(object parameter)
{
if (_busyViewModel != null)
{
_busyViewModel.Busy();
CanExecuteChanged?.Invoke(this, new EventArgs());
}
_ = InternalExecute(parameter);
}

async Task InternalExecute(object parameter)
{
try
{
await _execute(parameter);
}
catch (Exception ex)
{
_busyViewModel?.OnError(ex);
}
finally
{
if (_busyViewModel != null)
{
_busyViewModel.UnBusy();
CanExecuteChanged?.Invoke(this, new EventArgs());
}
}
}
}
}
106 changes: 106 additions & 0 deletions src/CSharp/EasyMicroservices.UI.Core.Mvvm/Commands/TaskRelayCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using EasyMicroservices.UI.Core.Interfaces;
using System;
using System.Threading.Tasks;

namespace EasyMicroservices.UI.Core.Commands
{
/// <summary>
///
/// </summary>
public class TaskRelayCommand : TaskBaseCommand
{
/// <summary>
///
/// </summary>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public TaskRelayCommand(Func<Task> execute, Func<bool> canExecute)
: this(default, execute, canExecute)
{

}

/// <summary>
///
/// </summary>
/// <param name="execute"></param>
public TaskRelayCommand(Func<Task> execute)
: this(default, execute, () => true)
{

}

/// <summary>
///
/// </summary>
/// <param name="busyViewModel"></param>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public TaskRelayCommand(IBusyViewModel busyViewModel, Func<Task> execute, Func<bool> canExecute)
: base(busyViewModel, (x) => execute(), (x) => canExecute())
{

}

/// <summary>
///
/// </summary>
/// <param name="busyViewModel"></param>
/// <param name="execute"></param>
public TaskRelayCommand(IBusyViewModel busyViewModel, Func<Task> execute)
: this(busyViewModel, execute, () => true)
{

}
}

/// <summary>
///
/// </summary>
public class TaskRelayCommand<T> : TaskBaseCommand
{
/// <summary>
///
/// </summary>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public TaskRelayCommand(Func<T, Task> execute, Func<T, bool> canExecute)
: this(default, execute, canExecute)
{

}

/// <summary>
///
/// </summary>
/// <param name="execute"></param>
public TaskRelayCommand(Func<T, Task> execute)
: this(default, execute, (x) => true)
{

}

/// <summary>
///
/// </summary>
/// <param name="busyViewModel"></param>
/// <param name="execute"></param>
/// <param name="canExecute"></param>
public TaskRelayCommand(IBusyViewModel busyViewModel, Func<T, Task> execute, Func<T, bool> canExecute)
: base(busyViewModel, (x) => execute((T)x), (x) => canExecute((T)x))
{

}

/// <summary>
///
/// </summary>
/// <param name="busyViewModel"></param>
/// <param name="execute"></param>
public TaskRelayCommand(IBusyViewModel busyViewModel, Func<T, Task> execute)
: this(busyViewModel, execute, (x) => true)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.3</Version>
<Version>0.0.0.4</Version>
<Description>Model View View Model</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>mvvm,mvpvm,modelview,modelviewviewmodel</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace EasyMicroservices.UI.Core.Interfaces
{
/// <summary>
///
/// </summary>
public interface IBusyViewModel : INotifyPropertyChanged
{
/// <summary>
///
/// </summary>
Action<bool> OnBusyChanged { get; set; }

/// <summary>
///
/// </summary>
bool IsBusy { get; set; }

/// <summary>
///
/// </summary>
void Busy();

/// <summary>
///
/// </summary>
void UnBusy();
/// <summary>
///
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
Task OnError(Exception exception);
}
}

0 comments on commit 67f9547

Please sign in to comment.