< Summary

Information
Class: MyNet.Observable.Collections.Selection.SelectableCollection
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Selection/SelectableCollection.cs
Tag: 323_28699572109
Line coverage
50%
Covered lines: 4
Uncovered lines: 4
Coverable lines: 8
Total lines: 181
Line coverage: 50%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
From(...)100%11100%
FromObservable(...)100%210%
Empty(...)100%210%
Create(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Selection/SelectableCollection.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectableCollection.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.Collections.ObjectModel;
 10using System.Diagnostics.CodeAnalysis;
 11using System.Reactive.Concurrency;
 12using DynamicData;
 13using MyNet.Observable.Collections.Sources;
 14
 15namespace MyNet.Observable.Collections.Selection;
 16
 17/// <summary>
 18/// Provides factory methods to create selectable collections from various sources, allowing items to be selected and un
 19/// </summary>
 20[SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "This class serves as 
 21public static class SelectableCollection
 22{
 23    /// <summary>
 24    /// Creates a selectable collection from an enumerable.
 25    /// </summary>
 26    public static SelectableCollection<T> From<T>(
 27        IEnumerable<T> items,
 28        SelectionMode mode = SelectionMode.Multiple,
 29        IScheduler? scheduler = null)
 30        where T : notnull
 31    {
 332        var source = SourceEngine<T>.From(items, readOnly: false);
 33
 334        return Create(source, mode, scheduler);
 35    }
 36
 37    /// <summary>
 38    /// Creates a selectable collection from an observable source.
 39    /// </summary>
 40    public static SelectableCollection<T> FromObservable<T>(
 41        IObservable<IChangeSet<T>> source,
 42        SelectionMode mode = SelectionMode.Multiple,
 43        IScheduler? scheduler = null)
 44        where T : notnull
 45    {
 046        var engine = SourceEngine<T>.FromObservable(source);
 47
 048        return Create(engine, mode, scheduler);
 49    }
 50
 51    /// <summary>
 52    /// Creates an empty selectable collection.
 53    /// </summary>
 54    public static SelectableCollection<T> Empty<T>(
 55        SelectionMode mode = SelectionMode.Multiple,
 56        IScheduler? scheduler = null)
 57        where T : notnull
 58    {
 059        var source = SourceEngine<T>.Empty();
 60
 061        return Create(source, mode, scheduler);
 62    }
 63
 64    /// <summary>
 65    /// Core factory. The created <see cref="ExtendedCollection{T}"/> is owned and disposed with the selectable collecti
 66    /// </summary>
 67    public static SelectableCollection<T> Create<T>(
 68        SourceEngine<T> source,
 69        SelectionMode mode = SelectionMode.Multiple,
 70        IScheduler? scheduler = null)
 71        where T : notnull
 72    {
 373        var collection = new ExtendedCollection<T>(source, scheduler);
 74
 375        return new(collection, mode, disposeCollection: true);
 76    }
 77}
 78
 79/// <summary>
 80/// Represents a collection of items that can be selected, providing methods to manage the selection state of the items.
 81/// </summary>
 82/// <typeparam name="T">The type of items in the collection.</typeparam>
 83[SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "This class wraps a co
 84public sealed class SelectableCollection<T> : IDisposable
 85    where T : notnull
 86{
 87    private readonly SelectionEngine<T> _selection;
 88    private readonly bool _disposeCollection;
 89
 90    /// <summary>
 91    /// Initializes a new instance of the <see cref="SelectableCollection{T}"/> class over an existing <see cref="Extend
 92    /// The underlying collection is not disposed unless <paramref name="disposeCollection"/> is <c>true</c>.
 93    /// </summary>
 94    /// <param name="collection">The extended collection whose filtered items participate in selection.</param>
 95    /// <param name="mode">The selection mode.</param>
 96    /// <param name="disposeCollection">When <c>true</c>, <see cref="Dispose"/> also disposes <paramref name="collection
 97    public SelectableCollection(
 98        ExtendedCollection<T> collection,
 99        SelectionMode mode = SelectionMode.Multiple,
 100        bool disposeCollection = false)
 101    {
 102        ArgumentNullException.ThrowIfNull(collection);
 103
 104        Collection = collection;
 105        _selection = new(collection.Connect(), mode);
 106        _disposeCollection = disposeCollection;
 107    }
 108
 109    /// <summary>
 110    /// Gets the selection mode of the collection, indicating whether multiple items can be selected simultaneously or o
 111    /// </summary>
 112    public SelectionMode Mode => _selection.Mode;
 113
 114    /// <summary>
 115    /// Gets the underlying extended collection.
 116    /// </summary>
 117    public ExtendedCollection<T> Collection { get; }
 118
 119    /// <summary>
 120    /// Gets the filtered items exposed by the underlying collection.
 121    /// </summary>
 122    public ReadOnlyObservableCollection<T> Items => Collection.Items;
 123
 124    /// <summary>
 125    /// Gets the collection of selected items in the selectable collection.
 126    /// </summary>
 127    public IEnumerable<T> SelectedItems => _selection.SelectedItems;
 128
 129    /// <summary>
 130    /// Gets the number of selected items in the selectable collection.
 131    /// </summary>
 132    public int SelectedCount => _selection.Count;
 133
 134    /// <summary>
 135    /// Selects the specified item in the selectable collection.
 136    /// </summary>
 137    /// <param name="item">The item to select.</param>
 138    public void Select(T item) => _selection.Select(item);
 139
 140    /// <summary>
 141    /// Unselects the specified item in the selectable collection.
 142    /// </summary>
 143    /// <param name="item">The item to unselect.</param>
 144    public void Unselect(T item) => _selection.Unselect(item);
 145
 146    /// <summary>
 147    /// Toggles the selection state of the specified item in the selectable collection.
 148    /// </summary>
 149    /// <param name="item">The item to toggle.</param>
 150    public void Toggle(T item) => _selection.Toggle(item);
 151
 152    /// <summary>
 153    /// Clears the selection in the selectable collection.
 154    /// </summary>
 155    public void ClearSelection() => _selection.Clear();
 156
 157    /// <summary>
 158    /// Sets the selection to the specified items in the selectable collection.
 159    /// </summary>
 160    /// <param name="items">The items to select.</param>
 161    public void SetSelection(IEnumerable<T> items) => _selection.Set(items);
 162
 163    /// <summary>
 164    /// Determines whether the specified item is selected in the selectable collection.
 165    /// </summary>
 166    /// <param name="item">The item to check.</param>
 167    /// <returns>True if the item is selected; otherwise, false.</returns>
 168    public bool IsSelected(T item) => _selection.Contains(item);
 169
 170    /// <summary>
 171    /// Disposes the selectable collection and releases all resources.
 172    /// </summary>
 173    public void Dispose()
 174    {
 175        _selection.Dispose();
 176
 177        if (_disposeCollection)
 178            Collection.Dispose();
 179    }
 180}
 181