< Summary

Information
Class: MyNet.Observable.Behaviors.SelectionBehavior
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Behaviors/SelectionBehavior.cs
Tag: 323_28699572109
Line coverage
80%
Covered lines: 20
Uncovered lines: 5
Coverable lines: 25
Total lines: 83
Line coverage: 80%
Branch coverage
56%
Covered branches: 9
Total branches: 16
Branch coverage: 56.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_IsSelectable()100%210%
set_IsSelectable(...)50%9877.77%
get_IsSelected()100%11100%
set_IsSelected(...)66.66%6683.33%
SetSelected(...)50%2280%
NotifyChanged(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Behaviors/SelectionBehavior.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectionBehavior.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7namespace MyNet.Observable.Behaviors;
 8
 9/// <summary>
 10/// Provides selection state (IsSelected / IsSelectable) for an ObservableObject.
 11/// The behavior implements <see cref="ISelectable"/> and raises property changed
 12/// notifications on the owner when the selection state changes.
 13/// </summary>
 14/// <remarks>
 15/// Initializes a new instance of the <see cref="SelectionBehavior"/> class.
 16/// </remarks>
 17/// <param name="owner">The owner observable object.</param>
 18/// <param name="isSelectable">Initial value for IsSelectable (default true).</param>
 619public sealed class SelectionBehavior(ObservableObject owner, bool isSelectable = true) : SuspendableBehavior<Observable
 20{
 621    private bool _isSelectable = isSelectable;
 22    private bool _isSelected;
 23
 24    /// <inheritdoc />
 25    public bool IsSelectable
 26    {
 027        get => _isSelectable;
 28        set
 29        {
 330            if (IsDisposed || IsSuspended)
 031                return;
 32
 333            if (_isSelectable == value)
 034                return;
 35
 336            _isSelectable = value;
 37
 338            if (!_isSelectable)
 39            {
 340                SetSelected(false);
 41            }
 42
 343            NotifyChanged(nameof(IsSelectable));
 344        }
 45    }
 46
 47    /// <inheritdoc />
 48    public bool IsSelected
 49    {
 350        get => _isSelected;
 51        set
 52        {
 653            if (IsDisposed || IsSuspended)
 354                return;
 55
 356            if (!_isSelectable)
 057                return;
 58
 359            SetSelected(value);
 360        }
 61    }
 62
 63    /// <summary>
 64    /// Sets the IsSelected property to the specified value and raises a property changed notification if the value has 
 65    /// </summary>
 66    /// <param name="value">The new value for the IsSelected property.</param>
 67    private void SetSelected(bool value)
 68    {
 669        if (_isSelected == value)
 070            return;
 71
 672        _isSelected = value;
 73
 674        NotifyChanged(nameof(IsSelected));
 675    }
 76
 77    /// <summary>
 78    /// Notifies the owner that a property has changed by raising the PropertyChanged event with the specified property 
 79    /// </summary>
 80    /// <param name="propertyName">The name of the property that changed.</param>
 981    private void NotifyChanged(string propertyName) => Owner.NotifyPropertyChanged(propertyName);
 82}
 83