< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.SelectedValueFilterViewModel<T1, T2>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/SelectedValueFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 88
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_Operator(...)100%210%
set_Value(...)100%210%
set_AvailableValues(...)100%210%
get_IsEmpty()100%210%
BuildExpressionCore()0%2040%
Reset()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/SelectedValueFilterViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectedValueFilterViewModel.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.Expressions;
 10using MyNet.Primitives;
 11
 12namespace MyNet.UI.ViewModels.List.Filtering.Filters;
 13
 14/// <summary>
 15/// Filter view model for selecting a single value from a list of available values.
 16/// Supports Is/IsNot operators with equality comparison.
 17/// </summary>
 18/// <typeparam name="T">The type of the items being filtered.</typeparam>
 19/// <typeparam name="TValue">The type of the property value to filter on.</typeparam>
 20/// <param name="propertyName">The key that identifies this filter condition.</param>
 21/// <param name="property">The expression representing the property to filter on.</param>
 22/// <param name="availableValues">The list of available values for selection.</param>
 23/// <param name="operatorMode">The binary operator to use (default: Is).</param>
 24public class SelectedValueFilterViewModel<T, TValue>(
 25    string propertyName,
 26    Expression<Func<T, TValue?>> property,
 27    IEnumerable<TValue> availableValues,
 28    BinaryOperator operatorMode = BinaryOperator.Is)
 029    : FilterConditionViewModel<T>(propertyName)
 30{
 31    /// <summary>
 32    /// Gets the expression representing the property to filter on.
 33    /// </summary>
 034    public Expression<Func<T, TValue?>> Property { get; } = property;
 35
 36    /// <summary>
 37    /// Gets or sets the binary operator used for matching (Is or IsNot).
 38    /// </summary>
 039    public BinaryOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode;
 40
 41    /// <summary>
 42    /// Gets or sets the selected value to match against.
 43    /// When null, the filter is considered empty.
 44    /// </summary>
 045    public TValue? Value { get; set => SetProperty(ref field, value); }
 46
 47    /// <summary>
 48    /// Gets or sets the available values for selection.
 49    /// </summary>
 050    public IEnumerable<TValue> AvailableValues { get; set => SetProperty(ref field, value); } = availableValues;
 51
 52    /// <summary>
 53    /// Gets a value indicating whether this filter is in an empty state.
 54    /// A selected value filter is empty when <see cref="Value"/> is null.
 55    /// </summary>
 056    public override bool IsEmpty => Value is null;
 57
 58    /// <inheritdoc />
 59    protected override Expression<Func<T, bool>> BuildExpressionCore()
 60    {
 061        var parameter = Property.Parameters[0];
 062        var propertyBody = Property.Body;
 63
 64        // Use EqualityComparer<TValue>.Default.Equals for robust comparison
 065        var equalsMethod = typeof(EqualityComparer<TValue>).GetProperty(nameof(EqualityComparer<>.Default))!;
 066        var defaultComparer = Expression.Property(null, equalsMethod);
 067        var equalsCall = Expression.Call(
 068            defaultComparer,
 069            typeof(EqualityComparer<TValue>).GetMethod(nameof(EqualityComparer<>.Equals), [typeof(TValue), typeof(TValue
 070            Expression.Convert(propertyBody, typeof(TValue)),
 071            Expression.Constant(Value, typeof(TValue)));
 72
 073        Expression body = Operator switch
 074        {
 075            BinaryOperator.Is => equalsCall,
 076            BinaryOperator.IsNot => Expression.Not(equalsCall),
 077            _ => throw new NotSupportedException($"Operator {Operator} is not supported")
 078        };
 79
 080        return Expression.Lambda<Func<T, bool>>(body, parameter);
 81    }
 82
 83    /// <summary>
 84    /// Resets the filter to its default state by clearing the value.
 85    /// </summary>
 086    public override void Reset() => Value = default;
 87}
 88