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