| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ListViewModelFactory.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 MyNet.Observable.Collections; |
| | | 10 | | using MyNet.Observable.Collections.Selection; |
| | | 11 | | using MyNet.Observable.Collections.Sources; |
| | | 12 | | using MyNet.UI.ViewModels.List.Selection; |
| | | 13 | | using MyNet.UI.ViewModels.List.Services; |
| | | 14 | | |
| | | 15 | | namespace MyNet.UI.ViewModels.List.Factories; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Provides simple factory methods to create list view models with optional shared configuration. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class ListViewModelFactory |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Creates a new instance of <see cref="ListViewModel{T}"/> with the specified data provider and optional configura |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="provider">The data provider for the list view model.</param> |
| | | 26 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 27 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 28 | | /// <returns>A new instance of <see cref="ListViewModel{T}"/>.</returns> |
| | | 29 | | public static ListViewModel<T> Create<T>(IListDataProvider<T> provider, ListViewModelOptions<T>? options = null) |
| | | 30 | | where T : notnull |
| | | 31 | | { |
| | 0 | 32 | | ArgumentNullException.ThrowIfNull(provider); |
| | | 33 | | |
| | 0 | 34 | | return new(provider, options); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Creates a new instance of <see cref="CrudListViewModel{T}"/> with the specified data provider, CRUD service and |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="provider">The data provider for the list view model.</param> |
| | | 41 | | /// <param name="crudService">The CRUD service used by the view model commands.</param> |
| | | 42 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 43 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 44 | | /// <returns>A new instance of <see cref="CrudListViewModel{T}"/>.</returns> |
| | | 45 | | public static CrudListViewModel<T> CreateCrud<T>( |
| | | 46 | | IListDataProvider<T> provider, |
| | | 47 | | ICrudService<T> crudService, |
| | | 48 | | ListViewModelOptions<T>? options = null) |
| | | 49 | | where T : notnull |
| | | 50 | | { |
| | 9 | 51 | | ArgumentNullException.ThrowIfNull(provider); |
| | 6 | 52 | | ArgumentNullException.ThrowIfNull(crudService); |
| | | 53 | | |
| | 3 | 54 | | return new(provider, crudService, options); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Creates a new instance of <see cref="ListViewModel{T}"/> with the specified items and optional configuration. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="items">The items to be displayed in the list view model.</param> |
| | | 61 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 62 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 63 | | /// <returns>A new instance of <see cref="ListViewModel{T}"/>.</returns> |
| | | 64 | | public static ListViewModel<T> Create<T>( |
| | | 65 | | IEnumerable<T> items, |
| | | 66 | | ListViewModelOptions<T>? options = null) |
| | | 67 | | where T : notnull |
| | | 68 | | { |
| | 0 | 69 | | ArgumentNullException.ThrowIfNull(items); |
| | | 70 | | |
| | 0 | 71 | | var source = SourceEngine<T>.From(items, readOnly: false); |
| | 0 | 72 | | return new(source, options); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Creates a new instance of <see cref="CrudListViewModel{T}"/> with the specified items, CRUD service and optional |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="items">The items to be displayed in the list view model.</param> |
| | | 79 | | /// <param name="crudService">The CRUD service used by the view model commands.</param> |
| | | 80 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 81 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 82 | | /// <returns>A new instance of <see cref="CrudListViewModel{T}"/>.</returns> |
| | | 83 | | public static CrudListViewModel<T> CreateCrud<T>( |
| | | 84 | | IEnumerable<T> items, |
| | | 85 | | ICrudService<T> crudService, |
| | | 86 | | ListViewModelOptions<T>? options = null) |
| | | 87 | | where T : notnull |
| | | 88 | | { |
| | 6 | 89 | | ArgumentNullException.ThrowIfNull(items); |
| | 3 | 90 | | ArgumentNullException.ThrowIfNull(crudService); |
| | | 91 | | |
| | 3 | 92 | | var source = SourceEngine<T>.From(items, readOnly: false); |
| | 3 | 93 | | var provider = new ExtendedCollectionDataProvider<T>(new(source, options?.Scheduler)); |
| | | 94 | | |
| | 3 | 95 | | return new(provider, crudService, options); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Creates a new instance of <see cref="VirtualizedListViewModel{T}"/> with the specified data provider and optiona |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="provider">The data provider for the list view model.</param> |
| | | 102 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 103 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 104 | | /// <returns>A new instance of <see cref="VirtualizedListViewModel{T}"/>.</returns> |
| | | 105 | | public static VirtualizedListViewModel<T> CreateVirtualized<T>( |
| | | 106 | | IListDataProvider<T> provider, |
| | | 107 | | ListViewModelOptions<T>? options = null) |
| | | 108 | | where T : notnull |
| | | 109 | | { |
| | 6 | 110 | | ArgumentNullException.ThrowIfNull(provider); |
| | | 111 | | |
| | 3 | 112 | | return new(provider, options); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Creates a new instance of <see cref="VirtualizedListViewModel{T}"/> with the specified items and optional config |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="items">The items to be displayed in the list view model.</param> |
| | | 119 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 120 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 121 | | /// <returns>A new instance of <see cref="VirtualizedListViewModel{T}"/>.</returns> |
| | | 122 | | public static VirtualizedListViewModel<T> CreateVirtualized<T>( |
| | | 123 | | IEnumerable<T> items, |
| | | 124 | | ListViewModelOptions<T>? options = null) |
| | | 125 | | where T : notnull |
| | | 126 | | { |
| | 6 | 127 | | ArgumentNullException.ThrowIfNull(items); |
| | | 128 | | |
| | 3 | 129 | | var source = SourceEngine<T>.From(items, readOnly: false); |
| | 3 | 130 | | var provider = new ExtendedCollectionDataProvider<T>(new(source, options?.Scheduler)); |
| | | 131 | | |
| | 3 | 132 | | return new(provider, options); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Creates a new instance of <see cref="SelectableListViewModel{T}"/> with the specified items and optional configu |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="items">The items to be displayed in the list view model.</param> |
| | | 139 | | /// <param name="options">Optional configuration for the list view model.</param> |
| | | 140 | | /// <param name="selectionMode">The selection mode for the list view model.</param> |
| | | 141 | | /// <typeparam name="T">The type of items in the list view model.</typeparam> |
| | | 142 | | /// <returns>A new instance of <see cref="SelectableListViewModel{T}"/>.</returns> |
| | | 143 | | public static SelectableListViewModel<T> CreateSelection<T>( |
| | | 144 | | IEnumerable<T> items, |
| | | 145 | | ListViewModelOptions<T>? options = null, |
| | | 146 | | SelectionMode selectionMode = SelectionMode.Multiple) |
| | | 147 | | where T : notnull |
| | | 148 | | { |
| | 18 | 149 | | ArgumentNullException.ThrowIfNull(items); |
| | | 150 | | |
| | 18 | 151 | | options ??= new(); |
| | | 152 | | |
| | 18 | 153 | | var source = SourceEngine<T>.From(items, readOnly: false); |
| | 18 | 154 | | var collection = new ExtendedCollection<T>(source, options.Scheduler); |
| | | 155 | | |
| | 18 | 156 | | return new(collection, selectionMode, options); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Creates a selectable list view model from an existing extended collection. |
| | | 161 | | /// </summary> |
| | | 162 | | public static SelectableListViewModel<T> CreateSelection<T>( |
| | | 163 | | ExtendedCollection<T> collection, |
| | | 164 | | ListViewModelOptions<T>? options = null, |
| | | 165 | | SelectionMode selectionMode = SelectionMode.Multiple) |
| | | 166 | | where T : notnull |
| | | 167 | | { |
| | 3 | 168 | | ArgumentNullException.ThrowIfNull(collection); |
| | | 169 | | |
| | 3 | 170 | | return new(collection, selectionMode, options); |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | |