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