| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BooleanFilterViewModel.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 | | |
| | | 10 | | namespace MyNet.UI.ViewModels.List.Filtering.Filters; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Filter view model for boolean properties. |
| | | 14 | | /// Supports nullable boolean values with optional null value filtering. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | | 17 | | /// <param name="propertyName">The key that identifies this filter condition.</param> |
| | | 18 | | /// <param name="property">The expression representing the boolean property to filter on.</param> |
| | | 19 | | /// <param name="allowNullValue">Whether null values are allowed. When true, Reset sets Value to null instead of false.< |
| | | 20 | | public class BooleanFilterViewModel<T>( |
| | | 21 | | string propertyName, |
| | | 22 | | Expression<Func<T, bool?>> property, |
| | | 23 | | bool allowNullValue = false) |
| | 0 | 24 | | : FilterConditionViewModel<T>(propertyName) |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the expression representing the boolean property to filter on. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public Expression<Func<T, bool?>> Property { get; } = property; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the boolean value to filter by. |
| | | 33 | | /// When null, the filter is considered empty (unless AllowNullValue is true). |
| | | 34 | | /// </summary> |
| | 0 | 35 | | public bool? Value { get; set => SetProperty(ref field, value); } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets or sets a value indicating whether null values are allowed in the filter. |
| | | 39 | | /// When true, <see cref="Reset"/> sets Value to null instead of false. |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public bool AllowNullValue { get; set => SetProperty(ref field, value); } = allowNullValue; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets a value indicating whether this filter is in an empty state. |
| | | 45 | | /// A boolean filter is empty when <see cref="Value"/> is null. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public override bool IsEmpty => Value is null; |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | protected override Expression<Func<T, bool>> BuildExpressionCore() |
| | | 51 | | { |
| | 0 | 52 | | var parameter = Property.Parameters[0]; |
| | 0 | 53 | | var propertyBody = Property.Body; |
| | 0 | 54 | | var value = Expression.Constant(Value, typeof(bool?)); |
| | | 55 | | |
| | 0 | 56 | | var body = Expression.Equal(propertyBody, value); |
| | | 57 | | |
| | 0 | 58 | | return Expression.Lambda<Func<T, bool>>(body, parameter); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Resets the filter to its default state. |
| | | 63 | | /// If <see cref="AllowNullValue"/> is true, sets Value to null; otherwise, sets it to null (empty filter). |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public override void Reset() => Value = null; |
| | | 66 | | } |
| | | 67 | | |