| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NullableComparableFilterViewModel.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 nullable comparable properties (int?, DateTime?, double?, etc.). |
| | | 15 | | /// Supports range filtering (from/to) and various comparison operators including IsBetween. |
| | | 16 | | /// Null property values are excluded from match unless operator is specifically checking for null. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | | 19 | | /// <typeparam name="TValue">The type of the nullable comparable value (must be a struct implementing IComparable).</typ |
| | | 20 | | /// <param name="propertyName">The key that identifies this filter condition.</param> |
| | | 21 | | /// <param name="property">The expression representing the nullable comparable property to filter on.</param> |
| | | 22 | | /// <param name="comparableOperator">The comparison operator to use (default: EqualsTo).</param> |
| | | 23 | | public class NullableComparableFilterViewModel<T, TValue>( |
| | | 24 | | string propertyName, |
| | | 25 | | Expression<Func<T, TValue?>> property, |
| | | 26 | | ComplexComparableOperator comparableOperator = ComplexComparableOperator.EqualsTo) |
| | 0 | 27 | | : FilterConditionViewModel<T>(propertyName) |
| | | 28 | | where TValue : struct, IComparable<TValue> |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the expression representing the nullable comparable property to filter on. |
| | | 32 | | /// </summary> |
| | 0 | 33 | | public Expression<Func<T, TValue?>> Property { get; } = property; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the comparison operator used for matching. |
| | | 37 | | /// </summary> |
| | 0 | 38 | | public ComplexComparableOperator Operator { get; set => SetProperty(ref field, value); } = comparableOperator; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the lower bound value for comparison. |
| | | 42 | | /// </summary> |
| | | 43 | | public TValue? From |
| | | 44 | | { |
| | | 45 | | get; |
| | 0 | 46 | | set => SetProperty(ref field, value); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets or sets the upper bound value for comparison. |
| | | 51 | | /// </summary> |
| | | 52 | | public TValue? To |
| | | 53 | | { |
| | | 54 | | get; |
| | 0 | 55 | | set => SetProperty(ref field, value); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets or sets the minimum allowed value for the filter range. |
| | | 60 | | /// </summary> |
| | | 61 | | public TValue? Minimum |
| | | 62 | | { |
| | | 63 | | get; |
| | 0 | 64 | | set => SetProperty(ref field, value); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets or sets the maximum allowed value for the filter range. |
| | | 69 | | /// </summary> |
| | | 70 | | public TValue? Maximum |
| | | 71 | | { |
| | | 72 | | get; |
| | 0 | 73 | | set => SetProperty(ref field, value); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets a value indicating whether this filter is in an empty state. |
| | | 78 | | /// </summary> |
| | 0 | 79 | | public override bool IsEmpty => (From is null || From.Equals(default(TValue))) && (To is null || To.Equals(default(T |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | protected override Expression<Func<T, bool>> BuildExpressionCore() |
| | | 83 | | { |
| | 0 | 84 | | var parameter = Property.Parameters[0]; |
| | 0 | 85 | | var propertyBody = Property.Body; |
| | | 86 | | |
| | | 87 | | // x.Property.HasValue |
| | 0 | 88 | | var hasValue = Expression.Property(propertyBody, nameof(Nullable<>.HasValue)); |
| | | 89 | | |
| | | 90 | | // x.Property.Value |
| | 0 | 91 | | var propertyValue = Expression.Property(propertyBody, nameof(Nullable<>.Value)); |
| | | 92 | | |
| | 0 | 93 | | Expression comparison = Operator switch |
| | 0 | 94 | | { |
| | 0 | 95 | | ComplexComparableOperator.EqualsTo when From is not null => |
| | 0 | 96 | | Expression.Equal(propertyValue, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 97 | | |
| | 0 | 98 | | ComplexComparableOperator.NotEqualsTo when From is not null => |
| | 0 | 99 | | Expression.NotEqual(propertyValue, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 100 | | |
| | 0 | 101 | | ComplexComparableOperator.LessThan when To is not null => |
| | 0 | 102 | | Expression.LessThan(propertyValue, Expression.Constant(To.Value, typeof(TValue))), |
| | 0 | 103 | | |
| | 0 | 104 | | ComplexComparableOperator.GreaterThan when From is not null => |
| | 0 | 105 | | Expression.GreaterThan(propertyValue, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 106 | | |
| | 0 | 107 | | ComplexComparableOperator.LessEqualThan when To is not null => |
| | 0 | 108 | | Expression.LessThanOrEqual(propertyValue, Expression.Constant(To.Value, typeof(TValue))), |
| | 0 | 109 | | |
| | 0 | 110 | | ComplexComparableOperator.GreaterEqualThan when From is not null => |
| | 0 | 111 | | Expression.GreaterThanOrEqual(propertyValue, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 112 | | |
| | 0 | 113 | | ComplexComparableOperator.IsBetween when From is not null && To is not null => |
| | 0 | 114 | | Expression.AndAlso( |
| | 0 | 115 | | Expression.GreaterThanOrEqual(propertyValue, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 116 | | Expression.LessThanOrEqual(propertyValue, Expression.Constant(To.Value, typeof(TValue)))), |
| | 0 | 117 | | |
| | 0 | 118 | | ComplexComparableOperator.IsNotBetween when From is not null && To is not null => |
| | 0 | 119 | | Expression.OrElse( |
| | 0 | 120 | | Expression.LessThan(propertyValue, Expression.Constant(From.Value, typeof(TValue))), |
| | 0 | 121 | | Expression.GreaterThan(propertyValue, Expression.Constant(To.Value, typeof(TValue)))), |
| | 0 | 122 | | |
| | 0 | 123 | | _ => Expression.Constant(true) |
| | 0 | 124 | | }; |
| | | 125 | | |
| | | 126 | | // Combine: x.Property.HasValue && <comparison> |
| | 0 | 127 | | var body = Expression.AndAlso(hasValue, comparison); |
| | | 128 | | |
| | 0 | 129 | | return Expression.Lambda<Func<T, bool>>(body, parameter); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Resets the filter to its default state by clearing From and To values. |
| | | 134 | | /// </summary> |
| | | 135 | | public override void Reset() |
| | | 136 | | { |
| | 0 | 137 | | From = null; |
| | 0 | 138 | | To = null; |
| | 0 | 139 | | } |
| | | 140 | | } |
| | | 141 | | |