< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.EnumMultipleFilterViewModel<T1, T2>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/EnumMultipleFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 85
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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_Values(...)100%210%
get_IsEmpty()0%620%
BuildExpressionCore()0%2040%
Reset()100%210%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="EnumMultipleFilterViewModel.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 (multiple values selection).
 17/// Matches items where the property value is contained in the selected values list.
 18/// Supports Is/IsNot operators.
 19/// </summary>
 20/// <typeparam name="T">The type of the items being filtered.</typeparam>
 21/// <typeparam name="TEnum">The enum type to filter on.</typeparam>
 22/// <param name="propertyName">The key that identifies this filter condition.</param>
 23/// <param name="property">The expression representing the enum property to filter on.</param>
 24/// <param name="operatorMode">The binary operator to use (default: Is).</param>
 25public class EnumMultipleFilterViewModel<T, TEnum>(
 26    string propertyName,
 27    Expression<Func<T, TEnum>> property,
 28    BinaryOperator operatorMode = BinaryOperator.Is)
 029    : FilterConditionViewModel<T>(propertyName)
 30    where TEnum : struct, Enum
 31{
 32    /// <summary>
 33    /// Gets the expression representing the enum property to filter on.
 34    /// </summary>
 035    public Expression<Func<T, TEnum>> Property { get; } = property;
 36
 37    /// <summary>
 38    /// Gets or sets the binary operator used for matching (Is or IsNot).
 39    /// </summary>
 040    public BinaryOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode;
 41
 42    /// <summary>
 43    /// Gets or sets the selected enum values to match against.
 44    /// When null or empty, the filter is considered empty.
 45    /// </summary>
 046    public TEnum[]? Values { get; set => SetProperty(ref field, value); }
 47
 48    /// <summary>
 49    /// Gets the available enum values for selection.
 50    /// </summary>
 051    public IReadOnlyList<TEnum> AvailableValues { get; } = Enum.GetValues<TEnum>().ToList().AsReadOnly();
 52
 53    /// <summary>
 54    /// Gets a value indicating whether this filter is in an empty state.
 55    /// An enum multiple filter is empty when <see cref="Values"/> is null or empty.
 56    /// </summary>
 057    public override bool IsEmpty => Values is null || Values.Length == 0;
 58
 59    /// <inheritdoc />
 60    protected override Expression<Func<T, bool>> BuildExpressionCore()
 61    {
 062        var parameter = Property.Parameters[0];
 063        var propertyBody = Property.Body;
 64
 65        // Build: Values.Contains(x.Property)
 066        var valuesConstant = Expression.Constant(Values, typeof(ICollection<TEnum>));
 067        var containsMethod = typeof(ICollection<TEnum>).GetMethod(nameof(ICollection<>.Contains))!;
 068        var containsCall = Expression.Call(valuesConstant, containsMethod, propertyBody);
 69
 070        Expression body = Operator switch
 071        {
 072            BinaryOperator.Is => containsCall,
 073            BinaryOperator.IsNot => Expression.Not(containsCall),
 074            _ => throw new NotSupportedException($"Operator {Operator} is not supported")
 075        };
 76
 077        return Expression.Lambda<Func<T, bool>>(body, parameter);
 78    }
 79
 80    /// <summary>
 81    /// Resets the filter to its default state by clearing the values.
 82    /// </summary>
 083    public override void Reset() => Values = null;
 84}
 85