| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectableCollection.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Reactive.Concurrency; |
| | | 12 | | using DynamicData; |
| | | 13 | | using MyNet.Observable.Collections.Sources; |
| | | 14 | | |
| | | 15 | | namespace 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 |
| | | 21 | | public 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 | | { |
| | | 32 | | var source = SourceEngine<T>.From(items, readOnly: false); |
| | | 33 | | |
| | | 34 | | 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 | | { |
| | | 46 | | var engine = SourceEngine<T>.FromObservable(source); |
| | | 47 | | |
| | | 48 | | 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 | | { |
| | | 59 | | var source = SourceEngine<T>.Empty(); |
| | | 60 | | |
| | | 61 | | 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 | | { |
| | | 73 | | var collection = new ExtendedCollection<T>(source, scheduler); |
| | | 74 | | |
| | | 75 | | 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 |
| | | 84 | | public 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 | | { |
| | 39 | 102 | | ArgumentNullException.ThrowIfNull(collection); |
| | | 103 | | |
| | 39 | 104 | | Collection = collection; |
| | 39 | 105 | | _selection = new(collection.Connect(), mode); |
| | 39 | 106 | | _disposeCollection = disposeCollection; |
| | 39 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the selection mode of the collection, indicating whether multiple items can be selected simultaneously or o |
| | | 111 | | /// </summary> |
| | 6 | 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> |
| | 0 | 122 | | public ReadOnlyObservableCollection<T> Items => Collection.Items; |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets the collection of selected items in the selectable collection. |
| | | 126 | | /// </summary> |
| | 27 | 127 | | public IEnumerable<T> SelectedItems => _selection.SelectedItems; |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the number of selected items in the selectable collection. |
| | | 131 | | /// </summary> |
| | 9 | 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> |
| | 21 | 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> |
| | 0 | 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> |
| | 0 | 150 | | public void Toggle(T item) => _selection.Toggle(item); |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Clears the selection in the selectable collection. |
| | | 154 | | /// </summary> |
| | 3 | 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> |
| | 0 | 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> |
| | 6 | 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 | | { |
| | 42 | 175 | | _selection.Dispose(); |
| | | 176 | | |
| | 42 | 177 | | if (_disposeCollection) |
| | 9 | 178 | | Collection.Dispose(); |
| | 42 | 179 | | } |
| | | 180 | | } |
| | | 181 | | |