< Summary

Information
Class: MyNet.UI.ViewModels.List.Selection.SelectableListViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Selection/SelectableListViewModel.cs
Tag: 323_28699572109
Line coverage
92%
Covered lines: 39
Uncovered lines: 3
Coverable lines: 42
Total lines: 146
Line coverage: 92.8%
Branch coverage
55%
Covered branches: 10
Total branches: 18
Branch coverage: 55.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(...)50%1010100%
.ctor(...)50%22100%
get_SelectionMode()100%11100%
get_SelectedItems()100%11100%
get_SelectedCount()100%11100%
get_SelectedItem()100%11100%
set_SelectedItem(...)66.66%7666.66%
IsSelected(...)100%11100%
Select(...)100%11100%
Unselect(...)100%11100%
Toggle(...)100%11100%
ClearSelection()100%11100%
SetSelection(...)100%11100%
HandleSelectionChanged(...)100%11100%
DisposeManagedResources()100%11100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectableListViewModel.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.Diagnostics.CodeAnalysis;
 10using System.Linq;
 11using System.Reactive.Concurrency;
 12using MyNet.Observable.Collections;
 13using MyNet.Observable.Collections.Selection;
 14using MyNet.UI.ViewModels.List.Factories;
 15using MyNet.UI.ViewModels.List.Filtering;
 16using MyNet.UI.ViewModels.List.Grouping;
 17using MyNet.UI.ViewModels.List.Paging;
 18using MyNet.UI.ViewModels.List.Sorting;
 19
 20namespace MyNet.UI.ViewModels.List.Selection;
 21
 22/// <summary>
 23/// List view model with selection backed by <see cref="SelectableCollection{T}"/> (no UI wrappers).
 24/// </summary>
 25/// <typeparam name="T">The item type.</typeparam>
 26public class SelectableListViewModel<T> : ListViewModelBase<T, ExtendedCollection<T>>, ISelectableListViewModel<T>
 27    where T : notnull
 28{
 29    /// <summary>
 30    /// Initializes a new instance of the <see cref="SelectableListViewModel{T}"/> class.
 31    /// </summary>
 32    public SelectableListViewModel(
 33        ExtendedCollection<T> collection,
 34        SelectionMode mode = SelectionMode.Multiple,
 35        IFiltersViewModel<T>? filters = null,
 36        ISortingViewModel<T>? sorting = null,
 37        IGroupingViewModel<T>? grouping = null,
 38        IPagingViewModel? paging = null,
 39        IScheduler? scheduler = null)
 2740        : this(
 2741            collection,
 2742            new SelectableCollectionSelectionManager<T>(collection, mode),
 2743            filters,
 2744            sorting,
 2745            grouping,
 2746            paging,
 2747            scheduler)
 48    {
 2749    }
 50
 51    /// <summary>
 52    /// Initializes a new instance of the <see cref="SelectableListViewModel{T}"/> class.
 53    /// </summary>
 54    public SelectableListViewModel(
 55        ExtendedCollection<T> collection,
 56        SelectionMode mode,
 57        ListViewModelOptions<T>? options)
 2758        : this(collection, mode, options?.Filters, options?.Sorting, options?.Grouping, options?.Paging, options?.Schedu
 59    {
 2760    }
 61
 62    /// <summary>
 63    /// Initializes a new instance of the <see cref="SelectableListViewModel{T}"/> class.
 64    /// </summary>
 65    public SelectableListViewModel(
 66        ExtendedCollection<T> collection,
 67        ISelectionManager<T> selectionManager,
 68        IFiltersViewModel<T>? filters = null,
 69        ISortingViewModel<T>? sorting = null,
 70        IGroupingViewModel<T>? grouping = null,
 71        IPagingViewModel? paging = null,
 72        IScheduler? scheduler = null)
 5773        : base(collection, filters, sorting, grouping, paging, scheduler)
 74    {
 5775        _selection = selectionManager ?? throw new ArgumentNullException(nameof(selectionManager));
 5776        _selection.SelectionChanged += HandleSelectionChanged;
 5777    }
 78
 79    [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed in Cleanup.")]
 80    private readonly ISelectionManager<T> _selection;
 81
 82    /// <inheritdoc />
 683    public SelectionMode SelectionMode => _selection.Mode;
 84
 85    /// <inheritdoc />
 2486    public IReadOnlyList<T> SelectedItems => _selection.SelectedItems;
 87
 88    /// <inheritdoc />
 989    public int SelectedCount => _selection.SelectedCount;
 90
 91    /// <inheritdoc />
 92    public T? SelectedItem
 93    {
 1894        get => SelectedItems.FirstOrDefault();
 95        set
 96        {
 997            if (EqualityComparer<T>.Default.Equals(SelectedItem, value))
 098                return;
 99
 9100            if (value is null)
 101            {
 3102                ClearSelection();
 3103                return;
 104            }
 105
 6106            if (SelectionMode == SelectionMode.Single)
 6107                Select(value);
 108            else
 0109                SetSelection([value]);
 0110        }
 111    }
 112
 113    /// <inheritdoc />
 9114    public bool IsSelected(T item) => _selection.IsSelected(item);
 115
 116    /// <inheritdoc />
 18117    public void Select(T item) => _selection.Select(item);
 118
 119    /// <inheritdoc />
 3120    public void Unselect(T item) => _selection.Unselect(item);
 121
 122    /// <inheritdoc />
 3123    public void Toggle(T item) => _selection.Toggle(item);
 124
 125    /// <inheritdoc />
 6126    public void ClearSelection() => _selection.ClearSelection();
 127
 128    /// <inheritdoc />
 3129    public void SetSelection(IEnumerable<T> items) => _selection.SetSelection(items);
 130
 131    private void HandleSelectionChanged(object? sender, EventArgs e)
 132    {
 21133        NotifyPropertyChanged(nameof(SelectedItems));
 21134        NotifyPropertyChanged(nameof(SelectedCount));
 21135        NotifyPropertyChanged(nameof(SelectedItem));
 21136    }
 137
 138    /// <inheritdoc />
 139    protected override void DisposeManagedResources()
 140    {
 57141        _selection.SelectionChanged -= HandleSelectionChanged;
 57142        _selection.Dispose();
 57143        base.DisposeManagedResources();
 57144    }
 145}
 146