| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ComparableFilterViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Linq.Expressions; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | |
| | | 11 | | namespace MyNet.UI.ViewModels.List.Filtering.Filters; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Filter view model for comparable (numeric, date, etc.) properties. |
| | | 15 | | /// Supports range filtering (from/to) and various comparison operators including IsBetween. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | | 18 | | /// <typeparam name="TValue">The type of the comparable value (must be a struct implementing IComparable).</typeparam> |
| | | 19 | | /// <param name="propertyName">The key that identifies this filter condition.</param> |
| | | 20 | | /// <param name="property">The expression representing the comparable property to filter on.</param> |
| | | 21 | | /// <param name="comparableOperator">The comparison operator to use (default: EqualsTo).</param> |
| | | 22 | | public class ComparableFilterViewModel<T, TValue>( |
| | | 23 | | string propertyName, |
| | | 24 | | Expression<Func<T, TValue>> property, |
| | | 25 | | ComplexComparableOperator comparableOperator = ComplexComparableOperator.EqualsTo) |
| | 0 | 26 | | : FilterConditionViewModel<T>(propertyName) |
| | | 27 | | where TValue : struct, IComparable<TValue> |
| | | 28 | | { |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the expression representing the comparable property to filter on. |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public Expression<Func<T, TValue>> Property { get; } = property; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets or sets the comparison operator used for matching. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public ComplexComparableOperator Operator { get; set => SetProperty(ref field, value); } = comparableOperator; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets the lower bound value for comparison. |
| | | 41 | | /// Used as the primary value for single-value operators (EqualsTo, LessThan, etc.). |
| | | 42 | | /// </summary> |
| | 0 | 43 | | public TValue? From { get; set => SetProperty(ref field, value); } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets or sets the upper bound value for comparison. |
| | | 47 | | /// Used together with <see cref="From"/> for range operators (IsBetween, IsNotBetween). |
| | | 48 | | /// </summary> |
| | 0 | 49 | | public TValue? To { get; set => SetProperty(ref field, value); } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets the minimum allowed value for the filter range. |
| | | 53 | | /// </summary> |
| | 0 | 54 | | public TValue? Minimum { get; set => SetProperty(ref field, value); } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets the maximum allowed value for the filter range. |
| | | 58 | | /// </summary> |
| | 0 | 59 | | public TValue? Maximum { get; set => SetProperty(ref field, value); } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets a value indicating whether this filter is in an empty state. |
| | | 63 | | /// A comparable filter is empty when both From and To are null or equal to default. |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public override bool IsEmpty => (From is null || From.Equals(default(TValue))) && (To is null || To.Equals(default(T |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | protected override Expression<Func<T, bool>> BuildExpressionCore() |
| | | 69 | | { |
| | 0 | 70 | | var parameter = Property.Parameters[0]; |
| | 0 | 71 | | var propertyBody = Property.Body; |
| | | 72 | | |
| | 0 | 73 | | Expression body = Operator switch |
| | 0 | 74 | | { |
| | 0 | 75 | | ComplexComparableOperator.EqualsTo when From is not null => |
| | 0 | 76 | | Expression.Equal(propertyBody, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 77 | | |
| | 0 | 78 | | ComplexComparableOperator.NotEqualsTo when From is not null => |
| | 0 | 79 | | Expression.NotEqual(propertyBody, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 80 | | |
| | 0 | 81 | | ComplexComparableOperator.LessThan when To is not null => |
| | 0 | 82 | | Expression.LessThan(propertyBody, Expression.Constant(To.Value, typeof(TValue))), |
| | 0 | 83 | | |
| | 0 | 84 | | ComplexComparableOperator.GreaterThan when From is not null => |
| | 0 | 85 | | Expression.GreaterThan(propertyBody, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 86 | | |
| | 0 | 87 | | ComplexComparableOperator.LessEqualThan when To is not null => |
| | 0 | 88 | | Expression.LessThanOrEqual(propertyBody, Expression.Constant(To.Value, typeof(TValue))), |
| | 0 | 89 | | |
| | 0 | 90 | | ComplexComparableOperator.GreaterEqualThan when From is not null => |
| | 0 | 91 | | Expression.GreaterThanOrEqual(propertyBody, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 92 | | |
| | 0 | 93 | | ComplexComparableOperator.IsBetween when From is not null && To is not null => |
| | 0 | 94 | | Expression.AndAlso( |
| | 0 | 95 | | Expression.GreaterThanOrEqual(propertyBody, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 96 | | Expression.LessThanOrEqual(propertyBody, Expression.Constant(To.Value, typeof(TValue)))), |
| | 0 | 97 | | |
| | 0 | 98 | | ComplexComparableOperator.IsNotBetween when From is not null && To is not null => |
| | 0 | 99 | | Expression.OrElse( |
| | 0 | 100 | | Expression.LessThan(propertyBody, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 101 | | Expression.GreaterThan(propertyBody, Expression.Constant(To.Value, typeof(TValue)))), |
| | 0 | 102 | | |
| | 0 | 103 | | _ => Expression.Constant(true) |
| | 0 | 104 | | }; |
| | | 105 | | |
| | 0 | 106 | | return Expression.Lambda<Func<T, bool>>(body, parameter); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Resets the filter to its default state by clearing From and To values. |
| | | 111 | | /// </summary> |
| | | 112 | | public override void Reset() |
| | | 113 | | { |
| | 0 | 114 | | From = null; |
| | 0 | 115 | | To = null; |
| | 0 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |