| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DialogServiceListExtensions.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.Selection; |
| | | 10 | | using MyNet.UI.Commands; |
| | | 11 | | using MyNet.UI.Dialogs.ContentDialogs; |
| | | 12 | | using MyNet.UI.ViewModels.Dialog; |
| | | 13 | | using MyNet.UI.ViewModels.List; |
| | | 14 | | using MyNet.UI.ViewModels.List.Factories; |
| | | 15 | | using MyNet.UI.ViewModels.List.Selection; |
| | | 16 | | |
| | | 17 | | #pragma warning disable IDE0130 |
| | | 18 | | namespace MyNet.UI.Dialogs; |
| | | 19 | | #pragma warning restore IDE0130 |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Provides fluent helpers to create list-based dialogs from existing list view models or raw item sequences. |
| | | 23 | | /// </summary> |
| | | 24 | | public static class DialogServiceListExtensions |
| | | 25 | | { |
| | | 26 | | extension(IDialogService dialogService) |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// Creates a fluent builder for a list dialog backed by an existing list view model. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 32 | | /// <typeparam name="TList">The concrete list view model type.</typeparam> |
| | | 33 | | /// <param name="list">The list view model displayed by the dialog.</param> |
| | | 34 | | /// <param name="commandFactory">Optional command factory used by the dialog view model.</param> |
| | | 35 | | /// <returns>A typed fluent builder for the dialog.</returns> |
| | | 36 | | public IDialogBuilder<IReadOnlyCollection<T>> CreateList<T, TList>(TList list, ICommandFactory? commandFactory = |
| | | 37 | | where T : notnull |
| | | 38 | | where TList : IListViewModel<T> |
| | | 39 | | { |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(dialogService); |
| | 0 | 41 | | ArgumentNullException.ThrowIfNull(list); |
| | | 42 | | |
| | 0 | 43 | | return dialogService.Create(new ListDialogViewModel<T, TList>(list, commandFactory: commandFactory)); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Creates a fluent builder for a list dialog from a raw sequence of items. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 50 | | /// <param name="items">The items to display.</param> |
| | | 51 | | /// <param name="listOptions">Optional configuration for the created list view model.</param> |
| | | 52 | | /// <param name="commandFactory">Optional command factory used by the dialog view model.</param> |
| | | 53 | | /// <returns>A typed fluent builder for the dialog.</returns> |
| | | 54 | | public IDialogBuilder<IReadOnlyCollection<T>> CreateList<T>( |
| | | 55 | | IEnumerable<T> items, |
| | | 56 | | ListViewModelOptions<T>? listOptions = null, |
| | | 57 | | ICommandFactory? commandFactory = null) |
| | | 58 | | where T : notnull |
| | | 59 | | { |
| | 0 | 60 | | ArgumentNullException.ThrowIfNull(dialogService); |
| | 0 | 61 | | ArgumentNullException.ThrowIfNull(items); |
| | | 62 | | |
| | 0 | 63 | | var list = ListViewModelFactory.Create(items, listOptions); |
| | 0 | 64 | | return dialogService.Create(new ListDialogViewModel<T, ListViewModel<T>>(list, commandFactory: commandFactor |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a fluent builder for a selection dialog backed by an existing selectable list view model. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 71 | | /// <param name="list">The selectable list view model displayed by the dialog.</param> |
| | | 72 | | /// <param name="commandFactory">Optional command factory used by the dialog view model.</param> |
| | | 73 | | /// <returns>A typed fluent builder for the dialog.</returns> |
| | | 74 | | public IDialogBuilder<IReadOnlyCollection<T>> CreateSelection<T>(SelectableListViewModel<T> list, ICommandFactor |
| | | 75 | | where T : notnull |
| | | 76 | | { |
| | 0 | 77 | | ArgumentNullException.ThrowIfNull(dialogService); |
| | 0 | 78 | | ArgumentNullException.ThrowIfNull(list); |
| | | 79 | | |
| | 0 | 80 | | return dialogService.Create(new SelectionDialogViewModel<T>(list, commandFactory: commandFactory)); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Creates a fluent builder for a selection dialog from a raw sequence of items. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 87 | | /// <param name="items">The items to display.</param> |
| | | 88 | | /// <param name="selectionMode">The selection mode used by the generated list view model.</param> |
| | | 89 | | /// <param name="listOptions">Optional configuration for the created list view model.</param> |
| | | 90 | | /// <param name="commandFactory">Optional command factory used by the dialog view model.</param> |
| | | 91 | | /// <returns>A typed fluent builder for the dialog.</returns> |
| | | 92 | | public IDialogBuilder<IReadOnlyCollection<T>> CreateSelection<T>( |
| | | 93 | | IEnumerable<T> items, |
| | | 94 | | SelectionMode selectionMode = SelectionMode.Multiple, |
| | | 95 | | ListViewModelOptions<T>? listOptions = null, |
| | | 96 | | ICommandFactory? commandFactory = null) |
| | | 97 | | where T : notnull |
| | | 98 | | { |
| | 0 | 99 | | ArgumentNullException.ThrowIfNull(dialogService); |
| | 0 | 100 | | ArgumentNullException.ThrowIfNull(items); |
| | | 101 | | |
| | 0 | 102 | | var list = ListViewModelFactory.CreateSelection(items, listOptions, selectionMode); |
| | 0 | 103 | | return dialogService.Create(new SelectionDialogViewModel<T>(list, commandFactory: commandFactory)); |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |