| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="StringListEditionViewModel.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.Linq; |
| | | 10 | | using MyNet.Observable; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.Crud; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides an editable list of strings with add/remove row commands. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Initializes a new instance of the <see cref="StringListEditionViewModel"/> class. |
| | | 20 | | /// </remarks> |
| | | 21 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | 12 | 22 | | public sealed class StringListEditionViewModel(ICommandFactory? commandFactory = null) : CollectionEditionViewModel<Stri |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the number of non-empty values in <see cref="CollectionEditionViewModel{TRow}.Items"/>. |
| | | 26 | | /// </summary> |
| | 3 | 27 | | public int Count => Items.Count(x => !string.IsNullOrWhiteSpace(x.Value)); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Loads values into the editor. |
| | | 31 | | /// Ensures there is always at least one editable row. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="values">The values to edit.</param> |
| | 12 | 34 | | public void Load(IEnumerable<string>? values) => LoadItems(values?.Select(x => new StringListItemViewModel { Value = |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Returns non-empty values from <see cref="CollectionEditionViewModel{TRow}.Items"/>. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <returns>The current edited values.</returns> |
| | | 40 | | public IReadOnlyCollection<string> GetValues() |
| | 6 | 41 | | => [.. Items.Select(x => x.Value).Where(x => !string.IsNullOrWhiteSpace(x)).Cast<string>()]; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Replaces the target collection content with current edited values. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="target">The target collection to update.</param> |
| | | 47 | | public void ApplyTo(ICollection<string> target) |
| | | 48 | | { |
| | 3 | 49 | | ArgumentNullException.ThrowIfNull(target); |
| | | 50 | | |
| | 3 | 51 | | target.Clear(); |
| | | 52 | | |
| | 18 | 53 | | foreach (var value in GetValues()) |
| | 6 | 54 | | target.Add(value); |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 3 | 58 | | protected override StringListItemViewModel CreateNewItem() => new(); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | protected override bool CanAdd(StringListItemViewModel? item) |
| | 6 | 62 | | => item is not null |
| | 6 | 63 | | && Items.IndexOf(item) == Items.Count - 1 |
| | 6 | 64 | | && !string.IsNullOrWhiteSpace(item.Value); |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | protected override bool CanRemove(StringListItemViewModel? item) |
| | 6 | 68 | | => item is not null && (Items.Count > 1 || !string.IsNullOrWhiteSpace(item.Value)); |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | protected override void EnsureEditableRow() |
| | | 72 | | { |
| | 15 | 73 | | if (!Items.Any()) |
| | 0 | 74 | | Items.Add(new()); |
| | 15 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | protected override void OnItemsStateChanged() |
| | | 79 | | { |
| | 51 | 80 | | (AddCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 51 | 81 | | (RemoveCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 51 | 82 | | NotifyPropertyChanged(nameof(Count)); |
| | 51 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Represents one editable string row. |
| | | 88 | | /// </summary> |
| | | 89 | | public sealed class StringListItemViewModel : ObservableObject |
| | | 90 | | { |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets or sets the row value. |
| | | 93 | | /// </summary> |
| | | 94 | | public string? Value { get; set; } |
| | | 95 | | } |
| | | 96 | | |