< Summary

Information
Class: MyNet.UI.ViewModels.List.Selection.SelectableCollectionSelectionManager<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Selection/SelectableCollectionSelectionManager.cs
Tag: 323_28699572109
Line coverage
58%
Covered lines: 18
Uncovered lines: 13
Coverable lines: 31
Total lines: 113
Line coverage: 58%
Branch coverage
25%
Covered branches: 4
Total branches: 16
Branch coverage: 25%
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(...)0%620%
get_Mode()100%11100%
get_SelectedItems()100%11100%
get_SelectedCount()100%11100%
IsSelected(...)100%11100%
Select(...)50%22100%
Unselect(...)0%620%
Toggle(...)0%620%
ClearSelection()50%4480%
SetSelection(...)0%620%
Dispose()50%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Selection/SelectableCollectionSelectionManager.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectableCollectionSelectionManager.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using MyNet.Observable.Collections;
 11using MyNet.Observable.Collections.Selection;
 12
 13namespace MyNet.UI.ViewModels.List.Selection;
 14
 15/// <summary>
 16/// Selection manager backed by <see cref="SelectableCollection{T}"/>.
 17/// </summary>
 18/// <typeparam name="T">The item type.</typeparam>
 19public sealed class SelectableCollectionSelectionManager<T> : ISelectionManager<T>
 20    where T : notnull
 21{
 22    private readonly SelectableCollection<T> _selection;
 23    private readonly bool _ownsSelectable;
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="SelectableCollectionSelectionManager{T}"/> class.
 27    /// Creates a new selection manager with its own internal selectable collection (caller owns disposal of the collect
 28    /// </summary>
 29    /// <param name="collection">The extended collection to be managed.</param>
 30    /// <param name="mode">The selection mode to be used.</param>
 31    /// <exception cref="ArgumentNullException">Thrown if the collection is null.</exception>
 32    public SelectableCollectionSelectionManager(ExtendedCollection<T> collection, SelectionMode mode = SelectionMode.Mul
 33    {
 2734        ArgumentNullException.ThrowIfNull(collection);
 35
 2736        _selection = new(collection, mode, disposeCollection: false);
 2737        _ownsSelectable = true;
 2738    }
 39
 40    /// <summary>
 41    /// Initializes a new instance of the <see cref="SelectableCollectionSelectionManager{T}"/> class.
 42    /// Uses an existing selectable collection (caller owns disposal of the collection itself).
 43    /// </summary>
 44    /// <param name="selectable">The existing selectable collection to be managed.</param>
 45    /// <param name="disposeSelectable">A value indicating whether the selectable collection should be disposed when the
 46    /// <exception cref="ArgumentNullException">Thrown if the selectable collection is null.</exception>
 47    public SelectableCollectionSelectionManager(SelectableCollection<T> selectable, bool disposeSelectable = true)
 48    {
 049        _selection = selectable ?? throw new ArgumentNullException(nameof(selectable));
 050        _ownsSelectable = disposeSelectable;
 051    }
 52
 53    /// <inheritdoc />
 54    public event EventHandler? SelectionChanged;
 55
 56    /// <inheritdoc />
 657    public SelectionMode Mode => _selection.Mode;
 58
 59    /// <inheritdoc />
 2160    public IReadOnlyList<T> SelectedItems => [.. _selection.SelectedItems];
 61
 62    /// <inheritdoc />
 663    public int SelectedCount => _selection.SelectedCount;
 64
 65    /// <inheritdoc />
 666    public bool IsSelected(T item) => _selection.IsSelected(item);
 67
 68    /// <inheritdoc />
 69    public void Select(T item)
 70    {
 1571        _selection.Select(item);
 1572        SelectionChanged?.Invoke(this, EventArgs.Empty);
 1573    }
 74
 75    /// <inheritdoc />
 76    public void Unselect(T item)
 77    {
 078        _selection.Unselect(item);
 079        SelectionChanged?.Invoke(this, EventArgs.Empty);
 080    }
 81
 82    /// <inheritdoc />
 83    public void Toggle(T item)
 84    {
 085        _selection.Toggle(item);
 086        SelectionChanged?.Invoke(this, EventArgs.Empty);
 087    }
 88
 89    /// <inheritdoc />
 90    public void ClearSelection()
 91    {
 392        if (!_selection.SelectedItems.Any())
 093            return;
 94
 395        _selection.ClearSelection();
 396        SelectionChanged?.Invoke(this, EventArgs.Empty);
 397    }
 98
 99    /// <inheritdoc />
 100    public void SetSelection(IEnumerable<T> items)
 101    {
 0102        _selection.SetSelection(items);
 0103        SelectionChanged?.Invoke(this, EventArgs.Empty);
 0104    }
 105
 106    /// <inheritdoc />
 107    public void Dispose()
 108    {
 27109        if (_ownsSelectable)
 27110            _selection.Dispose();
 27111    }
 112}
 113