| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CollectionContainsFilterViewModel.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.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Linq.Expressions; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.List.Filtering.Filters; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Filter view model for collection properties. |
| | | 17 | | /// Checks whether a collection property contains any/all of the specified values. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | | 20 | | /// <typeparam name="TElement">The type of elements in the collection property.</typeparam> |
| | | 21 | | /// <param name="propertyName">The key that identifies this filter condition.</param> |
| | | 22 | | /// <param name="property">The expression representing the collection property to filter on.</param> |
| | | 23 | | /// <param name="availableValues">The available values for selection.</param> |
| | | 24 | | /// <param name="operatorMode">The logical operator to combine checks (And = all must match, Or = any must match).</para |
| | | 25 | | public class CollectionContainsFilterViewModel<T, TElement>( |
| | | 26 | | string propertyName, |
| | | 27 | | Expression<Func<T, IEnumerable<TElement>>> property, |
| | | 28 | | IEnumerable<TElement> availableValues, |
| | | 29 | | LogicalOperator operatorMode = LogicalOperator.Or) |
| | 0 | 30 | | : FilterConditionViewModel<T>(propertyName) |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the expression representing the collection property to filter on. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | public Expression<Func<T, IEnumerable<TElement>>> Property { get; } = property; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets or sets the logical operator (And = all values must be present, Or = any value must be present). |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public LogicalOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets or sets the values to search for in the collection. |
| | | 44 | | /// When null or empty, the filter is considered empty. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public TElement[]? Values { get; set => SetProperty(ref field, value); } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets or sets the available values for selection. |
| | | 50 | | /// </summary> |
| | 0 | 51 | | public IEnumerable<TElement> AvailableValues { get; set => SetProperty(ref field, value); } = availableValues; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets a value indicating whether this filter is in an empty state. |
| | | 55 | | /// </summary> |
| | 0 | 56 | | public override bool IsEmpty => Values is null || Values.Length == 0; |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | protected override Expression<Func<T, bool>> BuildExpressionCore() |
| | | 60 | | { |
| | 0 | 61 | | var parameter = Property.Parameters[0]; |
| | 0 | 62 | | var propertyBody = Property.Body; |
| | | 63 | | |
| | 0 | 64 | | var values = Values?.ToList() ?? []; |
| | | 65 | | |
| | | 66 | | // Build individual Contains checks for each value |
| | 0 | 67 | | var containsMethod = typeof(Enumerable).GetMethods() |
| | 0 | 68 | | .First(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2) |
| | 0 | 69 | | .MakeGenericMethod(typeof(TElement)); |
| | | 70 | | |
| | 0 | 71 | | var combined = values.Select(value => Expression.Call(containsMethod, propertyBody, Expression.Constant(value, t |
| | 0 | 72 | | .Aggregate<MethodCallExpression?, Expression?>(null, (current, containsCall) => current is null |
| | 0 | 73 | | ? containsCall |
| | 0 | 74 | | : Operator == LogicalOperator.And |
| | 0 | 75 | | ? Expression.AndAlso(current, containsCall!) |
| | 0 | 76 | | : Expression.OrElse(current, containsCall!)); |
| | | 77 | | |
| | 0 | 78 | | combined ??= Expression.Constant(true); |
| | | 79 | | |
| | 0 | 80 | | return Expression.Lambda<Func<T, bool>>(combined, parameter); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Resets the filter to its default state by clearing the values. |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public override void Reset() => Values = null; |
| | | 87 | | } |
| | | 88 | | |