| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SortingPropertyViewModelBuilder.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.Primitives; |
| | | 12 | | using MyNet.UI.ViewModels.List.Grouping; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.ViewModels.List.Sorting; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Fluent builder used to configure a sorting property view model. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of the items to sort.</typeparam> |
| | | 20 | | public sealed class SortingPropertyViewModelBuilder<T>(Expression<Func<T, object?>> expression) |
| | | 21 | | { |
| | | 22 | | private string? _key; |
| | | 23 | | private IObservableValue<string>? _displayName; |
| | | 24 | | private SortingDefaultState? _defaultState; |
| | | 25 | | private int _order; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Sets the unique key of the sorting property. |
| | | 29 | | /// </summary> |
| | | 30 | | public SortingPropertyViewModelBuilder<T> WithKey(string key) |
| | | 31 | | { |
| | 0 | 32 | | _key = key; |
| | 0 | 33 | | return this; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Sets the display name provider used by the UI. |
| | | 38 | | /// </summary> |
| | | 39 | | public SortingPropertyViewModelBuilder<T> WithDisplayName(IObservableValue<string> displayName) |
| | | 40 | | { |
| | 0 | 41 | | _displayName = displayName; |
| | 0 | 42 | | return this; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Sets the display name using a resource key. |
| | | 47 | | /// </summary> |
| | 0 | 48 | | public SortingPropertyViewModelBuilder<T> WithDisplayName(string displayName) => WithDisplayName(new LocalizedString |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Sets the default sorting direction to ascending. This means that when this sorting property is applied, it will |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="order">The order in which this sorting property should be applied relative to other sorting propert |
| | | 54 | | /// <returns>The current instance of <see cref="SortingPropertyViewModelBuilder{T}"/> for fluent configuration.</ret |
| | | 55 | | public SortingPropertyViewModelBuilder<T> Ascending(int? order = null) |
| | | 56 | | { |
| | 0 | 57 | | _defaultState = new(ListSortDirection.Ascending, order ?? _order++); |
| | 0 | 58 | | return this; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Sets the default sorting direction to descending. This means that when this sorting property is applied, it will |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="order">The order in which this sorting property should be applied relative to other sorting propert |
| | | 65 | | /// <returns>The current instance of <see cref="SortingPropertyViewModelBuilder{T}"/> for fluent configuration.</ret |
| | | 66 | | public SortingPropertyViewModelBuilder<T> Descending(int? order = null) |
| | | 67 | | { |
| | 0 | 68 | | _defaultState = new(ListSortDirection.Descending, order ?? _order++); |
| | 0 | 69 | | return this; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Builds the sorting property configuration based on the current state of the builder. This method validates that |
| | | 74 | | /// </summary> |
| | | 75 | | /// <returns>The constructed <see cref="GroupingPropertyDefinition{T}"/> instance.</returns> |
| | | 76 | | /// <exception cref="InvalidOperationException">Thrown if a sorting property key has not been provided.</exception> |
| | | 77 | | internal SortingPropertyDefinition<T> Build() |
| | | 78 | | { |
| | 0 | 79 | | var key = ResolveKey(); |
| | 0 | 80 | | var displayName = _displayName ?? new LocalizedString(key); |
| | | 81 | | |
| | 0 | 82 | | return new(key, expression, displayName, _defaultState); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Resolves the key for the sorting property. If a key has been explicitly set using the builder, it returns that k |
| | | 87 | | /// </summary> |
| | | 88 | | /// <returns>The resolved key for the sorting property.</returns> |
| | | 89 | | /// <exception cref="InvalidOperationException">Thrown if a sorting property key cannot be resolved.</exception> |
| | | 90 | | private string ResolveKey() |
| | | 91 | | { |
| | 0 | 92 | | if (!string.IsNullOrWhiteSpace(_key)) |
| | 0 | 93 | | return _key; |
| | | 94 | | |
| | 0 | 95 | | var inferredKey = expression.PropertyName(); |
| | | 96 | | |
| | 0 | 97 | | return string.IsNullOrWhiteSpace(inferredKey) ? throw new InvalidOperationException("A sorting property key must |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Represents the configuration for a sorting property, including its key, expression, display name, and default sortin |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="Key">The unique key of the sorting property.</param> |
| | | 105 | | /// <param name="Expression">The expression used to extract the value for sorting.</param> |
| | | 106 | | /// <param name="DisplayName">The display name provider for the UI.</param> |
| | | 107 | | /// <param name="DefaultState">The default configuration for the sorting property, if any.</param> |
| | | 108 | | /// <typeparam name="T">The type of the items being sorted.</typeparam> |
| | | 109 | | internal sealed record SortingPropertyDefinition<T>(string Key, Expression<Func<T, object?>> Expression, IObservableValu |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Represents the active configuration for a sorting property, including its default sorting direction and the order in |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="Direction">The default sorting direction for the active sorting property.</param> |
| | | 115 | | /// <param name="Order">The order in which the sorting property was activated.</param> |
| | | 116 | | internal sealed record SortingDefaultState(ListSortDirection Direction, int Order); |
| | | 117 | | |