< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.EnumFilterViewModel<T1, T2>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/EnumFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 80
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%
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/EnumFilterViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="EnumFilterViewModel.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.Primitives;
 12
 13namespace MyNet.UI.ViewModels.List.Filtering.Filters;
 14
 15/// <summary>
 16/// Filter view model for enum properties (single value selection).
 17/// Supports Is/IsNot operators.
 18/// </summary>
 19/// <typeparam name="T">The type of the items being filtered.</typeparam>
 20/// <typeparam name="TEnum">The enum type to filter on.</typeparam>
 21/// <param name="propertyName">The key that identifies this filter condition.</param>
 22/// <param name="property">The expression representing the enum property to filter on.</param>
 23/// <param name="operatorMode">The binary operator to use (default: Is).</param>
 24public class EnumFilterViewModel<T, TEnum>(
 25    string propertyName,
 26    Expression<Func<T, TEnum>> property,
 27    BinaryOperator operatorMode = BinaryOperator.Is)
 028    : FilterConditionViewModel<T>(propertyName)
 29    where TEnum : struct, Enum
 30{
 31    /// <summary>
 32    /// Gets the expression representing the enum property to filter on.
 33    /// </summary>
 034    public Expression<Func<T, TEnum>> 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 enum value to match against.
 43    /// When null, the filter is considered empty.
 44    /// </summary>
 045    public TEnum? Value { get; set => SetProperty(ref field, value); }
 46
 47    /// <summary>
 48    /// Gets the available enum values for selection.
 49    /// </summary>
 050    public IReadOnlyList<TEnum> AvailableValues { get; } = Enum.GetValues<TEnum>().ToList().AsReadOnly();
 51
 52    /// <summary>
 53    /// Gets a value indicating whether this filter is in an empty state.
 54    /// An enum 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;
 063        var value = Expression.Constant(Value!.Value, typeof(TEnum));
 64
 065        Expression body = Operator switch
 066        {
 067            BinaryOperator.Is => Expression.Equal(propertyBody, value),
 068            BinaryOperator.IsNot => Expression.NotEqual(propertyBody, value),
 069            _ => throw new NotSupportedException($"Operator {Operator} is not supported")
 070        };
 71
 072        return Expression.Lambda<Func<T, bool>>(body, parameter);
 73    }
 74
 75    /// <summary>
 76    /// Resets the filter to its default state by clearing the value.
 77    /// </summary>
 078    public override void Reset() => Value = null;
 79}
 80