| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectedValueFilterViewModel.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 a single value from a list of available values. |
| | | 16 | | /// Supports Is/IsNot operators with equality comparison. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | | 19 | | /// <typeparam name="TValue">The type of the property value to filter on.</typeparam> |
| | | 20 | | /// <param name="propertyName">The key that identifies this filter condition.</param> |
| | | 21 | | /// <param name="property">The expression representing the property to filter on.</param> |
| | | 22 | | /// <param name="availableValues">The list of available values for selection.</param> |
| | | 23 | | /// <param name="operatorMode">The binary operator to use (default: Is).</param> |
| | | 24 | | public class SelectedValueFilterViewModel<T, TValue>( |
| | | 25 | | string propertyName, |
| | | 26 | | Expression<Func<T, TValue?>> property, |
| | | 27 | | IEnumerable<TValue> availableValues, |
| | | 28 | | BinaryOperator operatorMode = BinaryOperator.Is) |
| | 0 | 29 | | : FilterConditionViewModel<T>(propertyName) |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the expression representing the property to filter on. |
| | | 33 | | /// </summary> |
| | 0 | 34 | | public Expression<Func<T, TValue?>> Property { get; } = property; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets the binary operator used for matching (Is or IsNot). |
| | | 38 | | /// </summary> |
| | 0 | 39 | | public BinaryOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or sets the selected value to match against. |
| | | 43 | | /// When null, the filter is considered empty. |
| | | 44 | | /// </summary> |
| | 0 | 45 | | public TValue? Value { get; set => SetProperty(ref field, value); } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the available values for selection. |
| | | 49 | | /// </summary> |
| | 0 | 50 | | public IEnumerable<TValue> AvailableValues { get; set => SetProperty(ref field, value); } = availableValues; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a value indicating whether this filter is in an empty state. |
| | | 54 | | /// A selected value filter is empty when <see cref="Value"/> is null. |
| | | 55 | | /// </summary> |
| | 0 | 56 | | public override bool IsEmpty => Value is null; |
| | | 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 | | |
| | | 64 | | // Use EqualityComparer<TValue>.Default.Equals for robust comparison |
| | 0 | 65 | | var equalsMethod = typeof(EqualityComparer<TValue>).GetProperty(nameof(EqualityComparer<>.Default))!; |
| | 0 | 66 | | var defaultComparer = Expression.Property(null, equalsMethod); |
| | 0 | 67 | | var equalsCall = Expression.Call( |
| | 0 | 68 | | defaultComparer, |
| | 0 | 69 | | typeof(EqualityComparer<TValue>).GetMethod(nameof(EqualityComparer<>.Equals), [typeof(TValue), typeof(TValue |
| | 0 | 70 | | Expression.Convert(propertyBody, typeof(TValue)), |
| | 0 | 71 | | Expression.Constant(Value, typeof(TValue))); |
| | | 72 | | |
| | 0 | 73 | | Expression body = Operator switch |
| | 0 | 74 | | { |
| | 0 | 75 | | BinaryOperator.Is => equalsCall, |
| | 0 | 76 | | BinaryOperator.IsNot => Expression.Not(equalsCall), |
| | 0 | 77 | | _ => throw new NotSupportedException($"Operator {Operator} is not supported") |
| | 0 | 78 | | }; |
| | | 79 | | |
| | 0 | 80 | | return Expression.Lambda<Func<T, bool>>(body, parameter); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Resets the filter to its default state by clearing the value. |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public override void Reset() => Value = default; |
| | | 87 | | } |
| | | 88 | | |