| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectableCollectionSelectionManager.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 MyNet.Observable.Collections; |
| | | 11 | | using MyNet.Observable.Collections.Selection; |
| | | 12 | | |
| | | 13 | | namespace 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> |
| | | 19 | | public 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 | | { |
| | 27 | 34 | | ArgumentNullException.ThrowIfNull(collection); |
| | | 35 | | |
| | 27 | 36 | | _selection = new(collection, mode, disposeCollection: false); |
| | 27 | 37 | | _ownsSelectable = true; |
| | 27 | 38 | | } |
| | | 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 | | { |
| | 0 | 49 | | _selection = selectable ?? throw new ArgumentNullException(nameof(selectable)); |
| | 0 | 50 | | _ownsSelectable = disposeSelectable; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public event EventHandler? SelectionChanged; |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | 6 | 57 | | public SelectionMode Mode => _selection.Mode; |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | 21 | 60 | | public IReadOnlyList<T> SelectedItems => [.. _selection.SelectedItems]; |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | 6 | 63 | | public int SelectedCount => _selection.SelectedCount; |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | 6 | 66 | | public bool IsSelected(T item) => _selection.IsSelected(item); |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public void Select(T item) |
| | | 70 | | { |
| | 15 | 71 | | _selection.Select(item); |
| | 15 | 72 | | SelectionChanged?.Invoke(this, EventArgs.Empty); |
| | 15 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public void Unselect(T item) |
| | | 77 | | { |
| | 0 | 78 | | _selection.Unselect(item); |
| | 0 | 79 | | SelectionChanged?.Invoke(this, EventArgs.Empty); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public void Toggle(T item) |
| | | 84 | | { |
| | 0 | 85 | | _selection.Toggle(item); |
| | 0 | 86 | | SelectionChanged?.Invoke(this, EventArgs.Empty); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | | 90 | | public void ClearSelection() |
| | | 91 | | { |
| | 3 | 92 | | if (!_selection.SelectedItems.Any()) |
| | 0 | 93 | | return; |
| | | 94 | | |
| | 3 | 95 | | _selection.ClearSelection(); |
| | 3 | 96 | | SelectionChanged?.Invoke(this, EventArgs.Empty); |
| | 3 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | public void SetSelection(IEnumerable<T> items) |
| | | 101 | | { |
| | 0 | 102 | | _selection.SetSelection(items); |
| | 0 | 103 | | SelectionChanged?.Invoke(this, EventArgs.Empty); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc /> |
| | | 107 | | public void Dispose() |
| | | 108 | | { |
| | 27 | 109 | | if (_ownsSelectable) |
| | 27 | 110 | | _selection.Dispose(); |
| | 27 | 111 | | } |
| | | 112 | | } |
| | | 113 | | |