| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GroupingPropertyViewModel.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 | | using MyNet.Observable.Collections.Grouping; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.List.Grouping; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents a view model for a grouping property that can be used to configure how a collection is grouped. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="expression">The expression used to access the property to group by.</param> |
| | | 19 | | /// <param name="key">The unique identifier for the grouping property.</param> |
| | | 20 | | /// <param name="displayName">The provider for the localized display name.</param> |
| | | 21 | | /// <typeparam name="T">The type of the items being grouped.</typeparam> |
| | 0 | 22 | | public class GroupingPropertyViewModel<T>( |
| | 0 | 23 | | Expression<Func<T, object?>> expression, |
| | 0 | 24 | | string key, |
| | 0 | 25 | | IObservableValue<string> displayName) |
| | | 26 | | : ObservableObject, IGroupingPropertyViewModel<T> |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the localized display name for this grouping property. |
| | | 30 | | /// </summary> |
| | 0 | 31 | | public IObservableValue<string> DisplayName { get; } = displayName; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the unique identifier for the grouping property. This key is used to identify the grouping property within |
| | | 35 | | /// </summary> |
| | 0 | 36 | | string IGroupingPropertyViewModel<T>.Key { get; } = key; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the expression that defines the grouping property. This expression is used to determine the value of the pr |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public Expression<Func<T, object?>> Expression { get; } = expression; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets a value indicating whether this grouping property is currently enabled/active. |
| | | 45 | | /// When true, this property contributes to the collection's grouping. |
| | | 46 | | /// When false, this property is available but not applied. |
| | | 47 | | /// </summary> |
| | | 48 | | public bool IsEnabled |
| | | 49 | | { |
| | | 50 | | get; |
| | | 51 | | set |
| | | 52 | | { |
| | 0 | 53 | | if (!SetProperty(ref field, value)) |
| | 0 | 54 | | return; |
| | | 55 | | |
| | 0 | 56 | | OnIsEnabledChanged(); |
| | 0 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | = true; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the date and time when this grouping property was activated (enabled). This property is null if the groupin |
| | | 64 | | /// </summary> |
| | | 65 | | public DateTime? ActivatedAt |
| | | 66 | | { |
| | | 67 | | get; |
| | 0 | 68 | | private set => SetProperty(ref field, value); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Builds the core grouping property based on the current state of the view model. If the grouping property is acti |
| | | 73 | | /// </summary> |
| | | 74 | | /// <returns>An instance of <see cref="IGroupingProperty{T}"/> if the grouping property is active; otherwise, null.< |
| | 0 | 75 | | public IGroupingProperty<T> Build() => new ExpressionGroupingProperty<T>(Expression); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Determines whether the provided grouping property matches the expression of this view model. This method compare |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="property">The grouping property to compare with this view model.</param> |
| | | 81 | | /// <returns>True if the expressions match; otherwise, false.</returns> |
| | 0 | 82 | | public bool Matches(IGroupingProperty<T> property) => Expression.IsSimilar(property.ProvideExpression()); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Handles changes to the IsEnabled property. When the IsEnabled property changes, this method updates the Activate |
| | | 86 | | /// </summary> |
| | 0 | 87 | | protected virtual void OnIsEnabledChanged() => ActivatedAt = IsEnabled ? DateTime.UtcNow : null; |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | 0 | 90 | | public override string ToString() => DisplayName.Value.OrEmpty(); |
| | | 91 | | } |
| | | 92 | | |