< Summary

Information
Class: MyNet.UI.Commands.RelayCommand<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Commands/RelayCommand.cs
Tag: 323_28699572109
Line coverage
87%
Covered lines: 7
Uncovered lines: 1
Coverable lines: 8
Total lines: 96
Line coverage: 87.5%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)83.33%66100%
CanExecute(...)100%88100%
Execute(...)50%2275%
RaiseCanExecuteChanged()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Commands/RelayCommand.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="RelayCommand.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Reactive.Concurrency;
 9using System.Windows.Input;
 10using MyNet.UI.Threading;
 11
 12namespace MyNet.UI.Commands;
 13
 14/// <summary>
 15/// A command that relays its functionality to other objects by invoking delegates. The default return value for the Can
 16/// </summary>
 17/// <param name="execute">The action to execute.</param>
 18/// <param name="canExecute">A function that determines whether the command can execute.</param>
 19/// <param name="schedulerProvider">Optional scheduler provider used to raise CanExecuteChanged on the UI scheduler.</pa
 20public class RelayCommand(Action execute, Func<bool>? canExecute = null, ISchedulerProvider? schedulerProvider = null) :
 21{
 22    private readonly Action _execute = execute ?? throw new ArgumentNullException(nameof(execute));
 23    private readonly IScheduler _uiScheduler = schedulerProvider?.Ui ?? CurrentThreadScheduler.Instance;
 24
 25    /// <summary>
 26    /// Gets the function that determines whether the command can execute. If null, the command can always execute.
 27    /// </summary>
 28    public virtual event EventHandler? CanExecuteChanged;
 29
 30    /// <summary>
 31    /// Determines whether the command can execute in its current state. If the canExecute function is null, this method
 32    /// </summary>
 33    /// <param name="parameter">The parameter to pass to the canExecute function.</param>
 34    /// <returns>True if the command can execute; otherwise, false.</returns>
 35    public virtual bool CanExecute(object? parameter) => canExecute?.Invoke() ?? true;
 36
 37    /// <summary>
 38    /// Executes the command. If the command cannot execute, this method does nothing.
 39    /// </summary>
 40    /// <param name="parameter">The parameter to pass to the execute action.</param>
 41    public virtual void Execute(object? parameter)
 42    {
 43        if (!CanExecute(parameter))
 44            return;
 45
 46        _execute();
 47    }
 48
 49    /// <summary>
 50    /// Raises the CanExecuteChanged event to indicate that the return value of the CanExecute method has changed. This 
 51    /// </summary>
 52    public virtual void RaiseCanExecuteChanged() => _uiScheduler.Schedule(() => CanExecuteChanged?.Invoke(this, EventArg
 53}
 54
 55/// <summary>
 56/// A command that relays its functionality to other objects by invoking delegates. The default return value for the Can
 57/// </summary>
 58/// <param name="execute">The action to execute.</param>
 59/// <param name="canExecute">A function that determines whether the command can execute.</param>
 60/// <param name="schedulerProvider">Optional scheduler provider used to raise CanExecuteChanged on the UI scheduler.</pa
 61/// <typeparam name="T">The type of the parameter.</typeparam>
 62public class RelayCommand<T>(Action<T?> execute, Func<T?, bool>? canExecute = null, ISchedulerProvider? schedulerProvide
 63{
 27364    private readonly Action<T?> _execute = execute ?? throw new ArgumentNullException(nameof(execute));
 27365    private readonly IScheduler _uiScheduler = schedulerProvider?.Ui ?? CurrentThreadScheduler.Instance;
 66
 67    /// <summary>
 68    /// Gets the function that determines whether the command can execute. If null, the command can always execute.
 69    /// </summary>
 70    public event EventHandler? CanExecuteChanged;
 71
 72    /// <summary>
 73    /// Determines whether the command can execute in its current state. If the canExecute function is null, this method
 74    /// </summary>
 75    /// <param name="parameter">The parameter to pass to the canExecute function.</param>
 76    /// <returns>True if the command can execute; otherwise, false.</returns>
 1877    public bool CanExecute(object? parameter) => parameter is T or null && (canExecute?.Invoke((T?)parameter) ?? true);
 78
 79    /// <summary>
 80    /// Executes the command. If the command cannot execute, this method does nothing. The command can execute if the pa
 81    /// </summary>
 82    /// <param name="parameter">The parameter to pass to the execute action.</param>
 83    public void Execute(object? parameter)
 84    {
 1585        if (!CanExecute(parameter))
 086            return;
 87
 1588        _execute((T?)parameter);
 1589    }
 90
 91    /// <summary>
 92    /// Raises the CanExecuteChanged event to indicate that the return value of the CanExecute method has changed. This 
 93    /// </summary>
 13594    public virtual void RaiseCanExecuteChanged() => _uiScheduler.Schedule(() => CanExecuteChanged?.Invoke(this, EventArg
 95}
 96