| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GroupingViewModelBuilder.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.Grouping; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.List.Grouping; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Fluent builder used to create <see cref="GroupingViewModel{T}"/> instances. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The type of the items to group.</typeparam> |
| | | 19 | | public sealed class GroupingViewModelBuilder<T> |
| | | 20 | | { |
| | 0 | 21 | | private readonly List<GroupingPropertyViewModelBuilder<T>> _propertyBuilders = []; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates and immediately builds a grouping view model from a configuration action. |
| | | 25 | | /// </summary> |
| | | 26 | | public static GroupingViewModel<T> Create(Action<GroupingViewModelBuilder<T>> configure) |
| | | 27 | | { |
| | 0 | 28 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 29 | | |
| | 0 | 30 | | var builder = new GroupingViewModelBuilder<T>(); |
| | 0 | 31 | | configure(builder); |
| | 0 | 32 | | return builder.Build(); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Adds a grouping property by using a dedicated property builder. |
| | | 37 | | /// </summary> |
| | | 38 | | public GroupingViewModelBuilder<T> AddProperty(Expression<Func<T, object?>> expression, Action<GroupingPropertyViewM |
| | | 39 | | { |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 41 | | |
| | 0 | 42 | | var builder = new GroupingPropertyViewModelBuilder<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="GroupingViewModel{T}"/>. |
| | | 50 | | /// </summary> |
| | | 51 | | public GroupingViewModel<T> Build() |
| | | 52 | | { |
| | 0 | 53 | | var results = new List<GroupingPropertyBuildResult>(); |
| | 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 grouping key '{def.Key}'."); |
| | | 60 | | |
| | 0 | 61 | | var vm = new GroupingPropertyViewModel<T>( |
| | 0 | 62 | | def.Expression, |
| | 0 | 63 | | def.Key, |
| | 0 | 64 | | def.DisplayName); |
| | | 65 | | |
| | 0 | 66 | | IGroupingProperty<T>? defaultGroup = null; |
| | | 67 | | |
| | 0 | 68 | | if (def.DefaultState is not null) |
| | | 69 | | { |
| | 0 | 70 | | defaultGroup = new ExpressionGroupingProperty<T>(def.Expression); |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | results.Add(new(vm, defaultGroup)); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | var defaultGrouping = results |
| | 0 | 77 | | .Select(x => x.DefaultGrouping) |
| | 0 | 78 | | .Where(x => x is not null) |
| | 0 | 79 | | .Cast<IGroupingProperty<T>>() |
| | 0 | 80 | | .ToList(); |
| | | 81 | | |
| | 0 | 82 | | var viewModels = results.ConvertAll(x => x.ViewModel); |
| | | 83 | | |
| | 0 | 84 | | return new(viewModels, defaultGrouping); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Represents the result of building a grouping property, containing both the view model and the default grouping c |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="ViewModel">The view model of the grouping property.</param> |
| | | 91 | | /// <param name="DefaultGrouping">The default grouping configuration, if specified.</param> |
| | | 92 | | private sealed record GroupingPropertyBuildResult(IGroupingPropertyViewModel<T> ViewModel, IGroupingProperty<T>? Def |
| | | 93 | | } |
| | | 94 | | |