| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectionEngine.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.Linq; |
| | | 10 | | using System.Reactive.Disposables; |
| | | 11 | | using System.Reactive.Subjects; |
| | | 12 | | using DynamicData; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Observable.Collections.Selection; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Manages the selection state of a collection of items, enforcing selection rules based on the specified selection mod |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of items in the collection, which must implement INotifyPropertyChanged.</typeparam> |
| | | 20 | | public sealed class SelectionEngine<T> : IDisposable |
| | | 21 | | where T : notnull |
| | | 22 | | { |
| | 87 | 23 | | private readonly HashSet<T> _items = []; |
| | 87 | 24 | | private readonly HashSet<T> _selected = []; |
| | | 25 | | private readonly BehaviorSubject<IReadOnlyCollection<T>> _subject; |
| | 87 | 26 | | private readonly CompositeDisposable _disposables = []; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="SelectionEngine{T}"/> class with the specified source of items, sel |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="source">The source of items to be managed by the selection engine.</param> |
| | | 32 | | /// <param name="mode">The selection mode to be enforced by the selection engine (single or multiple).</param> |
| | | 33 | | public SelectionEngine(IObservable<IChangeSet<T>> source, SelectionMode mode) |
| | | 34 | | { |
| | 87 | 35 | | Mode = mode; |
| | 87 | 36 | | _subject = new([]); |
| | | 37 | | |
| | 87 | 38 | | _disposables.Add(source.Subscribe(ApplySourceChanges)); |
| | 87 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the selection mode enforced by the selection engine. This property indicates whether the selection engine a |
| | | 43 | | /// </summary> |
| | | 44 | | public SelectionMode Mode { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets a read-only collection of the currently selected items. The collection is automatically updated as the sele |
| | | 48 | | /// </summary> |
| | 72 | 49 | | public IReadOnlyCollection<T> SelectedItems => _selected; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the count of currently selected items. This property provides a quick way to determine how many items are c |
| | | 53 | | /// </summary> |
| | 9 | 54 | | public int Count => _selected.Count; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Connects to the selection engine and returns an observable sequence of the currently selected items. This allows |
| | | 58 | | /// </summary> |
| | | 59 | | /// <returns>An observable sequence of the currently selected items.</returns> |
| | 3 | 60 | | public IObservable<IReadOnlyCollection<T>> Connect() => _subject; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Selects the specified item according to the selection rules defined by the current selection mode. If the item i |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="item">The item to be selected.</param> |
| | | 66 | | public void Select(T item) |
| | | 67 | | { |
| | 57 | 68 | | if (!_items.Contains(item)) |
| | 3 | 69 | | return; |
| | | 70 | | |
| | 54 | 71 | | if (Mode == SelectionMode.Single) |
| | 24 | 72 | | _selected.Clear(); |
| | | 73 | | |
| | 54 | 74 | | _selected.Add(item); |
| | 54 | 75 | | Publish(); |
| | 54 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Deselects the specified item. If the item is not selectable, this method does nothing. This method provides a co |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="item">The item to be deselected.</param> |
| | | 82 | | public void Unselect(T item) |
| | | 83 | | { |
| | 6 | 84 | | _selected.Remove(item); |
| | 6 | 85 | | Publish(); |
| | 6 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Toggles the selection state of the specified item. If the item is not selectable, this method does nothing. This |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="item">The item whose selection state is to be toggled.</param> |
| | | 92 | | public void Toggle(T item) |
| | | 93 | | { |
| | 12 | 94 | | if (!_items.Contains(item)) |
| | 0 | 95 | | return; |
| | | 96 | | |
| | 12 | 97 | | if (Mode == SelectionMode.Single) |
| | | 98 | | { |
| | 6 | 99 | | if (_selected.Contains(item)) |
| | | 100 | | { |
| | 3 | 101 | | _selected.Clear(); |
| | | 102 | | } |
| | | 103 | | else |
| | | 104 | | { |
| | 3 | 105 | | _selected.Clear(); |
| | 3 | 106 | | _selected.Add(item); |
| | | 107 | | } |
| | | 108 | | |
| | 6 | 109 | | Publish(); |
| | 6 | 110 | | return; |
| | | 111 | | } |
| | | 112 | | |
| | 6 | 113 | | if (!_selected.Add(item)) |
| | 3 | 114 | | _selected.Remove(item); |
| | | 115 | | |
| | 6 | 116 | | Publish(); |
| | 6 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Clears all selections in the collection. This method deselects all currently selected items, regardless of the c |
| | | 121 | | /// </summary> |
| | | 122 | | public void Clear() |
| | | 123 | | { |
| | 6 | 124 | | _selected.Clear(); |
| | 6 | 125 | | Publish(); |
| | 6 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Sets the selection state of the specified items according to the selection rules defined by the current selectio |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="items">The items whose selection state is to be set.</param> |
| | | 132 | | public void Set(IEnumerable<T> items) |
| | | 133 | | { |
| | 18 | 134 | | _selected.Clear(); |
| | | 135 | | |
| | 18 | 136 | | if (Mode == SelectionMode.Single) |
| | | 137 | | { |
| | 3 | 138 | | var first = items.FirstOrDefault(_items.Contains); |
| | 3 | 139 | | if (first is not null) |
| | 3 | 140 | | _selected.Add(first); |
| | | 141 | | |
| | 3 | 142 | | Publish(); |
| | 3 | 143 | | return; |
| | | 144 | | } |
| | | 145 | | |
| | 96 | 146 | | foreach (var item in items) |
| | | 147 | | { |
| | 33 | 148 | | if (_items.Contains(item)) |
| | 30 | 149 | | _selected.Add(item); |
| | | 150 | | } |
| | | 151 | | |
| | 15 | 152 | | Publish(); |
| | 15 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Determines whether the specified item is currently selected in the collection. This method checks if the given i |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="item">The wrapper representing the item to check.</param> |
| | | 159 | | /// <returns>True if the item is selected; otherwise, false.</returns> |
| | 6 | 160 | | public bool Contains(T item) => _selected.Contains(item); |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Publishes the current selection state to the observers. This method is called whenever there is a change in the |
| | | 164 | | /// </summary> |
| | 102 | 165 | | private void Publish() => _subject.OnNext([.. _selected]); |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Rebuilds the selection state based on the current items in the collection. This method is called when there are |
| | | 169 | | /// </summary> |
| | | 170 | | private void ApplySourceChanges(IChangeSet<T> changes) |
| | | 171 | | { |
| | 234 | 172 | | var selectionChanged = false; |
| | | 173 | | |
| | 960 | 174 | | foreach (var change in changes) |
| | | 175 | | { |
| | 246 | 176 | | switch (change.Reason) |
| | | 177 | | { |
| | | 178 | | case ListChangeReason.Add: |
| | 21 | 179 | | _items.Add(change.Item.Current); |
| | 21 | 180 | | break; |
| | | 181 | | case ListChangeReason.AddRange: |
| | 102504 | 182 | | foreach (var item in change.Range) |
| | 51111 | 183 | | _items.Add(item); |
| | | 184 | | |
| | | 185 | | break; |
| | | 186 | | case ListChangeReason.Remove: |
| | 3 | 187 | | _items.Remove(change.Item.Current); |
| | 3 | 188 | | selectionChanged |= _selected.Remove(change.Item.Current); |
| | 3 | 189 | | break; |
| | | 190 | | case ListChangeReason.RemoveRange: |
| | 18 | 191 | | foreach (var item in change.Range) |
| | | 192 | | { |
| | 6 | 193 | | _items.Remove(item); |
| | 6 | 194 | | selectionChanged |= _selected.Remove(item); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | break; |
| | | 198 | | case ListChangeReason.Replace: |
| | 0 | 199 | | if (change.Item.Previous.HasValue) |
| | | 200 | | { |
| | 0 | 201 | | var previous = change.Item.Previous.Value; |
| | 0 | 202 | | _items.Remove(previous); |
| | 0 | 203 | | selectionChanged |= _selected.Remove(previous); |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | _items.Add(change.Item.Current); |
| | 0 | 207 | | break; |
| | | 208 | | case ListChangeReason.Clear: |
| | 78 | 209 | | _items.Clear(); |
| | 78 | 210 | | if (_selected.Count > 0) |
| | | 211 | | { |
| | 3 | 212 | | _selected.Clear(); |
| | 3 | 213 | | selectionChanged = true; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | break; |
| | | 217 | | case ListChangeReason.Moved: |
| | | 218 | | case ListChangeReason.Refresh: |
| | | 219 | | default: |
| | | 220 | | break; |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | 234 | 224 | | if (Mode == SelectionMode.Single && _selected.Count > 1) |
| | | 225 | | { |
| | 0 | 226 | | var first = _selected.First(); |
| | 0 | 227 | | _selected.Clear(); |
| | 0 | 228 | | _selected.Add(first); |
| | 0 | 229 | | selectionChanged = true; |
| | | 230 | | } |
| | | 231 | | |
| | 234 | 232 | | if (selectionChanged) |
| | 6 | 233 | | Publish(); |
| | 234 | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Disposes of the resources used by the selection engine. This method should be called when the selection engine i |
| | | 238 | | /// </summary> |
| | | 239 | | public void Dispose() |
| | | 240 | | { |
| | 42 | 241 | | _subject.Dispose(); |
| | 42 | 242 | | _disposables.Dispose(); |
| | 42 | 243 | | } |
| | | 244 | | } |
| | | 245 | | |