| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ListViewModelBase.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 System.Reactive.Linq; |
| | | 13 | | using MyNet.Observable.Collections; |
| | | 14 | | using MyNet.Observable.Collections.Filters; |
| | | 15 | | using MyNet.Observable.Collections.Grouping; |
| | | 16 | | using MyNet.Observable.Collections.Sorting; |
| | | 17 | | using MyNet.UI.ViewModels.List.Factories; |
| | | 18 | | using MyNet.UI.ViewModels.List.Filtering; |
| | | 19 | | using MyNet.UI.ViewModels.List.Grouping; |
| | | 20 | | using MyNet.UI.ViewModels.List.Paging; |
| | | 21 | | using MyNet.UI.ViewModels.List.Sorting; |
| | | 22 | | using MyNet.Utilities.Deferring; |
| | | 23 | | |
| | | 24 | | namespace MyNet.UI.ViewModels.List; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Provides a lightweight list pipeline implementation for ViewModels. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 30 | | public class ListViewModelBase<T> : ViewModelBase, IListViewModel<T> |
| | | 31 | | where T : notnull |
| | | 32 | | { |
| | 132 | 33 | | private readonly ObservableCollection<IGroup<T>> _groups = []; |
| | | 34 | | private readonly DeferredAction _pipelineRefreshDeferrer; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="ListViewModelBase{T}"/> class. |
| | | 38 | | /// </summary> |
| | | 39 | | protected ListViewModelBase(IListDataProvider<T> dataProvider, ListViewModelOptions<T>? options) |
| | 0 | 40 | | : this(dataProvider, options?.Filters, options?.Sorting, options?.Grouping, options?.Paging, options?.Scheduler) |
| | | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="ListViewModelBase{T}"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="dataProvider">The pipeline data provider.</param> |
| | | 48 | | /// <param name="filters">Optional filtering configuration.</param> |
| | | 49 | | /// <param name="sorting">Optional sorting configuration.</param> |
| | | 50 | | /// <param name="grouping">Optional grouping configuration.</param> |
| | | 51 | | /// <param name="paging">Optional paging configuration.</param> |
| | | 52 | | /// <param name="scheduler">Optional scheduler for managing asynchronous operations.</param> |
| | 132 | 53 | | protected ListViewModelBase( |
| | 132 | 54 | | IListDataProvider<T> dataProvider, |
| | 132 | 55 | | IFiltersViewModel<T>? filters = null, |
| | 132 | 56 | | ISortingViewModel<T>? sorting = null, |
| | 132 | 57 | | IGroupingViewModel<T>? grouping = null, |
| | 132 | 58 | | IPagingViewModel? paging = null, |
| | 132 | 59 | | IScheduler? scheduler = null) |
| | | 60 | | { |
| | 132 | 61 | | ArgumentNullException.ThrowIfNull(dataProvider); |
| | | 62 | | |
| | 132 | 63 | | var scheduler1 = scheduler ?? Scheduler.Default; |
| | 132 | 64 | | DataProvider = dataProvider; |
| | 132 | 65 | | _pipelineRefreshDeferrer = new(RefreshPipeline); |
| | | 66 | | |
| | 132 | 67 | | Source = DataProvider.Source; |
| | 132 | 68 | | FilteredItems = DataProvider.FilteredItems; |
| | 132 | 69 | | Items = DataProvider.Items; |
| | 132 | 70 | | Groups = new(_groups); |
| | | 71 | | |
| | 132 | 72 | | Filters = filters; |
| | 132 | 73 | | Sorting = sorting; |
| | 132 | 74 | | Grouping = grouping; |
| | 132 | 75 | | Paging = paging; |
| | | 76 | | |
| | 132 | 77 | | CurrentFilter = filters?.CurrentFilter; |
| | 132 | 78 | | CurrentSorting = sorting?.CurrentSorting ?? []; |
| | 132 | 79 | | CurrentGrouping = grouping?.CurrentGrouping ?? []; |
| | | 80 | | |
| | 132 | 81 | | RefreshPipeline(); |
| | 132 | 82 | | SubscribeToConfigurationEvents(); |
| | | 83 | | |
| | 132 | 84 | | Disposables.Add( |
| | 132 | 85 | | DataProvider.ConnectGroups() |
| | 132 | 86 | | .ObserveOn(scheduler1) |
| | 132 | 87 | | .Subscribe(groups => |
| | 132 | 88 | | { |
| | 132 | 89 | | _groups.Clear(); |
| | 132 | 90 | | foreach (var g in groups) |
| | 132 | 91 | | _groups.Add(new Group<T>(g.Key, [.. g.Items])); |
| | 132 | 92 | | })); |
| | | 93 | | |
| | 132 | 94 | | if (Paging is not null) |
| | | 95 | | { |
| | 12 | 96 | | Disposables.Add( |
| | 12 | 97 | | DataProvider.ConnectFiltered() |
| | 12 | 98 | | .ObserveOn(scheduler1) |
| | 12 | 99 | | .Subscribe(_ => SyncPagingMetadata())); |
| | | 100 | | } |
| | 132 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the underlying list data provider. |
| | | 105 | | /// </summary> |
| | | 106 | | protected IListDataProvider<T> DataProvider { get; } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc /> |
| | | 109 | | public ReadOnlyObservableCollection<T> Source { get; } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public ReadOnlyObservableCollection<T> FilteredItems { get; } |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | | 115 | | public ReadOnlyObservableCollection<T> Items { get; } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | | 118 | | public ReadOnlyObservableCollection<IGroup<T>>? Groups { get; } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc /> |
| | 3 | 121 | | public int TotalCount => Source.Count; |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | 6 | 124 | | public int FilteredCount => DataProvider.FilteredCount; |
| | | 125 | | |
| | | 126 | | /// <inheritdoc /> |
| | | 127 | | public IFiltersViewModel<T>? Filters { get; } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc /> |
| | 192 | 130 | | public IFilter<T>? CurrentFilter { get; private set => SetProperty(ref field, value); } |
| | | 131 | | |
| | | 132 | | /// <inheritdoc /> |
| | | 133 | | public ISortingViewModel<T>? Sorting { get; } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc /> |
| | 309 | 136 | | public IReadOnlyList<ISortingProperty<T>> CurrentSorting { get; private set => SetProperty(ref field, value); } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc /> |
| | | 139 | | public IGroupingViewModel<T>? Grouping { get; } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc /> |
| | 300 | 142 | | public IReadOnlyList<IGroupingProperty<T>> CurrentGrouping { get; private set => SetProperty(ref field, value); } |
| | | 143 | | |
| | | 144 | | /// <inheritdoc /> |
| | | 145 | | public IPagingViewModel? Paging { get; } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Creates a deferral scope that suspends pipeline refreshes until the scope is disposed. |
| | | 149 | | /// Multiple configuration changes (filter, sorting, grouping) occurring within a |
| | | 150 | | /// single scope will trigger exactly ONE pipeline refresh when the scope ends. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <returns>An <see cref="IDisposable"/> scope; dispose it to flush the deferred refresh.</returns> |
| | | 153 | | /// <example> |
| | | 154 | | /// <code> |
| | | 155 | | /// using (vm.DeferRefresh()) |
| | | 156 | | /// { |
| | | 157 | | /// vm.Filters.Apply(...); |
| | | 158 | | /// vm.Sorting.Apply(...); |
| | | 159 | | /// vm.Grouping.Apply(...); |
| | | 160 | | /// } // ← single pipeline refresh here |
| | | 161 | | /// </code> |
| | | 162 | | /// </example> |
| | 12 | 163 | | public IDisposable DeferRefresh() => _pipelineRefreshDeferrer.Defer(); |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Refreshes the collection pipeline by applying the current filter, sorting, grouping, and paging window. |
| | | 167 | | /// </summary> |
| | | 168 | | private void RefreshPipeline() |
| | | 169 | | { |
| | 168 | 170 | | ApplyFilter(); |
| | 168 | 171 | | ApplySorting(); |
| | 168 | 172 | | ApplyGrouping(); |
| | 168 | 173 | | ApplyPaging(); |
| | 168 | 174 | | SyncPagingMetadata(); |
| | | 175 | | |
| | 168 | 176 | | if (Paging is not null) |
| | 24 | 177 | | ApplyPaging(); |
| | 168 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Requests a pipeline refresh using the same debounce/deferral mechanism as configuration changes. |
| | | 182 | | /// Derived classes can use this hook when external actions (for example CRUD operations) |
| | | 183 | | /// require re-applying the pipeline. |
| | | 184 | | /// </summary> |
| | 9 | 185 | | protected void RequestPipelineRefresh() => _pipelineRefreshDeferrer.Request(); |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Applies the current filter from the Filters view model to the collection. |
| | | 189 | | /// When no <see cref="Filters"/> view model is configured, the data provider filter is left unchanged |
| | | 190 | | /// so derived classes can manage ad-hoc filters through <see cref="IListDataProvider{T}.SetFilter"/>. |
| | | 191 | | /// </summary> |
| | | 192 | | private void ApplyFilter() |
| | | 193 | | { |
| | 168 | 194 | | if (Filters is null) |
| | 129 | 195 | | return; |
| | | 196 | | |
| | 39 | 197 | | CurrentFilter = Filters.CurrentFilter; |
| | | 198 | | |
| | 39 | 199 | | if (CurrentFilter is null) |
| | 36 | 200 | | DataProvider.ClearFilter(); |
| | | 201 | | else |
| | 3 | 202 | | DataProvider.SetFilter(CurrentFilter); |
| | 3 | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Applies the current sorting from the Sorting view model to the collection. |
| | | 207 | | /// </summary> |
| | | 208 | | private void ApplySorting() |
| | | 209 | | { |
| | 168 | 210 | | CurrentSorting = Sorting?.CurrentSorting ?? []; |
| | | 211 | | |
| | 168 | 212 | | if (CurrentSorting.Count == 0) |
| | 168 | 213 | | DataProvider.ClearSorting(); |
| | | 214 | | else |
| | 0 | 215 | | DataProvider.SetSorting([.. CurrentSorting]); |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Applies the current grouping from the Grouping view model to the collection. |
| | | 220 | | /// </summary> |
| | | 221 | | private void ApplyGrouping() |
| | | 222 | | { |
| | 168 | 223 | | CurrentGrouping = Grouping?.CurrentGrouping ?? []; |
| | | 224 | | |
| | 168 | 225 | | if (CurrentGrouping.Count == 0) |
| | 168 | 226 | | DataProvider.ClearGrouping(); |
| | | 227 | | else |
| | 0 | 228 | | DataProvider.SetGrouping([.. CurrentGrouping]); |
| | 0 | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Applies the current paging window to the collection. |
| | | 233 | | /// </summary> |
| | | 234 | | private void ApplyPaging() |
| | | 235 | | { |
| | 195 | 236 | | if (Paging is null) |
| | | 237 | | { |
| | 144 | 238 | | DataProvider.ClearPaging(); |
| | 144 | 239 | | return; |
| | | 240 | | } |
| | | 241 | | |
| | 51 | 242 | | DataProvider.SetPaging(Paging.CurrentPage, Paging.PageSize); |
| | 51 | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Updates paging metadata based on the current filtered count. |
| | | 247 | | /// </summary> |
| | | 248 | | private void SyncPagingMetadata() |
| | | 249 | | { |
| | 192 | 250 | | if (Paging is null) |
| | 144 | 251 | | return; |
| | | 252 | | |
| | 48 | 253 | | var previousPage = Paging.CurrentPage; |
| | 48 | 254 | | var currentPage = Math.Max(1, Paging.CurrentPage); |
| | 48 | 255 | | Paging.Update(DataProvider.FilteredCount, currentPage); |
| | | 256 | | |
| | 48 | 257 | | if (Paging.CurrentPage != previousPage) |
| | 3 | 258 | | ApplyPaging(); |
| | 48 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Handles changes in the filtering configuration. |
| | | 263 | | /// </summary> |
| | | 264 | | private void HandleFiltersChanged(object? sender, FiltersChangedEventArgs<T> e) |
| | | 265 | | { |
| | 21 | 266 | | CurrentFilter = e.Filter; |
| | 21 | 267 | | _pipelineRefreshDeferrer.Request(); |
| | 21 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Handles changes in the sorting configuration. |
| | | 272 | | /// </summary> |
| | | 273 | | private void HandleSortingChanged(object? sender, SortingChangedEventArgs<T> e) |
| | | 274 | | { |
| | 9 | 275 | | CurrentSorting = e.Sorting; |
| | 9 | 276 | | _pipelineRefreshDeferrer.Request(); |
| | 9 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Handles changes in the grouping configuration. |
| | | 281 | | /// </summary> |
| | | 282 | | private void HandleGroupingChanged(object? sender, GroupingChangedEventArgs<T> e) |
| | | 283 | | { |
| | 0 | 284 | | CurrentGrouping = e.Grouping; |
| | 0 | 285 | | _pipelineRefreshDeferrer.Request(); |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Handles changes in the paging configuration. |
| | | 290 | | /// </summary> |
| | 6 | 291 | | private void HandlePagingChanged(object? sender, PagingChangedEventArgs e) => _pipelineRefreshDeferrer.Request(); |
| | | 292 | | |
| | | 293 | | /// <summary> |
| | | 294 | | /// Subscribes to configuration events. |
| | | 295 | | /// </summary> |
| | | 296 | | private void SubscribeToConfigurationEvents() |
| | | 297 | | { |
| | 132 | 298 | | Filters?.FiltersChanged += HandleFiltersChanged; |
| | 132 | 299 | | Sorting?.SortingChanged += HandleSortingChanged; |
| | 132 | 300 | | Grouping?.GroupingChanged += HandleGroupingChanged; |
| | 132 | 301 | | Paging?.PagingChanged += HandlePagingChanged; |
| | 12 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Unsubscribes from configuration events. |
| | | 306 | | /// </summary> |
| | | 307 | | private void UnsubscribeFromConfigurationEvents() |
| | | 308 | | { |
| | 111 | 309 | | Filters?.FiltersChanged -= HandleFiltersChanged; |
| | 111 | 310 | | Sorting?.SortingChanged -= HandleSortingChanged; |
| | 111 | 311 | | Grouping?.GroupingChanged -= HandleGroupingChanged; |
| | 111 | 312 | | Paging?.PagingChanged -= HandlePagingChanged; |
| | 12 | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <inheritdoc /> |
| | | 316 | | protected override void DisposeManagedResources() |
| | | 317 | | { |
| | 111 | 318 | | UnsubscribeFromConfigurationEvents(); |
| | 111 | 319 | | DataProvider.Dispose(); |
| | 111 | 320 | | base.DisposeManagedResources(); |
| | 111 | 321 | | } |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// List view model base that exposes the underlying <see cref="ExtendedCollection{T}"/> for advanced scenarios (e.g. se |
| | | 326 | | /// </summary> |
| | | 327 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 328 | | /// <typeparam name="TCollection">The collection type.</typeparam> |
| | | 329 | | public class ListViewModelBase<T, TCollection> : ListViewModelBase<T> |
| | | 330 | | where TCollection : ExtendedCollection<T> |
| | | 331 | | where T : notnull |
| | | 332 | | { |
| | | 333 | | /// <summary> |
| | | 334 | | /// Initializes a new instance of the <see cref="ListViewModelBase{T, TCollection}"/> class. |
| | | 335 | | /// </summary> |
| | | 336 | | protected ListViewModelBase(TCollection collection, ListViewModelOptions<T>? options) |
| | | 337 | | : this(collection, options?.Filters, options?.Sorting, options?.Grouping, options?.Paging, options?.Scheduler) |
| | | 338 | | { |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Initializes a new instance of the <see cref="ListViewModelBase{T, TCollection}"/> class. |
| | | 343 | | /// </summary> |
| | | 344 | | protected ListViewModelBase( |
| | | 345 | | TCollection collection, |
| | | 346 | | IFiltersViewModel<T>? filters = null, |
| | | 347 | | ISortingViewModel<T>? sorting = null, |
| | | 348 | | IGroupingViewModel<T>? grouping = null, |
| | | 349 | | IPagingViewModel? paging = null, |
| | | 350 | | IScheduler? scheduler = null) |
| | | 351 | | : base(new ExtendedCollectionDataProvider<T>(collection), filters, sorting, grouping, paging, scheduler) |
| | | 352 | | => Collection = collection; |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// Gets the underlying extended collection used by the list pipeline. |
| | | 356 | | /// </summary> |
| | | 357 | | [field: SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed by the sha |
| | | 358 | | protected TCollection Collection { get; } |
| | | 359 | | } |
| | | 360 | | |