-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: main
Are you sure you want to change the base?
Changes from all commits
51ca08c
be113a7
8a3887d
aa0b292
7dcfc5a
1ebf1f2
be2a390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this parameter could be renamed to |
||
) => 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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.