< Summary

Information
Class: MyNet.UI.ViewModels.List.Sorting.SortingViewModelBuilder<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Sorting/SortingViewModelBuilder.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 96
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%210%
Create(...)100%210%
AddProperty(...)100%210%
Build()0%210140%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Sorting/SortingViewModelBuilder.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SortingViewModelBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using System.Linq.Expressions;
 11using MyNet.Observable.Collections.Sorting;
 12
 13namespace 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>
 19public sealed class SortingViewModelBuilder<T>
 20{
 021    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    {
 028        ArgumentNullException.ThrowIfNull(configure);
 29
 030        var builder = new SortingViewModelBuilder<T>();
 031        configure(builder);
 032        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    {
 040        ArgumentNullException.ThrowIfNull(configure);
 41
 042        var builder = new SortingPropertyViewModelBuilder<T>(expression);
 043        configure(builder);
 044        _propertyBuilders.Add(builder);
 045        return this;
 46    }
 47
 48    /// <summary>
 49    /// Creates the configured <see cref="SortingViewModel{T}"/>.
 50    /// </summary>
 51    public SortingViewModel<T> Build()
 52    {
 053        var results = new List<SortingPropertyBuildResult>();
 054        var keys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
 55
 056        foreach (var def in _propertyBuilders.Select(builder => builder.Build()))
 57        {
 058            if (!keys.Add(def.Key))
 059                throw new InvalidOperationException($"Duplicate sorting key '{def.Key}'.");
 60
 061            var vm = new SortingPropertyViewModel<T>(
 062                def.Expression,
 063                def.Key,
 064                def.DisplayName);
 65
 066            ISortingProperty<T>? defaultSort = null;
 67
 068            if (def.DefaultState is not null)
 69            {
 070                defaultSort = new ExpressionSortingProperty<T>(
 071                    def.Expression,
 072                    def.DefaultState.Direction);
 73            }
 74
 075            results.Add(new(vm, defaultSort));
 76        }
 77
 078        var defaultSorting = results
 079            .Select(x => x.DefaultSorting)
 080            .Where(x => x is not null)
 081            .Cast<ISortingProperty<T>>()
 082            .ToList();
 83
 084        var viewModels = results.ConvertAll(x => x.ViewModel);
 85
 086        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