| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectedValuesFilterViewModel.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.Expressions; |
| | | 10 | | using MyNet.Primitives; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.ViewModels.List.Filtering.Filters; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Filter view model for selecting multiple values from a list of available values. |
| | | 16 | | /// Matches items where the property value is contained in the selected values list. |
| | | 17 | | /// Supports Is/IsNot operators. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | | 20 | | /// <typeparam name="TValue">The type of the property value to filter on.</typeparam> |
| | | 21 | | /// <param name="propertyName">The key that identifies this filter condition.</param> |
| | | 22 | | /// <param name="property">The expression representing the property to filter on.</param> |
| | | 23 | | /// <param name="availableValues">The list of available values for selection.</param> |
| | | 24 | | /// <param name="operatorMode">The binary operator to use (default: Is).</param> |
| | | 25 | | public class SelectedValuesFilterViewModel<T, TValue>( |
| | | 26 | | string propertyName, |
| | | 27 | | Expression<Func<T, TValue?>> property, |
| | | 28 | | IEnumerable<TValue> availableValues, |
| | | 29 | | BinaryOperator operatorMode = BinaryOperator.Is) |
| | 0 | 30 | | : FilterConditionViewModel<T>(propertyName) |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the expression representing the property to filter on. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | public Expression<Func<T, TValue?>> Property { get; } = property; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets or sets the binary operator used for matching (Is or IsNot). |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public BinaryOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets or sets the selected values to match against. |
| | | 44 | | /// When null or empty, the filter is considered empty. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public TValue[]? 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<TValue> 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 | | /// A selected values filter is empty when <see cref="Values"/> is null or empty. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public override bool IsEmpty => Values is null || Values.Length == 0; |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | protected override Expression<Func<T, bool>> BuildExpressionCore() |
| | | 61 | | { |
| | 0 | 62 | | var parameter = Property.Parameters[0]; |
| | 0 | 63 | | var propertyBody = Property.Body; |
| | | 64 | | |
| | | 65 | | // Build: Values.Any(v => EqualityComparer<TValue>.Default.Equals(v, x.Property)) |
| | | 66 | | // Simplified approach: use a captured HashSet for O(1) lookups |
| | 0 | 67 | | var values = new HashSet<TValue>(Values ?? []); |
| | 0 | 68 | | var valuesConstant = Expression.Constant(values, typeof(HashSet<TValue>)); |
| | 0 | 69 | | var containsMethod = typeof(HashSet<TValue>).GetMethod(nameof(HashSet<>.Contains))!; |
| | 0 | 70 | | var containsCall = Expression.Call(valuesConstant, containsMethod, Expression.Convert(propertyBody, typeof(TValu |
| | | 71 | | |
| | 0 | 72 | | Expression body = Operator switch |
| | 0 | 73 | | { |
| | 0 | 74 | | BinaryOperator.Is => containsCall, |
| | 0 | 75 | | BinaryOperator.IsNot => Expression.Not(containsCall), |
| | 0 | 76 | | _ => throw new NotSupportedException($"Operator {Operator} is not supported") |
| | 0 | 77 | | }; |
| | | 78 | | |
| | 0 | 79 | | return Expression.Lambda<Func<T, bool>>(body, parameter); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Resets the filter to its default state by clearing the values. |
| | | 84 | | /// </summary> |
| | 0 | 85 | | public override void Reset() => Values = null; |
| | | 86 | | } |
| | | 87 | | |