< Summary

Information
Class: MyNet.UI.ViewModels.List.Grouping.GroupingPropertyViewModelBuilder<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Grouping/GroupingPropertyViewModelBuilder.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 13
Coverable lines: 13
Total lines: 97
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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%
ByDefault(...)0%620%
Build()0%620%
ResolveKey()0%2040%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="GroupingPropertyViewModelBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Linq.Expressions;
 9using MyNet.Observable;
 10using MyNet.Primitives;
 11
 12namespace MyNet.UI.ViewModels.List.Grouping;
 13
 14/// <summary>
 15/// Fluent builder used to configure a grouping property view model.
 16/// </summary>
 17/// <typeparam name="T">The type of the items to group.</typeparam>
 18public sealed class GroupingPropertyViewModelBuilder<T>(Expression<Func<T, object?>> expression)
 19{
 20    private string? _key;
 21    private IObservableValue<string>? _displayName;
 22    private GroupingDefaultState? _defaultState;
 23    private int _order;
 24
 25    /// <summary>
 26    /// Sets the unique key of the grouping property.
 27    /// </summary>
 28    public GroupingPropertyViewModelBuilder<T> WithKey(string key)
 29    {
 030        _key = key;
 031        return this;
 32    }
 33
 34    /// <summary>
 35    /// Sets the display name provider used by the UI.
 36    /// </summary>
 37    public GroupingPropertyViewModelBuilder<T> WithDisplayName(IObservableValue<string> displayName)
 38    {
 039        _displayName = displayName;
 040        return this;
 41    }
 42
 43    /// <summary>
 44    /// Configures this grouping property to be active by default when the grouping view model is created. The optional 
 45    /// </summary>
 46    /// <param name="order">The order in which this grouping property should be activated relative to other properties.<
 47    public GroupingPropertyViewModelBuilder<T> ByDefault(int? order = null)
 48    {
 049        _defaultState = new(order ?? _order++);
 050        return this;
 51    }
 52
 53    /// <summary>
 54    /// Builds the grouping property configuration based on the current state of the builder. This method validates that
 55    /// </summary>
 56    /// <returns>The constructed <see cref="GroupingPropertyDefinition{T}"/> instance.</returns>
 57    /// <exception cref="InvalidOperationException">Thrown if a grouping property key has not been provided.</exception>
 58    internal GroupingPropertyDefinition<T> Build()
 59    {
 060        var key = ResolveKey();
 061        var displayName = _displayName ?? new LocalizedString(key);
 62
 063        return new(key, expression, displayName, _defaultState);
 64    }
 65
 66    /// <summary>
 67    /// Resolves the key for the grouping property. If a key has been explicitly set using the builder, it returns that 
 68    /// </summary>
 69    /// <returns>The resolved key for the grouping property.</returns>
 70    /// <exception cref="InvalidOperationException">Thrown if a grouping property key cannot be resolved.</exception>
 71    private string ResolveKey()
 72    {
 073        if (!string.IsNullOrWhiteSpace(_key))
 074            return _key;
 75
 076        var inferredKey = expression.PropertyName();
 77
 078        return string.IsNullOrWhiteSpace(inferredKey) ? throw new InvalidOperationException("A grouping property key mus
 79    }
 80}
 81
 82/// <summary>
 83/// Represents the configuration for a grouping property, including its key, expression, display name, and default group
 84/// </summary>
 85/// <param name="Key">The unique key of the grouping property.</param>
 86/// <param name="Expression">The expression used to extract the value for grouping.</param>
 87/// <param name="DisplayName">The display name provider for the UI.</param>
 88/// <param name="DefaultState">The default configuration for the grouping property, if any.</param>
 89/// <typeparam name="T">The type of the items being grouped.</typeparam>
 90internal sealed record GroupingPropertyDefinition<T>(string Key, Expression<Func<T, object?>> Expression, IObservableVal
 91
 92/// <summary>
 93/// Represents the active configuration for a grouping property, including its default order in which it was activated. 
 94/// </summary>
 95/// <param name="Order">The order in which the grouping property was activated.</param>
 96internal sealed record GroupingDefaultState(int Order);
 97