< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WithKey(...)100%210%
WithDisplayName(...)100%210%
WithDisplayName(...)100%210%
Ascending(...)0%620%
Descending(...)0%620%
Build()0%620%
ResolveKey()0%2040%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SortingPropertyViewModelBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.ComponentModel;
 9using System.Linq.Expressions;
 10using MyNet.Observable;
 11using MyNet.Primitives;
 12using MyNet.UI.ViewModels.List.Grouping;
 13
 14namespace MyNet.UI.ViewModels.List.Sorting;
 15
 16/// <summary>
 17/// Fluent builder used to configure a sorting property view model.
 18/// </summary>
 19/// <typeparam name="T">The type of the items to sort.</typeparam>
 20public sealed class SortingPropertyViewModelBuilder<T>(Expression<Func<T, object?>> expression)
 21{
 22    private string? _key;
 23    private IObservableValue<string>? _displayName;
 24    private SortingDefaultState? _defaultState;
 25    private int _order;
 26
 27    /// <summary>
 28    /// Sets the unique key of the sorting property.
 29    /// </summary>
 30    public SortingPropertyViewModelBuilder<T> WithKey(string key)
 31    {
 032        _key = key;
 033        return this;
 34    }
 35
 36    /// <summary>
 37    /// Sets the display name provider used by the UI.
 38    /// </summary>
 39    public SortingPropertyViewModelBuilder<T> WithDisplayName(IObservableValue<string> displayName)
 40    {
 041        _displayName = displayName;
 042        return this;
 43    }
 44
 45    /// <summary>
 46    /// Sets the display name using a resource key.
 47    /// </summary>
 048    public SortingPropertyViewModelBuilder<T> WithDisplayName(string displayName) => WithDisplayName(new LocalizedString
 49
 50    /// <summary>
 51    /// Sets the default sorting direction to ascending. This means that when this sorting property is applied, it will 
 52    /// </summary>
 53    /// <param name="order">The order in which this sorting property should be applied relative to other sorting propert
 54    /// <returns>The current instance of <see cref="SortingPropertyViewModelBuilder{T}"/> for fluent configuration.</ret
 55    public SortingPropertyViewModelBuilder<T> Ascending(int? order = null)
 56    {
 057        _defaultState = new(ListSortDirection.Ascending, order ?? _order++);
 058        return this;
 59    }
 60
 61    /// <summary>
 62    /// Sets the default sorting direction to descending. This means that when this sorting property is applied, it will
 63    /// </summary>
 64    /// <param name="order">The order in which this sorting property should be applied relative to other sorting propert
 65    /// <returns>The current instance of <see cref="SortingPropertyViewModelBuilder{T}"/> for fluent configuration.</ret
 66    public SortingPropertyViewModelBuilder<T> Descending(int? order = null)
 67    {
 068        _defaultState = new(ListSortDirection.Descending, order ?? _order++);
 069        return this;
 70    }
 71
 72    /// <summary>
 73    /// Builds the sorting property configuration based on the current state of the builder. This method validates that 
 74    /// </summary>
 75    /// <returns>The constructed <see cref="GroupingPropertyDefinition{T}"/> instance.</returns>
 76    /// <exception cref="InvalidOperationException">Thrown if a sorting property key has not been provided.</exception>
 77    internal SortingPropertyDefinition<T> Build()
 78    {
 079        var key = ResolveKey();
 080        var displayName = _displayName ?? new LocalizedString(key);
 81
 082        return new(key, expression, displayName, _defaultState);
 83    }
 84
 85    /// <summary>
 86    /// Resolves the key for the sorting property. If a key has been explicitly set using the builder, it returns that k
 87    /// </summary>
 88    /// <returns>The resolved key for the sorting property.</returns>
 89    /// <exception cref="InvalidOperationException">Thrown if a sorting property key cannot be resolved.</exception>
 90    private string ResolveKey()
 91    {
 092        if (!string.IsNullOrWhiteSpace(_key))
 093            return _key;
 94
 095        var inferredKey = expression.PropertyName();
 96
 097        return string.IsNullOrWhiteSpace(inferredKey) ? throw new InvalidOperationException("A sorting property key must
 98    }
 99}
 100
 101/// <summary>
 102/// Represents the configuration for a sorting property, including its key, expression, display name, and default sortin
 103/// </summary>
 104/// <param name="Key">The unique key of the sorting property.</param>
 105/// <param name="Expression">The expression used to extract the value for sorting.</param>
 106/// <param name="DisplayName">The display name provider for the UI.</param>
 107/// <param name="DefaultState">The default configuration for the sorting property, if any.</param>
 108/// <typeparam name="T">The type of the items being sorted.</typeparam>
 109internal sealed record SortingPropertyDefinition<T>(string Key, Expression<Func<T, object?>> Expression, IObservableValu
 110
 111/// <summary>
 112/// Represents the active configuration for a sorting property, including its default sorting direction and the order in
 113/// </summary>
 114/// <param name="Direction">The default sorting direction for the active sorting property.</param>
 115/// <param name="Order">The order in which the sorting property was activated.</param>
 116internal sealed record SortingDefaultState(ListSortDirection Direction, int Order);
 117