< Summary

Information
Class: MyNet.UI.ViewModels.List.Sorting.SortingPropertyViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Sorting/SortingPropertyViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 96
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%
set_Direction(...)100%210%
set_IsEnabled(...)0%620%
set_ActivatedAt(...)100%210%
Build()100%210%
Matches(...)100%210%
OnIsEnabledChanged()0%620%
ToString()100%210%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SortingPropertyViewModel.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.Observable.Collections.Sorting;
 12using MyNet.Primitives;
 13
 14namespace MyNet.UI.ViewModels.List.Sorting;
 15
 16/// <summary>
 17/// Represents a view model for a sorting property that can be used to configure how a collection is sorted.
 18/// </summary>
 19/// <param name="expression">The expression used to access the property to sort by.</param>
 20/// <param name="key">The unique identifier for the sorting property.</param>
 21/// <param name="displayName">The provider for the localized display name.</param>
 22/// <param name="direction">The initial sort direction. Default is ascending.</param>
 23/// <typeparam name="T">The type of the items being sorted.</typeparam>
 024public class SortingPropertyViewModel<T>(
 025    Expression<Func<T, object?>> expression,
 026    string key,
 027    IObservableValue<string> displayName,
 028    ListSortDirection direction = ListSortDirection.Ascending)
 29    : ObservableObject, ISortingPropertyViewModel<T>
 30{
 31    /// <summary>
 32    /// Gets the localized display name for this sorting property.
 33    /// </summary>
 034    public IObservableValue<string> DisplayName { get; } = displayName;
 35
 36    /// <summary>
 37    /// Gets the unique identifier for the sorting property. This key is used to identify the sorting property within th
 38    /// </summary>
 039    string ISortingPropertyViewModel<T>.Key { get; } = key;
 40
 41    /// <summary>
 42    /// Gets the expression that defines the sorting property. This expression is used to determine the value of the pro
 43    /// </summary>
 044    public Expression<Func<T, object?>> Expression { get; } = expression;
 45
 46    /// <summary>
 47    /// Gets or sets the sort direction (ascending or descending).
 48    /// </summary>
 049    public ListSortDirection Direction { get; set => SetProperty(ref field, value); } = direction;
 50
 51    /// <summary>
 52    /// Gets or sets a value indicating whether this sorting property is currently enabled/active.
 53    /// When true, this property contributes to the collection's sorting.
 54    /// When false, this property is available but not applied.
 55    /// </summary>
 56    public bool IsEnabled
 57    {
 58        get;
 59        set
 60        {
 061            if (!SetProperty(ref field, value))
 062                return;
 63
 064            OnIsEnabledChanged();
 065        }
 66    }
 67
 068        = true;
 69
 70    /// <summary>
 71    /// Gets the date and time when this sorting property was activated (enabled). This property is null if the sorting 
 72    /// </summary>
 073    public DateTime? ActivatedAt { get; private set => SetProperty(ref field, value); }
 74
 75    /// <summary>
 76    /// Builds the core sorting property based on the current state of the view model. If the sorting property is active
 77    /// </summary>
 78    /// <returns>An instance of <see cref="ISortingProperty{T}"/> if the sorting property is active; otherwise, null.</r
 079    public ISortingProperty<T> Build() => new ExpressionSortingProperty<T>(Expression, Direction);
 80
 81    /// <summary>
 82    /// Determines whether the provided sorting property matches the expression of this view model. This method compares
 83    /// </summary>
 84    /// <param name="property">The sorting property to compare with this view model.</param>
 85    /// <returns>True if the expressions match; otherwise, false.</returns>
 086    public bool Matches(ISortingProperty<T> property) => Expression.IsSimilar(property.ProvideExpression());
 87
 88    /// <summary>
 89    /// Handles changes to the IsEnabled property. When the IsEnabled property changes, this method updates the Activate
 90    /// </summary>
 091    protected virtual void OnIsEnabledChanged() => ActivatedAt = IsEnabled ? DateTime.UtcNow : null;
 92
 93    /// <inheritdoc />
 094    public override string ToString() => DisplayName.Value.OrEmpty();
 95}
 96