| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CommandFactoryExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using System.Windows.Input; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 |
| | | 13 | | namespace MyNet.UI.Commands; |
| | | 14 | | #pragma warning restore IDE0130 |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides convenience helpers around <see cref="ICommandFactory"/>. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class CommandFactoryExtensions |
| | | 20 | | { |
| | | 21 | | extension(ICommandFactory? factory) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Returns the factory instance or <see cref="RelayCommandFactory.Default"/> when null. |
| | | 25 | | /// </summary> |
| | | 26 | | [SuppressMessage("Design", "CA1024:Use properties where appropriate", Justification = "Method is more discoverab |
| | 516 | 27 | | public ICommandFactory GetOrDefault() => factory ?? RelayCommandFactory.Default; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | extension(ICommandFactory factory) |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// Creates a command that requires a non-null parameter of type T. If the parameter is null, the command will n |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="execute">The action to execute.</param> |
| | | 36 | | /// <typeparam name="T">The type of the parameter.</typeparam> |
| | | 37 | | /// <returns>A command that executes the action if the parameter is not null.</returns> |
| | | 38 | | /// <exception cref="ArgumentNullException">Thrown if the factory or execute action is null.</exception> |
| | | 39 | | public ICommand CreateRequired<T>(Action<T> execute) |
| | | 40 | | { |
| | 90 | 41 | | ArgumentNullException.ThrowIfNull(factory); |
| | 90 | 42 | | ArgumentNullException.ThrowIfNull(execute); |
| | | 43 | | |
| | 90 | 44 | | return factory.Create<T>(x => |
| | 90 | 45 | | { |
| | 90 | 46 | | if (x is null) |
| | 90 | 47 | | return; |
| | 90 | 48 | | |
| | 90 | 49 | | execute.Invoke(x); |
| | 90 | 50 | | }); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Creates a command that requires a non-null parameter of type T and a canExecute function. If the parameter i |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="execute">The action to execute.</param> |
| | | 57 | | /// <param name="canExecute">The function that determines whether the command can execute.</param> |
| | | 58 | | /// <typeparam name="T">The type of the parameter.</typeparam> |
| | | 59 | | /// <returns>A command that executes the action if the parameter is not null and canExecute returns true.</retur |
| | | 60 | | /// <exception cref="ArgumentNullException">Thrown if the factory, execute action, or canExecute function is nul |
| | | 61 | | public ICommand CreateRequired<T>(Action<T> execute, Func<T, bool> canExecute) |
| | | 62 | | { |
| | 45 | 63 | | ArgumentNullException.ThrowIfNull(factory); |
| | 45 | 64 | | ArgumentNullException.ThrowIfNull(execute); |
| | 45 | 65 | | ArgumentNullException.ThrowIfNull(canExecute); |
| | | 66 | | |
| | 45 | 67 | | return factory.Create<T>(x => |
| | 45 | 68 | | { |
| | 45 | 69 | | if (x is null) |
| | 45 | 70 | | return; |
| | 45 | 71 | | |
| | 45 | 72 | | execute.Invoke(x); |
| | 45 | 73 | | }, |
| | 45 | 74 | | x => x is not null && canExecute.Invoke(x)); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Creates an asynchronous command that requires a non-null parameter of type T. If the parameter is null, the |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="execute">The asynchronous action to execute.</param> |
| | | 81 | | /// <typeparam name="T">The type of the parameter.</typeparam> |
| | | 82 | | /// <returns>A command that executes the asynchronous action if the parameter is not null.</returns> |
| | | 83 | | /// <exception cref="ArgumentNullException">Thrown if the factory or execute action is null.</exception> |
| | | 84 | | public ICommand CreateRequired<T>(Func<T, Task> execute) |
| | | 85 | | { |
| | 0 | 86 | | ArgumentNullException.ThrowIfNull(factory); |
| | 0 | 87 | | ArgumentNullException.ThrowIfNull(execute); |
| | | 88 | | |
| | 0 | 89 | | return factory.Create<T>( |
| | 0 | 90 | | x => x is null ? Task.CompletedTask : execute(x)); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Creates an asynchronous command that requires a non-null parameter of type T and a canExecute function. If t |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="execute">The asynchronous action to execute.</param> |
| | | 97 | | /// <param name="canExecute">The function that determines whether the command can execute.</param> |
| | | 98 | | /// <typeparam name="T">The type of the parameter.</typeparam> |
| | | 99 | | /// <returns>A command that executes the asynchronous action if the parameter is not null and canExecute returns |
| | | 100 | | /// <exception cref="ArgumentNullException">Thrown if the factory, execute action, or canExecute function is nul |
| | | 101 | | public ICommand CreateRequired<T>(Func<T, Task> execute, Func<T, bool> canExecute) |
| | | 102 | | { |
| | 42 | 103 | | ArgumentNullException.ThrowIfNull(factory); |
| | 42 | 104 | | ArgumentNullException.ThrowIfNull(execute); |
| | 42 | 105 | | ArgumentNullException.ThrowIfNull(canExecute); |
| | | 106 | | |
| | 42 | 107 | | return factory.Create<T>( |
| | 42 | 108 | | x => x is null ? Task.CompletedTask : execute(x), |
| | 42 | 109 | | x => x is not null && canExecute.Invoke(x)); |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |