< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.BooleanFilterViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/BooleanFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 11
Coverable lines: 11
Total lines: 67
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
set_Value(...)100%210%
set_AllowNullValue(...)100%210%
get_IsEmpty()100%210%
BuildExpressionCore()100%210%
Reset()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/BooleanFilterViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="BooleanFilterViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Linq.Expressions;
 9
 10namespace 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.<
 20public class BooleanFilterViewModel<T>(
 21    string propertyName,
 22    Expression<Func<T, bool?>> property,
 23    bool allowNullValue = false)
 024    : FilterConditionViewModel<T>(propertyName)
 25{
 26    /// <summary>
 27    /// Gets the expression representing the boolean property to filter on.
 28    /// </summary>
 029    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>
 035    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>
 041    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>
 047    public override bool IsEmpty => Value is null;
 48
 49    /// <inheritdoc />
 50    protected override Expression<Func<T, bool>> BuildExpressionCore()
 51    {
 052        var parameter = Property.Parameters[0];
 053        var propertyBody = Property.Body;
 054        var value = Expression.Constant(Value, typeof(bool?));
 55
 056        var body = Expression.Equal(propertyBody, value);
 57
 058        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>
 065    public override void Reset() => Value = null;
 66}
 67