Skip to content

feat: Add ItemCommandTask #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ public static IDynamicCommand GetCommandFromTask<TParameter>(
[CallerMemberName] string name = null
) => viewModel.GetOrCreateCommand(name, n => viewModel.GetDynamicCommandBuilderFactory().CreateFromTask(n, execute), configure);

/// <summary>
/// Gets or creates a unique instance of <see cref="IDynamicCommand"/> configured for every items in a list
/// with base name + guid.
/// </summary>
/// <param name="viewModel">The ViewModel.</param>
/// <param name="execute">The execute method.</param>
/// <param name="configure">The optional function to configure the command builder.</param>
/// <param name="name">The command name.</param>
/// <returns><see cref="IDynamicCommand"/></returns>
public static IDynamicCommand GetItemCommandFromTask(
this IViewModel viewModel, Func<CancellationToken, Task> execute,
Func<IDynamicCommandBuilder, IDynamicCommandBuilder> configure = null,
[CallerMemberName] string name = null
) => viewModel.GetCommandFromTask(execute, configure: c => c, name: (name + Guid.NewGuid()));

/// <summary>
/// Gets or creates a unique instance of <see cref="IDynamicCommand"/> configured for every items in a list.
/// with base name + guid.
/// </summary>
/// <typeparam name="TParameter">The type of the command parameter.</typeparam>
/// <param name="viewModel">The ViewModel.</param>
/// <param name="execute">The execute method.</param>
/// <param name="configure">The optional function to configure the command builder.</param>
/// <param name="name">The command name.</param>
/// <returns><see cref="IDynamicCommand"/></returns>
public static IDynamicCommand GetItemCommandFromTask<TParameter>(
this IViewModel viewModel, Func<CancellationToken, TParameter, Task> execute,
Func<IDynamicCommandBuilder, IDynamicCommandBuilder> configure = null,
[CallerMemberName] string name = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this parameter could be renamed to namePrefix because that's what it really is

) => viewModel.GetCommandFromTask<TParameter>(execute, configure: c => c, name: (name + Guid.NewGuid()));

/// <summary>
/// Gets or creates a <see cref="IDynamicCommand"/> that will be attached to the <paramref name="viewModel"/>.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/DynamicMvvm.Abstractions/ViewModel/IViewModel.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ public class MyViewModel : ViewModelBase
// command parameter when executed. The CancellationToken will be cancelled if the IViewModel is disposed.
public IDynamicCommand MyCommandFromTaskWithParameter => this.GetCommandFromTask<int>(ExecuteMyTaskWithParameter);
private async Task ExecuteMyTaskWithParameter(CancellationToken ct, int parameter) => Task.CompletedTask;

// This will create and attach a new IDynamicCommand named "MyCommandForItemFromTask + GUID"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of repeating the same thing as before, I would just refer to GetCommandFromTask and explain what the ItemCommand does differently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be the best strategy for the XML doc as well (if possible).

// to your IViewModel. It will call the ExecuteMyTask method when executed.
// This is useful for items in a list to excecute the command only for the selected item.
// The CancellationToken will be cancelled if the IViewModel is disposed.
public IDynamicCommand MyCommandForItemFromTask => this.GetItemCommandFromTask(ExecuteMyTask);
private async Task ExecuteMyTask(CancellationToken ct) => Task.CompletedTask;

// This will create and attach a new IDynamicCommand named "MyCommandForItemFromTaskWithParameter + GUID"
// to your IViewModel. It will call the ExecuteMyTaskWithParameter method with its command parameter when executed.
// This is useful for items in a list to excecute the command only for the selected item.
// The CancellationToken will be cancelled if the IViewModel is disposed.
public IDynamicCommand MyCommandForItemFromTaskWithParameter => this.GetItemCommandFromTask<int>(ExecuteMyTaskWithParameter);
private async Task ExecuteMyTaskWithParameter(CancellationToken ct, int parameter) => Task.CompletedTask;
}
```

Expand Down
14 changes: 14 additions & 0 deletions src/DynamicMvvm.Tests/ViewModel/ViewModelBaseCommandsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public async Task It_Gets_From_Task_T()
receivedParameter.Should().Be(parameter);
}

[Fact]
public async Task It_Get_From_Item_Task()
{
var receivedParameter = default(TestEntity);
var viewModel = new ViewModelBase(serviceProvider: _serviceProvider);

var command = viewModel.GetItemCommandFromTask<TestEntity>(async (ct, p) => receivedParameter = p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a test that validates that all instances returned by this method are different, even when using the same name.


var parameter = new TestEntity();
await command.Execute(parameter);

receivedParameter.Should().Be(parameter);
}

[Fact]
public void It_Adds_Disposable()
{
Expand Down