< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.FilterGroupViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/FilterGroupViewModel.cs
Tag: 323_28699572109
Line coverage
23%
Covered lines: 8
Uncovered lines: 26
Coverable lines: 34
Total lines: 135
Line coverage: 23.5%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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%11100%
set_Operator(...)100%11100%
set_IsEnabled(...)100%11100%
Add(...)100%11100%
Remove(...)100%210%
Clear()100%210%
Reset()0%620%
BuildExpression()0%156120%
ReplaceParameter(...)100%210%
.ctor(...)100%210%
VisitParameter(...)0%620%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FilterGroupViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.ObjectModel;
 9using System.Linq;
 10using System.Linq.Expressions;
 11using MyNet.Collections;
 12using MyNet.Observable;
 13using MyNet.Primitives;
 14
 15namespace MyNet.UI.ViewModels.List.Filtering;
 16
 17/// <summary>
 18/// Implements a view model for a group of filter nodes, which can contain child filter nodes and a logical operator (AN
 19/// </summary>
 20/// <typeparam name="T">The type of the items being filtered.</typeparam>
 21public class FilterGroupViewModel<T> : ObservableObject, IFilterGroupViewModel<T>
 22{
 623    private readonly ObservableCollection<IFilterNodeViewModel<T>> _children = [];
 24
 25    /// <summary>
 26    /// Gets or sets the logical operator used to combine the child filters. The default operator is AND.
 27    /// </summary>
 328    public LogicalOperator Operator { get; set => SetProperty(ref field, value); } = LogicalOperator.And;
 29
 30    /// <summary>
 31    /// Gets a value indicating whether this filter condition is read-only.
 32    /// </summary>
 33    public bool IsReadOnly { get; }
 34
 35    /// <summary>
 36    /// Gets or sets a value indicating whether the filter group is enabled.
 37    /// </summary>
 638    public bool IsEnabled { get; set => SetProperty(ref field, value); } = true;
 39
 40    /// <summary>
 41    /// Gets a read-only collection of child filter nodes contained in this group. The children can be either simple fil
 42    /// </summary>
 43    public ReadOnlyObservableCollection<IFilterNodeViewModel<T>> Children { get; }
 44
 45    /// <summary>
 46    /// Initializes a new instance of the <see cref="FilterGroupViewModel{T}"/> class, creating an empty group of filter
 47    /// </summary>
 48    /// <param name="isReadOnly">A value indicating whether this filter group is read-only.</param>
 649    public FilterGroupViewModel(bool isReadOnly = false)
 50    {
 651        IsReadOnly = isReadOnly;
 652        Children = new(_children);
 653    }
 54
 55    /// <summary>
 56    /// Adds a child filter node to this group. The child can be either a simple filter or another group of filters. The
 57    /// </summary>
 58    /// <param name="child">The child filter node to add.</param>
 959    public void Add(IFilterNodeViewModel<T> child) => _children.Add(child);
 60
 61    /// <summary>
 62    /// Removes a child filter node from this group.
 63    /// </summary>
 64    /// <param name="child">The child filter node to remove.</param>
 065    public void Remove(IFilterNodeViewModel<T> child) => _children.Remove(child);
 66
 67    /// <summary>
 68    /// Clears all child filter nodes from this group, leaving it empty. After calling this method, the filter group wil
 69    /// </summary>
 070    public void Clear() => _children.Clear();
 71
 72    /// <summary>
 73    /// Resets the filter condition to its default state. This method should be implemented by derived classes to clear 
 74    /// </summary>
 075    public void Reset() => _children.ForEach(c => c.Reset());
 76
 77    /// <summary>
 78    /// Builds the expression representing this filter group by combining the expressions of its active child nodes usin
 79    /// </summary>
 80    /// <returns>The combined expression representing the filter group.</returns>
 81    /// <exception cref="NotSupportedException">Thrown when the logical operator is not supported.</exception>
 82    public Expression<Func<T, bool>>? BuildExpression()
 83    {
 084        if (!IsEnabled)
 085            return null;
 86
 087        var expressions = _children
 088            .Select(c => c.BuildExpression())
 089            .NotNull()
 090            .ToList();
 91
 092        if (expressions.Count == 0)
 093            return null;
 94
 095        var param = Expression.Parameter(typeof(T), "x");
 096        var bodies = expressions.ConvertAll(e => ReplaceParameter(e!, param));
 097        var body = bodies[0];
 98
 099        for (var i = 1; i < bodies.Count; i++)
 100        {
 0101            body = Operator switch
 0102            {
 0103                LogicalOperator.And => Expression.AndAlso(body, bodies[i]),
 0104                LogicalOperator.Or => Expression.OrElse(body, bodies[i]),
 0105                _ => throw new NotSupportedException()
 0106            };
 107        }
 108
 0109        return Expression.Lambda<Func<T, bool>>(body, param);
 110    }
 111
 112    /// <summary>
 113    /// Replaces the parameter in the given expression with a new parameter. This is necessary to combine multiple expre
 114    /// </summary>
 115    /// <param name="expr">The expression in which to replace the parameter.</param>
 116    /// <param name="param">The new parameter to use in the expression.</param>
 117    /// <returns>The expression with the parameter replaced.</returns>
 118    private static Expression ReplaceParameter(Expression<Func<T, bool>> expr, ParameterExpression param)
 119    {
 0120        var visitor = new ReplaceVisitor(expr.Parameters[0], param);
 0121        return visitor.Visit(expr.Body);
 122    }
 123
 124    /// <summary>
 125    /// An expression visitor that replaces occurrences of a specific parameter with a new parameter in an expression tr
 126    /// </summary>
 127    /// <param name="oldParam">The parameter to be replaced.</param>
 128    /// <param name="newParam">The new parameter to replace the old parameter with.</param>
 0129    private sealed class ReplaceVisitor(ParameterExpression oldParam, ParameterExpression newParam) : ExpressionVisitor
 130    {
 131        protected override Expression VisitParameter(ParameterExpression node)
 0132            => node == oldParam ? newParam : base.VisitParameter(node);
 133    }
 134}
 135