| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SortingViewModelBuilder.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.Linq.Expressions; |
| | | 11 | | using MyNet.Observable.Collections.Sorting; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.List.Sorting; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Fluent builder used to create <see cref="SortingViewModel{T}"/> instances. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The type of the items to sort.</typeparam> |
| | | 19 | | public sealed class SortingViewModelBuilder<T> |
| | | 20 | | { |
| | 0 | 21 | | private readonly List<SortingPropertyViewModelBuilder<T>> _propertyBuilders = []; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates and immediately builds a sorting view model from a configuration action. |
| | | 25 | | /// </summary> |
| | | 26 | | public static SortingViewModel<T> Create(Action<SortingViewModelBuilder<T>> configure) |
| | | 27 | | { |
| | 0 | 28 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 29 | | |
| | 0 | 30 | | var builder = new SortingViewModelBuilder<T>(); |
| | 0 | 31 | | configure(builder); |
| | 0 | 32 | | return builder.Build(); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Adds a sorting property by using a dedicated property builder. |
| | | 37 | | /// </summary> |
| | | 38 | | public SortingViewModelBuilder<T> AddProperty(Expression<Func<T, object?>> expression, Action<SortingPropertyViewMod |
| | | 39 | | { |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 41 | | |
| | 0 | 42 | | var builder = new SortingPropertyViewModelBuilder<T>(expression); |
| | 0 | 43 | | configure(builder); |
| | 0 | 44 | | _propertyBuilders.Add(builder); |
| | 0 | 45 | | return this; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates the configured <see cref="SortingViewModel{T}"/>. |
| | | 50 | | /// </summary> |
| | | 51 | | public SortingViewModel<T> Build() |
| | | 52 | | { |
| | 0 | 53 | | var results = new List<SortingPropertyBuildResult>(); |
| | 0 | 54 | | var keys = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 55 | | |
| | 0 | 56 | | foreach (var def in _propertyBuilders.Select(builder => builder.Build())) |
| | | 57 | | { |
| | 0 | 58 | | if (!keys.Add(def.Key)) |
| | 0 | 59 | | throw new InvalidOperationException($"Duplicate sorting key '{def.Key}'."); |
| | | 60 | | |
| | 0 | 61 | | var vm = new SortingPropertyViewModel<T>( |
| | 0 | 62 | | def.Expression, |
| | 0 | 63 | | def.Key, |
| | 0 | 64 | | def.DisplayName); |
| | | 65 | | |
| | 0 | 66 | | ISortingProperty<T>? defaultSort = null; |
| | | 67 | | |
| | 0 | 68 | | if (def.DefaultState is not null) |
| | | 69 | | { |
| | 0 | 70 | | defaultSort = new ExpressionSortingProperty<T>( |
| | 0 | 71 | | def.Expression, |
| | 0 | 72 | | def.DefaultState.Direction); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | results.Add(new(vm, defaultSort)); |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | var defaultSorting = results |
| | 0 | 79 | | .Select(x => x.DefaultSorting) |
| | 0 | 80 | | .Where(x => x is not null) |
| | 0 | 81 | | .Cast<ISortingProperty<T>>() |
| | 0 | 82 | | .ToList(); |
| | | 83 | | |
| | 0 | 84 | | var viewModels = results.ConvertAll(x => x.ViewModel); |
| | | 85 | | |
| | 0 | 86 | | return new(viewModels, defaultSorting); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Represents the result of building a sorting property, containing both the view model and the default sorting con |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="ViewModel">The view model of the sorting property.</param> |
| | | 93 | | /// <param name="DefaultSorting">The default sorting configuration, if specified.</param> |
| | | 94 | | private sealed record SortingPropertyBuildResult(ISortingPropertyViewModel<T> ViewModel, ISortingProperty<T>? Defaul |
| | | 95 | | } |
| | | 96 | | |