forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsyncRelayCommand{T}.cs
More file actions
102 lines (89 loc) · 3.6 KB
/
Copy pathAsyncRelayCommand{T}.cs
File metadata and controls
102 lines (89 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
namespace Microsoft.Toolkit.Mvvm.Input
{
/// <summary>
/// A generic command that mirrors the functionality of <see cref="RelayCommand"/>, with the addition
/// of accepting a <see cref="Func{TResult}"/> returning a <see cref="Task{TResult}"/> as the execute
/// action, and providing a <see cref="ExecutionTask"/> property that notifies changes when
/// <see cref="Execute(T)"/> is invoked and when the returned <see cref="Task{TResult}"/> completes.
/// </summary>
/// <typeparam name="T">The type of parameter being passed as input to the callbacks.</typeparam>
public sealed class AsyncRelayCommand<T> : ObservableObject, IRelayCommand<T>
{
/// <summary>
/// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute(T)"/> is used.
/// </summary>
private readonly Func<T, Task> execute;
/// <summary>
/// The optional action to invoke when <see cref="CanExecute(T)"/> is used.
/// </summary>
private readonly Func<T, bool>? canExecute;
/// <inheritdoc/>
public event EventHandler? CanExecuteChanged;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public AsyncRelayCommand(Func<T, Task> execute)
{
this.execute = execute;
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public AsyncRelayCommand(Func<T, Task> execute, Func<T, bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
private Task? executionTask;
/// <summary>
/// Gets the last scheduled <see cref="Task"/>, if available.
/// This property notifies a change when the <see cref="Task{TResult}"/> completes.Th
/// </summary>
public Task? ExecutionTask
{
get => this.executionTask;
private set => SetAndNotifyOnCompletion(ref executionTask, () => executionTask, value);
}
/// <inheritdoc/>
public void NotifyCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanExecute(T parameter)
{
return this.canExecute?.Invoke(parameter) != false;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanExecute(object parameter)
{
return CanExecute((T)parameter);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Execute(T parameter)
{
if (CanExecute(parameter))
{
ExecutionTask = this.execute(parameter);
}
}
/// <inheritdoc/>
public void Execute(object parameter)
{
Execute((T)parameter);
}
}
}