| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="StringFilterViewModel.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 | | public class StringFilterViewModel<T>( |
| | | 14 | | string propertyName, |
| | | 15 | | Expression<Func<T, string?>> property, |
| | | 16 | | StringOperator filterMode = StringOperator.Contains, |
| | | 17 | | bool caseSensitive = false) |
| | 6 | 18 | | : FilterConditionViewModel<T>(propertyName) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the expression representing the string property to filter on. |
| | | 22 | | /// </summary> |
| | 6 | 23 | | public Expression<Func<T, string?>> Property { get; } = property; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets the string comparison operator used for matching. |
| | | 27 | | /// </summary> |
| | 6 | 28 | | public StringOperator Operator { get; set => SetProperty(ref field, value); } = filterMode; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets or sets the search value to match against. |
| | | 32 | | /// When null or empty, the filter matches all items (empty filter). |
| | | 33 | | /// </summary> |
| | 0 | 34 | | public string? Value { get; set => SetProperty(ref field, value); } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets a value indicating whether the string comparison should be case-sensitive. |
| | | 38 | | /// When false (default), comparisons are case-insensitive. |
| | | 39 | | /// </summary> |
| | 6 | 40 | | public bool CaseSensitive { get; set => SetProperty(ref field, value); } = caseSensitive; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets a value indicating whether this filter is in an empty state. |
| | | 44 | | /// A string filter is empty when <see cref="Value"/> is null or empty. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public override bool IsEmpty => string.IsNullOrEmpty(Value); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Builds the expression representing this string filter condition based on the specified operator, value, and case |
| | | 50 | | /// </summary> |
| | | 51 | | /// <returns>An expression representing the filter condition.</returns> |
| | | 52 | | /// <exception cref="NotSupportedException">Thrown when the specified operator is not supported.</exception> |
| | | 53 | | protected override Expression<Func<T, bool>> BuildExpressionCore() |
| | | 54 | | { |
| | 0 | 55 | | var parameter = Property.Parameters[0]; |
| | 0 | 56 | | var property = Property.Body; |
| | | 57 | | |
| | | 58 | | // Ensure it's string |
| | 0 | 59 | | var propertyAsString = property.Type == typeof(string) |
| | 0 | 60 | | ? property |
| | 0 | 61 | | : Expression.Call(property, nameof(ToString), Type.EmptyTypes); |
| | | 62 | | |
| | 0 | 63 | | var value = Expression.Constant(Value, typeof(string)); |
| | | 64 | | |
| | 0 | 65 | | var left = propertyAsString; |
| | 0 | 66 | | Expression right = value; |
| | | 67 | | |
| | | 68 | | // Handle case sensitivity |
| | 0 | 69 | | if (!CaseSensitive) |
| | | 70 | | { |
| | 0 | 71 | | var toUpper = typeof(string).GetMethod(nameof(string.ToUpper), Type.EmptyTypes)!; |
| | | 72 | | |
| | 0 | 73 | | left = Expression.Condition( |
| | 0 | 74 | | Expression.Equal(propertyAsString, Expression.Constant(null, typeof(string))), |
| | 0 | 75 | | Expression.Constant(null, typeof(string)), |
| | 0 | 76 | | Expression.Call(propertyAsString, toUpper)); |
| | | 77 | | |
| | 0 | 78 | | right = Expression.Call(value, toUpper); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // Handle null property |
| | 0 | 82 | | var notNull = Expression.NotEqual(propertyAsString, Expression.Constant(null, typeof(string))); |
| | | 83 | | |
| | 0 | 84 | | Expression body = Operator switch |
| | 0 | 85 | | { |
| | 0 | 86 | | StringOperator.Contains => |
| | 0 | 87 | | Expression.AndAlso( |
| | 0 | 88 | | notNull, |
| | 0 | 89 | | Expression.Call(left, nameof(string.Contains), Type.EmptyTypes, right)), |
| | 0 | 90 | | |
| | 0 | 91 | | StringOperator.StartsWith => |
| | 0 | 92 | | Expression.AndAlso( |
| | 0 | 93 | | notNull, |
| | 0 | 94 | | Expression.Call(left, nameof(string.StartsWith), Type.EmptyTypes, right)), |
| | 0 | 95 | | |
| | 0 | 96 | | StringOperator.EndsWith => |
| | 0 | 97 | | Expression.AndAlso( |
| | 0 | 98 | | notNull, |
| | 0 | 99 | | Expression.Call(left, nameof(string.EndsWith), Type.EmptyTypes, right)), |
| | 0 | 100 | | |
| | 0 | 101 | | StringOperator.Is => |
| | 0 | 102 | | Expression.Equal(left, right), |
| | 0 | 103 | | |
| | 0 | 104 | | StringOperator.IsNot => |
| | 0 | 105 | | Expression.NotEqual(left, right), |
| | 0 | 106 | | |
| | 0 | 107 | | _ => throw new NotSupportedException($"Operator {Operator} is not supported") |
| | 0 | 108 | | }; |
| | | 109 | | |
| | 0 | 110 | | return Expression.Lambda<Func<T, bool>>(body, parameter); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Resets the filter to its default state by clearing the value. |
| | | 115 | | /// </summary> |
| | 0 | 116 | | public override void Reset() => Value = null; |
| | | 117 | | } |
| | | 118 | | |