| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectableListViewModel.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.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Reactive.Concurrency; |
| | | 12 | | using MyNet.Observable.Collections; |
| | | 13 | | using MyNet.Observable.Collections.Selection; |
| | | 14 | | using MyNet.UI.ViewModels.List.Factories; |
| | | 15 | | using MyNet.UI.ViewModels.List.Filtering; |
| | | 16 | | using MyNet.UI.ViewModels.List.Grouping; |
| | | 17 | | using MyNet.UI.ViewModels.List.Paging; |
| | | 18 | | using MyNet.UI.ViewModels.List.Sorting; |
| | | 19 | | |
| | | 20 | | namespace 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> |
| | | 26 | | public 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) |
| | 27 | 40 | | : this( |
| | 27 | 41 | | collection, |
| | 27 | 42 | | new SelectableCollectionSelectionManager<T>(collection, mode), |
| | 27 | 43 | | filters, |
| | 27 | 44 | | sorting, |
| | 27 | 45 | | grouping, |
| | 27 | 46 | | paging, |
| | 27 | 47 | | scheduler) |
| | | 48 | | { |
| | 27 | 49 | | } |
| | | 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) |
| | 27 | 58 | | : this(collection, mode, options?.Filters, options?.Sorting, options?.Grouping, options?.Paging, options?.Schedu |
| | | 59 | | { |
| | 27 | 60 | | } |
| | | 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) |
| | 57 | 73 | | : base(collection, filters, sorting, grouping, paging, scheduler) |
| | | 74 | | { |
| | 57 | 75 | | _selection = selectionManager ?? throw new ArgumentNullException(nameof(selectionManager)); |
| | 57 | 76 | | _selection.SelectionChanged += HandleSelectionChanged; |
| | 57 | 77 | | } |
| | | 78 | | |
| | | 79 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed in Cleanup.")] |
| | | 80 | | private readonly ISelectionManager<T> _selection; |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | 6 | 83 | | public SelectionMode SelectionMode => _selection.Mode; |
| | | 84 | | |
| | | 85 | | /// <inheritdoc /> |
| | 24 | 86 | | public IReadOnlyList<T> SelectedItems => _selection.SelectedItems; |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | 9 | 89 | | public int SelectedCount => _selection.SelectedCount; |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | public T? SelectedItem |
| | | 93 | | { |
| | 18 | 94 | | get => SelectedItems.FirstOrDefault(); |
| | | 95 | | set |
| | | 96 | | { |
| | 9 | 97 | | if (EqualityComparer<T>.Default.Equals(SelectedItem, value)) |
| | 0 | 98 | | return; |
| | | 99 | | |
| | 9 | 100 | | if (value is null) |
| | | 101 | | { |
| | 3 | 102 | | ClearSelection(); |
| | 3 | 103 | | return; |
| | | 104 | | } |
| | | 105 | | |
| | 6 | 106 | | if (SelectionMode == SelectionMode.Single) |
| | 6 | 107 | | Select(value); |
| | | 108 | | else |
| | 0 | 109 | | SetSelection([value]); |
| | 0 | 110 | | } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <inheritdoc /> |
| | 9 | 114 | | public bool IsSelected(T item) => _selection.IsSelected(item); |
| | | 115 | | |
| | | 116 | | /// <inheritdoc /> |
| | 18 | 117 | | public void Select(T item) => _selection.Select(item); |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | 3 | 120 | | public void Unselect(T item) => _selection.Unselect(item); |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | 3 | 123 | | public void Toggle(T item) => _selection.Toggle(item); |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | 6 | 126 | | public void ClearSelection() => _selection.ClearSelection(); |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | 3 | 129 | | public void SetSelection(IEnumerable<T> items) => _selection.SetSelection(items); |
| | | 130 | | |
| | | 131 | | private void HandleSelectionChanged(object? sender, EventArgs e) |
| | | 132 | | { |
| | 21 | 133 | | NotifyPropertyChanged(nameof(SelectedItems)); |
| | 21 | 134 | | NotifyPropertyChanged(nameof(SelectedCount)); |
| | 21 | 135 | | NotifyPropertyChanged(nameof(SelectedItem)); |
| | 21 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc /> |
| | | 139 | | protected override void DisposeManagedResources() |
| | | 140 | | { |
| | 57 | 141 | | _selection.SelectionChanged -= HandleSelectionChanged; |
| | 57 | 142 | | _selection.Dispose(); |
| | 57 | 143 | | base.DisposeManagedResources(); |
| | 57 | 144 | | } |
| | | 145 | | } |
| | | 146 | | |