| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FilterGroupViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.ObjectModel; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Linq.Expressions; |
| | | 11 | | using MyNet.Collections; |
| | | 12 | | using MyNet.Observable; |
| | | 13 | | using MyNet.Primitives; |
| | | 14 | | |
| | | 15 | | namespace 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> |
| | | 21 | | public class FilterGroupViewModel<T> : ObservableObject, IFilterGroupViewModel<T> |
| | | 22 | | { |
| | 6 | 23 | | 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> |
| | 3 | 28 | | 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> |
| | 6 | 38 | | 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> |
| | 6 | 49 | | public FilterGroupViewModel(bool isReadOnly = false) |
| | | 50 | | { |
| | 6 | 51 | | IsReadOnly = isReadOnly; |
| | 6 | 52 | | Children = new(_children); |
| | 6 | 53 | | } |
| | | 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> |
| | 9 | 59 | | 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> |
| | 0 | 65 | | 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> |
| | 0 | 70 | | 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> |
| | 0 | 75 | | 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 | | { |
| | 0 | 84 | | if (!IsEnabled) |
| | 0 | 85 | | return null; |
| | | 86 | | |
| | 0 | 87 | | var expressions = _children |
| | 0 | 88 | | .Select(c => c.BuildExpression()) |
| | 0 | 89 | | .NotNull() |
| | 0 | 90 | | .ToList(); |
| | | 91 | | |
| | 0 | 92 | | if (expressions.Count == 0) |
| | 0 | 93 | | return null; |
| | | 94 | | |
| | 0 | 95 | | var param = Expression.Parameter(typeof(T), "x"); |
| | 0 | 96 | | var bodies = expressions.ConvertAll(e => ReplaceParameter(e!, param)); |
| | 0 | 97 | | var body = bodies[0]; |
| | | 98 | | |
| | 0 | 99 | | for (var i = 1; i < bodies.Count; i++) |
| | | 100 | | { |
| | 0 | 101 | | body = Operator switch |
| | 0 | 102 | | { |
| | 0 | 103 | | LogicalOperator.And => Expression.AndAlso(body, bodies[i]), |
| | 0 | 104 | | LogicalOperator.Or => Expression.OrElse(body, bodies[i]), |
| | 0 | 105 | | _ => throw new NotSupportedException() |
| | 0 | 106 | | }; |
| | | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | 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 | | { |
| | 0 | 120 | | var visitor = new ReplaceVisitor(expr.Parameters[0], param); |
| | 0 | 121 | | 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> |
| | 0 | 129 | | private sealed class ReplaceVisitor(ParameterExpression oldParam, ParameterExpression newParam) : ExpressionVisitor |
| | | 130 | | { |
| | | 131 | | protected override Expression VisitParameter(ParameterExpression node) |
| | 0 | 132 | | => node == oldParam ? newParam : base.VisitParameter(node); |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |