Skip to content

Commit

Permalink
Merge pull request #5 from kristoffer-tungland/2-rename-command-to-fl…
Browse files Browse the repository at this point in the history
…uentcommand-and-asynccommand-to-fluentasynccommand

2 rename command to fluentcommand and asynccommand to fluentasynccommand
  • Loading branch information
kristoffer-tungland authored Nov 4, 2024
2 parents 154a9ab + b61ff3b commit 2b59106
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 80 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,20 @@ public class MyViewModel : ViewModelBase
```
In this example, if the `Counter` property hasn’t been set before, the Get(10) will return 10 as the default value.

### Asynchronous FluentCommand (`AsyncCommand`)
The AsyncCommand in the MVVMFluent library is designed to simplify asynchronous command execution with built-in support for cancellation, progress reporting, and exception handling. It allows you to execute long-running tasks without blocking the UI thread, while maintaining full control over the command's lifecycle.
### Asynchronous FluentCommand (`AsyncFluentCommand`)
The AsyncFluentCommand in the MVVMFluent library is designed to simplify asynchronous command execution with built-in support for cancellation, progress reporting, and exception handling. It allows you to execute long-running tasks without blocking the UI thread, while maintaining full control over the command's lifecycle.

**Key Features:**
- **Cancellation Support:** The `CancelCommand` property allows users to stop an ongoing asynchronous operation, making it ideal for tasks that may take a significant amount of time or need to be interrupted.
- **Execution State Tracking:** The command exposes an `IsRunning` property to indicate whether the command is currently executing, which can be bound to UI elements like progress bars or buttons to reflect the task status dynamically.
- **Progress Reporting:** The `Progress` property enables reporting task completion status, which can be updated via the ReportProgress method. This is especially useful when performing iterative tasks or tasks with multiple stages.
- **Fluent Configuration:** Similar to the rest of the MVVMFluent library, the `AsyncCommand` can be configured fluently, allowing you to chain conditions (`If()`), exception handling (`Handle()`), and cancellation logic seamlessly.
- **Fluent Configuration:** Similar to the rest of the MVVMFluent library, the `AsyncFluentCommand` can be configured fluently, allowing you to chain conditions (`If()`), exception handling (`Handle()`), and cancellation logic seamlessly.

Example Usage
In your view model:

```csharp
public AsyncCommand LoadCommand =>
public AsyncFluentCommand LoadCommand =>
Do(LoadData)
.If(() => CanLoad)
.Handle(ex => HandleError(ex));
Expand Down Expand Up @@ -173,10 +173,10 @@ In XAML:
<Button Content="Cancel" FluentCommand="{Binding LoadCommand.CancelCommand}" />
```

This example demonstrates how AsyncCommand integrates with both your view model and XAML, providing a responsive and user-friendly interface for long-running or cancellable operations.
This example demonstrates how AsyncFluentCommand integrates with both your view model and XAML, providing a responsive and user-friendly interface for long-running or cancellable operations.

### Asynchronous FluentCommand with Parameter (`AsyncCommand<T>`)
The `AsyncCommand<T>` extends the functionality of AsyncCommand by allowing you to pass a parameter of type `T` to the asynchronous execution logic. This makes it ideal for scenarios where the command needs to act on dynamic data or context-specific parameters. Like `AsyncCommand`, it supports cancellation, progress reporting, and exception handling, while maintaining the same fluent API for configuration.
### Asynchronous FluentCommand with Parameter (`AsyncFluentCommand<T>`)
The `AsyncFluentCommand<T>` extends the functionality of AsyncFluentCommand by allowing you to pass a parameter of type `T` to the asynchronous execution logic. This makes it ideal for scenarios where the command needs to act on dynamic data or context-specific parameters. Like `AsyncFluentCommand`, it supports cancellation, progress reporting, and exception handling, while maintaining the same fluent API for configuration.

### Validation Fluent Setter (`ValidationFluentSetter<TValue>`)
The `ValidationFluentSetter<TValue>` class in the `MVVMFluent.WPF` library provides a flexible framework for implementing validation in your MVVM applications. It supports three distinct validation methods: Built-in Validation Rules allow the use of WPF’s standard validation rules, which can be added to the setter using the `Validate(params ValidationRule[] rules)` method. For example:
Expand Down Expand Up @@ -356,7 +356,7 @@ namespace MVVMFluent.Demo
return !string.IsNullOrWhiteSpace(value);
}

public AsyncCommand LoadCommand =>
public AsyncFluentCommand LoadCommand =>
Do(LoadData)
.If(() => Comments?.Contains("Load") == true)
.Handle(HandleError);
Expand Down
8 changes: 4 additions & 4 deletions src/MVVMFluent.Demo/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MainViewModel()
public string? Input
{
get => Get<string?>();
set => When(value).Required().Notify(AsyncCommand, OkCommand).Set();
set => When(value).Required().Notify(AsyncFluentCommand, OkCommand).Set();
}

public FluentCommand OkCommand => Do(() => ShowDialog(Input)).IfValid(nameof(Input));
Expand All @@ -27,7 +27,7 @@ private bool CanExecute()

public bool ThrowException { get => Get(false); set => Set(value); }

public AsyncCommand AsyncCommand => Do(ShowDialogAsync).If(CanExecute).Handle(HandleException).ConfigureAwait(false);
public AsyncFluentCommand AsyncFluentCommand => Do(ShowDialogAsync).If(CanExecute).Handle(HandleException).ConfigureAwait(false);

private void HandleException(Exception exception)
{
Expand All @@ -45,9 +45,9 @@ private async Task ShowDialogAsync(CancellationToken cancellationToken)
throw new TaskCanceledException();

await Task.Delay(50, cancellationToken);
AsyncCommand.ReportProgress(i+1, 100);
AsyncFluentCommand.ReportProgress(i+1, 100);

if (AsyncCommand.Progress == 50 && ThrowException)
if (AsyncFluentCommand.Progress == 50 && ThrowException)
throw new Exception("Something went wrong");

}
Expand Down
8 changes: 4 additions & 4 deletions src/MVVMFluent.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
</TextBox>

<ProgressBar Grid.Row="2" VerticalAlignment="Center" Padding="10" Margin="10"
Value="{Binding AsyncCommand.Progress}" Minimum="0" Maximum="100" Height="10">
Value="{Binding AsyncFluentCommand.Progress}" Minimum="0" Maximum="100" Height="10">
<ProgressBar.Style>
<Style TargetType="ProgressBar">
<Style.Triggers>
<DataTrigger Binding="{Binding AsyncCommand.IsRunning}" Value="False">
<DataTrigger Binding="{Binding AsyncFluentCommand.IsRunning}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
Expand All @@ -59,11 +59,11 @@

<Button DockPanel.Dock="Right" Padding="15,0" Margin="10"
Content="Async"
Command="{Binding AsyncCommand}" />
Command="{Binding AsyncFluentCommand}" />

<Button DockPanel.Dock="Right" Padding="15,0" Margin="10"
Content="Cancel"
Command="{Binding AsyncCommand.CancelCommand}" />
Command="{Binding AsyncFluentCommand.CancelCommand}" />

<CheckBox DockPanel.Dock="Left" Padding="10" Margin="10,5"
VerticalAlignment="Center" VerticalContentAlignment="Center"
Expand Down
4 changes: 2 additions & 2 deletions src/MVVMFluent.WPF/CommandValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static FluentCommand<T> IfValid<T>(this FluentCommand<T> command, params
/// <param name="propertyName">The name of the property to check for errors.</param>
/// <returns>The command with the condition added.</returns>
/// <exception cref="global::System.ArgumentNullException">Thrown when the property name is null or empty.</exception>
public static AsyncCommand IfValid(this AsyncCommand command, params string[] propertyName)
public static AsyncFluentCommand IfValid(this AsyncFluentCommand command, params string[] propertyName)
{
if (command.IsBuilt)
return command;
Expand All @@ -65,7 +65,7 @@ public static AsyncCommand IfValid(this AsyncCommand command, params string[] pr
/// <param name="propertyName">The name of the property to check for errors.</param>
/// <returns>The command with the condition added.</returns>
/// <exception cref="global::System.ArgumentNullException">Thrown when the property name is null or empty.</exception>
public static AsyncCommand<T> IfValid<T>(this AsyncCommand<T> command, params string[] propertyName)
public static AsyncFluentCommand<T> IfValid<T>(this AsyncFluentCommand<T> command, params string[] propertyName)
{
if (command.IsBuilt)
return command;
Expand Down
Loading

0 comments on commit 2b59106

Please sign in to comment.