< Summary

Information
Class: MyNet.UI.Loading.Models.Busy
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Loading/Models/Busy.cs
Tag: 323_28699572109
Line coverage
85%
Covered lines: 17
Uncovered lines: 3
Coverable lines: 20
Total lines: 93
Line coverage: 85%
Branch coverage
37%
Covered branches: 3
Total branches: 8
Branch coverage: 37.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
get_CancellationToken()0%620%
get_IsCancellable()50%22100%
set_CanCancel(...)100%210%
BindCancellation(...)100%11100%
Cancel()50%4483.33%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Loading/Models/Busy.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="Busy.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading;
 9using System.Windows.Input;
 10using MyNet.Observable;
 11using MyNet.UI.Commands;
 12
 13namespace MyNet.UI.Loading.Models;
 14
 15/// <summary>
 16/// Represents a busy indicator that supports cancellation.
 17/// </summary>
 18public class Busy : ObservableObject, IBusy
 19{
 20    private CancellationTokenSource? _cts;
 21
 22    /// <summary>
 23    /// Initializes a new instance of the <see cref="Busy"/> class.
 24    /// </summary>
 25    public Busy()
 11426        : this(RelayCommandFactory.Default)
 27    {
 11428    }
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="Busy"/> class.
 32    /// </summary>
 33    /// <param name="commandFactory">Command factory used to create the cancellation command.</param>
 11434    public Busy(ICommandFactory commandFactory)
 35    {
 11436        ArgumentNullException.ThrowIfNull(commandFactory);
 37        CancelCommand = commandFactory.Create(Cancel, () => IsCancellable && CanCancel && !IsCancelling);
 11438    }
 39
 40    /// <summary>
 41    /// Gets the cancellation token associated with this busy operation.
 42    /// </summary>
 043    public CancellationToken CancellationToken => _cts?.Token ?? CancellationToken.None;
 44
 45    /// <summary>
 46    /// Gets a value indicating whether cancellation is possible.
 47    /// </summary>
 348    public bool IsCancellable => _cts is { IsCancellationRequested: false };
 49
 50    /// <summary>
 51    /// Gets the command used to trigger cancellation.
 52    /// </summary>
 53    public ICommand CancelCommand { get; }
 54
 55    /// <summary>
 56    /// Gets a value indicating whether cancellation is in progress.
 57    /// </summary>
 58    public bool IsCancelling { get; private set; }
 59
 60    /// <summary>
 61    /// Gets or sets a value indicating whether cancellation can be triggered by the user.
 62    /// </summary>
 063    public bool CanCancel { get; set => SetProperty(ref field, value); } = true;
 64
 65    /// <summary>
 66    /// Binds a cancellation source to this busy indicator.
 67    /// Called by <see cref="Loading.BusyService"/> when a scope starts.
 68    /// </summary>
 69    /// <param name="cancellationTokenSource">The source that drives <see cref="CancellationToken"/> and user-initiated 
 70    public void BindCancellation(CancellationTokenSource cancellationTokenSource)
 71    {
 10872        ArgumentNullException.ThrowIfNull(cancellationTokenSource);
 10873        _cts = cancellationTokenSource;
 10874        IsCancelling = false;
 10875        NotifyPropertyChanged(nameof(IsCancellable));
 10876        NotifyPropertyChanged(nameof(IsCancelling));
 10877    }
 78
 79    /// <summary>
 80    /// Requests cancellation of the busy operation.
 81    /// </summary>
 82    public void Cancel()
 83    {
 384        if (!IsCancellable)
 085            return;
 86
 387        IsCancelling = true;
 388        NotifyPropertyChanged(nameof(IsCancelling));
 89
 390        _cts?.Cancel();
 391    }
 92}
 93