-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support for TaskRelayCommand and IBusyViewModel
- Loading branch information
1 parent
5f2a060
commit 67f9547
Showing
6 changed files
with
317 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/CSharp/EasyMicroservices.UI.Core.Mvvm/Commands/TaskBaseCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
src/CSharp/EasyMicroservices.UI.Core.Mvvm/Commands/TaskRelayCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
38 changes: 38 additions & 0 deletions
38
src/CSharp/EasyMicroservices.UI.Core.Mvvm/Interfaces/IBusyViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |