| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FilterConditionViewModel.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.Observable; |
| | | 10 | | |
| | | 11 | | namespace MyNet.UI.ViewModels.List.Filtering; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents a view model for a filter condition that can be applied to items of type T. It includes a key to identify |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="key">The key that identifies this filter condition.</param> |
| | | 17 | | /// <param name="isReadOnly">A value indicating whether this filter condition is read-only.</param> |
| | | 18 | | /// <typeparam name="T">The type of the items being filtered.</typeparam> |
| | 6 | 19 | | public abstract class FilterConditionViewModel<T>(string key, bool isReadOnly = false) : ObservableObject, IFilterCondit |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the key that identifies this filter condition. |
| | | 23 | | /// </summary> |
| | 6 | 24 | | public string Key { get; } = key; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets a value indicating whether this filter condition is read-only. |
| | | 28 | | /// </summary> |
| | 6 | 29 | | public bool IsReadOnly { get; } = isReadOnly; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets a value indicating whether this filter condition is enabled. When disabled, the condition is ignore |
| | | 33 | | /// </summary> |
| | | 34 | | public bool IsEnabled |
| | | 35 | | { |
| | | 36 | | get; |
| | 0 | 37 | | set => SetProperty(ref field, value); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets a value indicating whether this filter condition is empty, meaning it has no criteria defined and should no |
| | | 42 | | /// </summary> |
| | | 43 | | public abstract bool IsEmpty { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Builds the expression representing this filter condition. If the condition is empty, it returns an expression th |
| | | 47 | | /// </summary> |
| | | 48 | | /// <returns>An expression representing the filter condition.</returns> |
| | 0 | 49 | | public Expression<Func<T, bool>>? BuildExpression() => IsEmpty || !IsEnabled ? null : BuildExpressionCore(); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// When overridden in a derived class, builds the specific expression representing this filter condition based on i |
| | | 53 | | /// </summary> |
| | | 54 | | /// <returns>An expression representing the specific filter condition.</returns> |
| | | 55 | | protected abstract Expression<Func<T, bool>> BuildExpressionCore(); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Resets the filter condition to its default state. This method should be implemented by derived classes to clear |
| | | 59 | | /// </summary> |
| | | 60 | | public abstract void Reset(); |
| | | 61 | | } |
| | | 62 | | |