| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="EnumMultipleFilterViewModel.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 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> |
| | | 25 | | public class EnumMultipleFilterViewModel<T, TEnum>( |
| | | 26 | | string propertyName, |
| | | 27 | | Expression<Func<T, TEnum>> property, |
| | | 28 | | BinaryOperator operatorMode = BinaryOperator.Is) |
| | 0 | 29 | | : FilterConditionViewModel<T>(propertyName) |
| | | 30 | | where TEnum : struct, Enum |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the expression representing the enum property to filter on. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | 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> |
| | 0 | 40 | | 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> |
| | 0 | 46 | | public TEnum[]? Values { get; set => SetProperty(ref field, value); } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the available enum values for selection. |
| | | 50 | | /// </summary> |
| | 0 | 51 | | 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> |
| | 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.Contains(x.Property) |
| | 0 | 66 | | var valuesConstant = Expression.Constant(Values, typeof(ICollection<TEnum>)); |
| | 0 | 67 | | var containsMethod = typeof(ICollection<TEnum>).GetMethod(nameof(ICollection<>.Contains))!; |
| | 0 | 68 | | var containsCall = Expression.Call(valuesConstant, containsMethod, propertyBody); |
| | | 69 | | |
| | 0 | 70 | | Expression body = Operator switch |
| | 0 | 71 | | { |
| | 0 | 72 | | BinaryOperator.Is => containsCall, |
| | 0 | 73 | | BinaryOperator.IsNot => Expression.Not(containsCall), |
| | 0 | 74 | | _ => throw new NotSupportedException($"Operator {Operator} is not supported") |
| | 0 | 75 | | }; |
| | | 76 | | |
| | 0 | 77 | | 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> |
| | 0 | 83 | | public override void Reset() => Values = null; |
| | | 84 | | } |
| | | 85 | | |