| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ExtendedCollectionBuilder.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.ComponentModel; |
| | | 10 | | using System.Linq.Expressions; |
| | | 11 | | using System.Reactive.Concurrency; |
| | | 12 | | using DynamicData; |
| | | 13 | | using MyNet.Observable.Collections.Filters; |
| | | 14 | | using MyNet.Observable.Collections.Grouping; |
| | | 15 | | using MyNet.Observable.Collections.Selection; |
| | | 16 | | using MyNet.Observable.Collections.Sorting; |
| | | 17 | | using MyNet.Observable.Collections.Sources; |
| | | 18 | | using MyNet.Primitives.Providers; |
| | | 19 | | |
| | | 20 | | namespace MyNet.Observable.Collections; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Provides a fluent API for constructing an <see cref="ExtendedCollection{T}"/> instance with specified source, filter |
| | | 24 | | /// </summary> |
| | | 25 | | /// <typeparam name="T">The type of elements in the collection.</typeparam> |
| | | 26 | | public sealed class ExtendedCollectionBuilder<T> |
| | | 27 | | where T : notnull |
| | | 28 | | { |
| | 9 | 29 | | private readonly FilterBuilder<T> _filterBuilder = FilterBuilder<T>.Create(); |
| | 9 | 30 | | private readonly SortingBuilder<T> _sortingBuilder = SortingBuilder<T>.Create(); |
| | 9 | 31 | | private readonly GroupingBuilder<T> _groupingBuilder = GroupingBuilder<T>.Create(); |
| | 9 | 32 | | private Func<SourceEngine<T>> _sourceFactory = SourceEngine<T>.Empty; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Defines the source of items for the collection. The provided items will be loaded into the collection at constru |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="items">The initial set of items for the collection.</param> |
| | | 38 | | /// <param name="isReadOnly">Determines whether the collection operates in read-only mode.</param> |
| | | 39 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 40 | | public ExtendedCollectionBuilder<T> From(IEnumerable<T> items, bool isReadOnly = false) |
| | | 41 | | { |
| | 9 | 42 | | _sourceFactory = () => SourceEngine<T>.From(items, isReadOnly); |
| | 9 | 43 | | return this; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Defines the source of items for the collection using a custom provider. The provided items will be loaded into t |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="source">The observable source of change sets for the collection.</param> |
| | | 50 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 51 | | public ExtendedCollectionBuilder<T> FromObservable(IObservable<IChangeSet<T>> source) |
| | | 52 | | { |
| | 0 | 53 | | _sourceFactory = () => SourceEngine<T>.FromObservable(source); |
| | 0 | 54 | | return this; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Defines the source of items for the collection using a snapshot provider. The provided factory function will be |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="factory">A function that provides a snapshot of the collection's state.</param> |
| | | 61 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 62 | | public ExtendedCollectionBuilder<T> FromSnapshot(Func<IEnumerable<T>> factory) |
| | | 63 | | { |
| | 0 | 64 | | _sourceFactory = () => SourceEngine<T>.FromSnapshot(factory); |
| | 0 | 65 | | return this; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Defines the source of items for the collection using a custom provider. The provided provider will be used to re |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="provider">The custom provider that will be used to retrieve the initial set of items for the collec |
| | | 72 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 73 | | public ExtendedCollectionBuilder<T> FromProvider(IItemsProvider<T> provider) |
| | | 74 | | { |
| | 0 | 75 | | _sourceFactory = () => SourceEngine<T>.FromProvider(provider); |
| | 0 | 76 | | return this; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Defines the source of items for the collection using a custom provider. The provided factory function will be in |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="predicate">A lambda expression that defines the filter criteria.</param> |
| | | 83 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 84 | | public ExtendedCollectionBuilder<T> Where(Expression<Func<T, bool>> predicate) |
| | | 85 | | { |
| | 3 | 86 | | _filterBuilder.Where(predicate); |
| | 3 | 87 | | return this; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Defines a logical AND grouping of filter criteria for the collection. The provided predicate will be combined wi |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="predicate">A lambda expression that defines the filter criteria for the AND operation.</param> |
| | | 94 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 95 | | public ExtendedCollectionBuilder<T> And(Expression<Func<T, bool>> predicate) |
| | | 96 | | { |
| | 0 | 97 | | _filterBuilder.And(predicate); |
| | 0 | 98 | | return this; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Defines a logical OR grouping of filter criteria for the collection. The provided predicate will be combined wit |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="predicate">A lambda expression that defines the filter criteria for the OR operation.</param> |
| | | 105 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 106 | | public ExtendedCollectionBuilder<T> Or(Expression<Func<T, bool>> predicate) |
| | | 107 | | { |
| | 0 | 108 | | _filterBuilder.Or(predicate); |
| | 0 | 109 | | return this; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Defines a logical AND grouping of filter criteria for the collection. The provided group function allows for the |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="group">A function that defines the nested set of filter criteria.</param> |
| | | 116 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 117 | | public ExtendedCollectionBuilder<T> And(Func<FilterBuilder<T>, FilterBuilder<T>> group) |
| | | 118 | | { |
| | 0 | 119 | | _filterBuilder.And(group); |
| | 0 | 120 | | return this; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Defines a logical OR grouping of filter criteria for the collection. The provided group function allows for the |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="group">A function that defines the nested set of filter criteria.</param> |
| | | 127 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 128 | | public ExtendedCollectionBuilder<T> Or(Func<FilterBuilder<T>, FilterBuilder<T>> group) |
| | | 129 | | { |
| | 0 | 130 | | _filterBuilder.Or(group); |
| | 0 | 131 | | return this; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Defines the primary sorting key for the collection. The provided key expression will be used to sort the items i |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="key">A lambda expression that defines the key for sorting.</param> |
| | | 138 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 139 | | public ExtendedCollectionBuilder<T> OrderBy(Expression<Func<T, object?>> key) |
| | | 140 | | { |
| | 3 | 141 | | _sortingBuilder.Add(key, ListSortDirection.Ascending); |
| | 3 | 142 | | return this; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Defines the primary sorting criterion for the collection, specifying that the items should be sorted in descendi |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="key">A lambda expression that defines the key for sorting.</param> |
| | | 149 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 150 | | public ExtendedCollectionBuilder<T> OrderByDescending(Expression<Func<T, object?>> key) |
| | | 151 | | { |
| | 0 | 152 | | _sortingBuilder.Add(key, ListSortDirection.Descending); |
| | 0 | 153 | | return this; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Adds a secondary sorting key to the collection. The provided key will be used for sorting items that are conside |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="key">A lambda expression that defines the key for sorting.</param> |
| | | 160 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 161 | | public ExtendedCollectionBuilder<T> ThenBy(Expression<Func<T, object?>> key) |
| | | 162 | | { |
| | 0 | 163 | | _sortingBuilder.ThenBy(key); |
| | 0 | 164 | | return this; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Adds a secondary sorting criterion to the collection, allowing for multi-level sorting. The provided key express |
| | | 169 | | /// </summary> |
| | | 170 | | /// <param name="key">A lambda expression that defines the key for sorting.</param> |
| | | 171 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 172 | | public ExtendedCollectionBuilder<T> ThenByDescending(Expression<Func<T, object?>> key) |
| | | 173 | | { |
| | 0 | 174 | | _sortingBuilder.ThenByDescending(key); |
| | 0 | 175 | | return this; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Adds a grouping key to the collection. Items will be grouped by the provided expression. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="key">A lambda expression that defines the grouping key.</param> |
| | | 182 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 183 | | public ExtendedCollectionBuilder<T> GroupBy(Expression<Func<T, object?>> key) |
| | | 184 | | { |
| | 0 | 185 | | _groupingBuilder.Add(key); |
| | 0 | 186 | | return this; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Adds a secondary grouping key using a fluent ThenBy style. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="key">A lambda expression that defines the secondary grouping key.</param> |
| | | 193 | | /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns> |
| | | 194 | | public ExtendedCollectionBuilder<T> ThenGroupBy(Expression<Func<T, object?>> key) |
| | | 195 | | { |
| | 0 | 196 | | _groupingBuilder.ThenBy(key); |
| | 0 | 197 | | return this; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Builds the <see cref="ExtendedCollection{T}"/> instance based on the defined source, filter, and sorting configu |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="scheduler">The scheduler to control the execution context for collection updates.</param> |
| | | 204 | | /// <returns>The constructed <see cref="ExtendedCollection{T}"/> instance.</returns> |
| | | 205 | | public ExtendedCollection<T> Build(IScheduler? scheduler = null) |
| | | 206 | | { |
| | 3 | 207 | | var source = _sourceFactory.Invoke(); |
| | | 208 | | |
| | 3 | 209 | | var collection = new ExtendedCollection<T>(source, scheduler); |
| | | 210 | | |
| | | 211 | | // Apply filter |
| | 3 | 212 | | var filter = _filterBuilder.Build(); |
| | 3 | 213 | | if (filter is not null) |
| | 3 | 214 | | collection.SetFilter(filter); |
| | | 215 | | |
| | | 216 | | // Apply sorting |
| | 3 | 217 | | var sorting = _sortingBuilder.Build(); |
| | 3 | 218 | | collection.SetSorting([.. sorting]); |
| | | 219 | | |
| | | 220 | | // Apply grouping |
| | 3 | 221 | | var grouping = _groupingBuilder.Build(); |
| | 3 | 222 | | if (grouping.Count > 0) |
| | 0 | 223 | | collection.SetGrouping([.. grouping]); |
| | | 224 | | |
| | 3 | 225 | | return collection; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Builds a <see cref="SelectableCollection{T}"/> instance based on the defined source, filter, and sorting configu |
| | | 230 | | /// </summary> |
| | | 231 | | /// <param name="selectionMode">The selection mode for the collection.</param> |
| | | 232 | | /// <param name="scheduler">The scheduler to control the execution context for collection updates.</param> |
| | | 233 | | /// <returns>The constructed <see cref="SelectableCollection{T}"/> instance.</returns> |
| | | 234 | | public SelectableCollection<T> BuildSelectable(SelectionMode selectionMode = SelectionMode.Multiple, IScheduler? sch |
| | | 235 | | { |
| | 6 | 236 | | var source = _sourceFactory.Invoke(); |
| | | 237 | | |
| | 6 | 238 | | var collection = new ExtendedCollection<T>(source, scheduler); |
| | | 239 | | |
| | | 240 | | // Apply filter |
| | 6 | 241 | | var filter = _filterBuilder.Build(); |
| | 6 | 242 | | if (filter is not null) |
| | 0 | 243 | | collection.SetFilter(filter); |
| | | 244 | | |
| | | 245 | | // Apply sorting |
| | 6 | 246 | | var sorting = _sortingBuilder.Build(); |
| | 6 | 247 | | collection.SetSorting([.. sorting]); |
| | | 248 | | |
| | | 249 | | // Apply grouping |
| | 6 | 250 | | var grouping = _groupingBuilder.Build(); |
| | 6 | 251 | | if (grouping.Count > 0) |
| | 0 | 252 | | collection.SetGrouping([.. grouping]); |
| | | 253 | | |
| | 6 | 254 | | return new(collection, selectionMode, disposeCollection: true); |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | |