| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SortingViewModel.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.ComponentModel; |
| | | 11 | | using System.Linq; |
| | | 12 | | using MyNet.Collections; |
| | | 13 | | using MyNet.Observable; |
| | | 14 | | using MyNet.Observable.Collections.Sorting; |
| | | 15 | | using MyNet.Utilities.Deferring; |
| | | 16 | | |
| | | 17 | | namespace MyNet.UI.ViewModels.List.Sorting; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Represents a view model for managing sorting configuration of a collection. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <typeparam name="T">The type of items in the collection.</typeparam> |
| | | 23 | | public class SortingViewModel<T> : ObservableObject, ISortingViewModel<T> |
| | | 24 | | { |
| | | 25 | | private readonly DeferredAction _deferredAction; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Creates a fluent builder used to configure and instantiate a <see cref="SortingViewModel{T}"/>. |
| | | 29 | | /// </summary> |
| | 0 | 30 | | public static SortingViewModelBuilder<T> CreateBuilder() => new(); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="SortingViewModel{T}"/> class with the specified sorting properties |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="properties">The sorting properties for the collection.</param> |
| | | 36 | | /// <param name="defaultSorting">The optional default sorting configuration.</param> |
| | 0 | 37 | | public SortingViewModel(IEnumerable<ISortingPropertyViewModel<T>> properties, IEnumerable<ISortingProperty<T>>? defa |
| | | 38 | | { |
| | 0 | 39 | | Properties = new(new(properties)); |
| | 0 | 40 | | DefaultSorting = defaultSorting?.ToList() ?? []; |
| | | 41 | | |
| | 0 | 42 | | _deferredAction = new(RaiseSortingChanged); |
| | | 43 | | |
| | 0 | 44 | | Reset(); |
| | | 45 | | |
| | 0 | 46 | | foreach (var property in Properties) |
| | 0 | 47 | | property.PropertyChanged += HandlePropertyChanged; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the collection of sorting property view models that can be configured for sorting. This collection is read- |
| | | 52 | | /// </summary> |
| | | 53 | | public ReadOnlyObservableCollection<ISortingPropertyViewModel<T>> Properties { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the current sorting configuration built from the UI. This collection is read-only and represents the active |
| | | 57 | | /// </summary> |
| | 0 | 58 | | public IReadOnlyList<ISortingProperty<T>> CurrentSorting { get; private set => SetProperty(ref field, value); } = [] |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the default sorting configuration for the collection. This collection is read-only and represents the initi |
| | | 62 | | /// </summary> |
| | | 63 | | public IReadOnlyList<ISortingProperty<T>> DefaultSorting { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets a value indicating whether there are any active sorting properties in the current configuration. This prope |
| | | 67 | | /// </summary> |
| | 0 | 68 | | public bool HasActiveSorting => CurrentSorting.Any(); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Occurs when the sorting configuration has changed. Subscribers can react to this event to apply the new sorting |
| | | 72 | | /// </summary> |
| | | 73 | | public event EventHandler<SortingChangedEventArgs<T>>? SortingChanged; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Applies the current sorting configuration. This method triggers the sorting update process by invoking the defer |
| | | 77 | | /// </summary> |
| | 0 | 78 | | public void Apply() => _deferredAction.Request(); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Clears all active sorting by disabling all sorting properties. This method iterates through the Properties colle |
| | | 82 | | /// </summary> |
| | | 83 | | public void Clear() |
| | | 84 | | { |
| | 0 | 85 | | using (_deferredAction.Defer()) |
| | 0 | 86 | | Properties.ForEach(p => p.IsEnabled = false); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Resets the sorting configuration to its default state. This method first clears all active sorting properties an |
| | | 91 | | /// </summary> |
| | | 92 | | public void Reset() |
| | | 93 | | { |
| | 0 | 94 | | using (_deferredAction.Defer()) |
| | | 95 | | { |
| | 0 | 96 | | Clear(); |
| | | 97 | | |
| | 0 | 98 | | foreach (var def in DefaultSorting) |
| | | 99 | | { |
| | 0 | 100 | | var vm = FindMatching(def); |
| | | 101 | | |
| | 0 | 102 | | if (vm != null) |
| | | 103 | | { |
| | 0 | 104 | | vm.IsEnabled = true; |
| | 0 | 105 | | vm.Direction = def.Direction; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Sets the active state of a sorting property identified by the specified key. This method finds the sorting prope |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="key">The key of the sorting property to update.</param> |
| | | 115 | | /// <param name="isActive">A value indicating whether the sorting property should be active.</param> |
| | | 116 | | public void SetActive(string key, bool isActive) |
| | | 117 | | { |
| | 0 | 118 | | var property = Find(key); |
| | | 119 | | |
| | 0 | 120 | | if (property is null) return; |
| | | 121 | | |
| | 0 | 122 | | using (_deferredAction.Defer()) |
| | 0 | 123 | | property.IsEnabled = isActive; |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Toggles the sort direction of a sorting property identified by the specified key. If the sorting property is cur |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="key">The key of the sorting property to toggle.</param> |
| | | 130 | | public void Toggle(string key) |
| | | 131 | | { |
| | 0 | 132 | | var property = Find(key); |
| | | 133 | | |
| | 0 | 134 | | if (property is null) return; |
| | | 135 | | |
| | 0 | 136 | | using (_deferredAction.Defer()) |
| | | 137 | | { |
| | 0 | 138 | | if (!property.IsEnabled) |
| | | 139 | | { |
| | 0 | 140 | | property.IsEnabled = true; |
| | 0 | 141 | | property.Direction = ListSortDirection.Ascending; |
| | | 142 | | } |
| | | 143 | | else |
| | | 144 | | { |
| | 0 | 145 | | property.Direction = property.Direction == ListSortDirection.Ascending |
| | 0 | 146 | | ? ListSortDirection.Descending |
| | 0 | 147 | | : ListSortDirection.Ascending; |
| | | 148 | | } |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Sets the sort direction of a sorting property identified by the specified key. This method finds the sorting pro |
| | | 154 | | /// </summary> |
| | | 155 | | /// <param name="key">The key of the sorting property to update.</param> |
| | | 156 | | /// <param name="direction">The direction to set for the sorting property.</param> |
| | | 157 | | public void SetDirection(string key, ListSortDirection direction) |
| | | 158 | | { |
| | 0 | 159 | | var property = Find(key); |
| | | 160 | | |
| | 0 | 161 | | if (property is null) return; |
| | | 162 | | |
| | 0 | 163 | | using (_deferredAction.Defer()) |
| | | 164 | | { |
| | 0 | 165 | | property.IsEnabled = true; |
| | 0 | 166 | | property.Direction = direction; |
| | 0 | 167 | | } |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Computes the current sorting configuration based on the enabled sorting properties in the Properties collection. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <returns>A read-only list of the current sorting properties.</returns> |
| | | 174 | | private IReadOnlyList<ISortingProperty<T>> ComputeCurrentSorting() => |
| | 0 | 175 | | [ |
| | 0 | 176 | | .. Properties |
| | 0 | 177 | | .Where(p => p.IsEnabled) |
| | 0 | 178 | | .OrderBy(p => p.ActivatedAt) |
| | 0 | 179 | | .Select(p => p.Build()) |
| | 0 | 180 | | ]; |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Handles the PropertyChanged event for the sorting property view models. When any property of a sorting property |
| | | 184 | | /// </summary> |
| | | 185 | | /// <param name="sender">The source of the event.</param> |
| | | 186 | | /// <param name="e">The event data.</param> |
| | 0 | 187 | | private void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e) => _deferredAction.Request(); |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Handles the logic for when the sorting configuration has changed. This method computes the current sorting confi |
| | | 191 | | /// </summary> |
| | | 192 | | private void RaiseSortingChanged() |
| | | 193 | | { |
| | 0 | 194 | | CurrentSorting = ComputeCurrentSorting(); |
| | 0 | 195 | | SortingChanged?.Invoke(this, new(CurrentSorting)); |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Finds a sorting property view model in the Properties collection by its unique key. This method searches through |
| | | 200 | | /// </summary> |
| | | 201 | | /// <param name="key">The unique key of the sorting property to find.</param> |
| | | 202 | | /// <returns>The sorting property view model with the specified key, or null if not found.</returns> |
| | | 203 | | private ISortingPropertyViewModel<T>? Find(string key) |
| | 0 | 204 | | => Properties.FirstOrDefault(p => p.Key == key); |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Finds a sorting property view model in the Properties collection that matches the specified sorting property. Th |
| | | 208 | | /// </summary> |
| | | 209 | | /// <param name="property">The sorting property to match.</param> |
| | | 210 | | /// <returns>The sorting property view model that matches the specified sorting property, or null if not found.</ret |
| | | 211 | | private ISortingPropertyViewModel<T>? FindMatching(ISortingProperty<T> property) |
| | 0 | 212 | | => Properties.FirstOrDefault(p => p.Matches(property)); |
| | | 213 | | |
| | | 214 | | /// <inheritdoc /> |
| | | 215 | | protected override void DisposeManagedResources() |
| | | 216 | | { |
| | 0 | 217 | | foreach (var property in Properties) |
| | 0 | 218 | | property.PropertyChanged -= HandlePropertyChanged; |
| | | 219 | | |
| | 0 | 220 | | base.DisposeManagedResources(); |
| | 0 | 221 | | } |
| | | 222 | | } |
| | | 223 | | |