| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CollectionEditionViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Collections.ObjectModel; |
| | | 9 | | using System.Collections.Specialized; |
| | | 10 | | using System.ComponentModel; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Windows.Input; |
| | | 13 | | using MyNet.UI.Commands; |
| | | 14 | | |
| | | 15 | | namespace MyNet.UI.ViewModels.Crud; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Provides a reusable base implementation for editable collections. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <typeparam name="TRow">The edited row view model type.</typeparam> |
| | | 21 | | public abstract class CollectionEditionViewModel<TRow> : ViewModelBase, IEditionStateViewModel |
| | | 22 | | where TRow : class, INotifyPropertyChanged |
| | | 23 | | { |
| | | 24 | | private bool _isLoading; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="CollectionEditionViewModel{TRow}"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | 12 | 30 | | protected CollectionEditionViewModel(ICommandFactory? commandFactory = null) |
| | | 31 | | { |
| | 12 | 32 | | var commands = commandFactory.GetOrDefault(); |
| | | 33 | | |
| | 12 | 34 | | AddCommand = commands.Create<TRow>(Add, CanAdd); |
| | 12 | 35 | | RemoveCommand = commands.Create<TRow>(Remove, CanRemove); |
| | | 36 | | |
| | 12 | 37 | | Items.CollectionChanged += HandleCollectionChanged; |
| | 12 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the command that adds a row. |
| | | 42 | | /// </summary> |
| | | 43 | | public ICommand AddCommand { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the command that removes a row. |
| | | 47 | | /// </summary> |
| | | 48 | | public ICommand RemoveCommand { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the edited rows. |
| | | 52 | | /// </summary> |
| | 12 | 53 | | public ObservableCollection<TRow> Items { get; } = []; |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | 18 | 56 | | public bool IsDirty { get; private set => SetProperty(ref field, value); } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Replaces current rows with <paramref name="items"/> and marks the editor as clean. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="items">The rows to load.</param> |
| | | 62 | | protected void LoadItems(IEnumerable<TRow>? items) |
| | | 63 | | { |
| | 12 | 64 | | _isLoading = true; |
| | | 65 | | |
| | | 66 | | try |
| | | 67 | | { |
| | 12 | 68 | | UnsubscribeFromItems(Items); |
| | 12 | 69 | | Items.Clear(); |
| | | 70 | | |
| | 12 | 71 | | if (items is not null) |
| | | 72 | | { |
| | 66 | 73 | | foreach (var item in items) |
| | 21 | 74 | | Items.Add(item); |
| | | 75 | | } |
| | | 76 | | |
| | 12 | 77 | | EnsureEditableRow(); |
| | 12 | 78 | | SubscribeToItems(Items); |
| | 12 | 79 | | IsDirty = false; |
| | 12 | 80 | | OnItemsStateChanged(); |
| | 12 | 81 | | } |
| | | 82 | | finally |
| | | 83 | | { |
| | 12 | 84 | | _isLoading = false; |
| | 12 | 85 | | } |
| | 12 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Creates a new row to add to <see cref="Items"/>. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <returns>The new row instance.</returns> |
| | | 92 | | protected abstract TRow CreateNewItem(); |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Determines whether a row can trigger add. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="item">The source row.</param> |
| | | 98 | | /// <returns><see langword="true"/> when add is allowed.</returns> |
| | 0 | 99 | | protected virtual bool CanAdd(TRow? item) => item is not null; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Determines whether a row can be removed. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="item">The row to remove.</param> |
| | | 105 | | /// <returns><see langword="true"/> when remove is allowed.</returns> |
| | 0 | 106 | | protected virtual bool CanRemove(TRow? item) => item is not null; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Ensures editor constraints after a load/remove operation. |
| | | 110 | | /// </summary> |
| | | 111 | | protected virtual void EnsureEditableRow() |
| | | 112 | | { |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Called after row collection/state changed. |
| | | 117 | | /// </summary> |
| | | 118 | | protected virtual void OnItemsStateChanged() |
| | | 119 | | { |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Adds the specified item to <see cref="Items"/>. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="item">The item to add.</param> |
| | | 126 | | /// <returns><see langword="true"/> when the item was added; otherwise <see langword="false"/>.</returns> |
| | | 127 | | protected bool AddItem(TRow? item) |
| | | 128 | | { |
| | 0 | 129 | | if (item is null) |
| | 0 | 130 | | return false; |
| | | 131 | | |
| | 0 | 132 | | Items.Add(item); |
| | 0 | 133 | | return true; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Removes the specified item from <see cref="Items"/> when allowed. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="item">The item to remove.</param> |
| | | 140 | | /// <returns><see langword="true"/> when the item was removed; otherwise <see langword="false"/>.</returns> |
| | | 141 | | protected bool RemoveItem(TRow? item) |
| | | 142 | | { |
| | 3 | 143 | | if (!CanRemove(item) || item is null) |
| | 0 | 144 | | return false; |
| | | 145 | | |
| | 3 | 146 | | var removed = Items.Remove(item); |
| | | 147 | | |
| | 3 | 148 | | if (removed) |
| | 3 | 149 | | EnsureEditableRow(); |
| | | 150 | | |
| | 3 | 151 | | return removed; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private void Add(TRow? item) |
| | | 155 | | { |
| | 3 | 156 | | if (!CanAdd(item)) |
| | 0 | 157 | | return; |
| | | 158 | | |
| | 3 | 159 | | Items.Add(CreateNewItem()); |
| | 3 | 160 | | } |
| | | 161 | | |
| | 3 | 162 | | private void Remove(TRow? item) => _ = RemoveItem(item); |
| | | 163 | | |
| | | 164 | | private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
| | | 165 | | { |
| | 39 | 166 | | if (e.OldItems is not null) |
| | 3 | 167 | | UnsubscribeFromItems(e.OldItems.Cast<TRow>()); |
| | | 168 | | |
| | 39 | 169 | | if (e.NewItems is not null) |
| | 24 | 170 | | SubscribeToItems(e.NewItems.Cast<TRow>()); |
| | | 171 | | |
| | 39 | 172 | | MarkDirtyIfNeeded(); |
| | 39 | 173 | | OnItemsStateChanged(); |
| | 39 | 174 | | } |
| | | 175 | | |
| | | 176 | | private void HandleItemPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 177 | | { |
| | 0 | 178 | | MarkDirtyIfNeeded(); |
| | 0 | 179 | | OnItemsStateChanged(); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | private void MarkDirtyIfNeeded() |
| | | 183 | | { |
| | 39 | 184 | | if (!_isLoading) |
| | 6 | 185 | | IsDirty = true; |
| | 39 | 186 | | } |
| | | 187 | | |
| | | 188 | | private void SubscribeToItems(IEnumerable<TRow> items) |
| | | 189 | | { |
| | 162 | 190 | | foreach (var item in items) |
| | 45 | 191 | | item.PropertyChanged += HandleItemPropertyChanged; |
| | 36 | 192 | | } |
| | | 193 | | |
| | | 194 | | private void UnsubscribeFromItems(IEnumerable<TRow> items) |
| | | 195 | | { |
| | 36 | 196 | | foreach (var item in items) |
| | 3 | 197 | | item.PropertyChanged -= HandleItemPropertyChanged; |
| | 15 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <inheritdoc /> |
| | | 201 | | protected override void DisposeManagedResources() |
| | | 202 | | { |
| | 0 | 203 | | Items.CollectionChanged -= HandleCollectionChanged; |
| | 0 | 204 | | UnsubscribeFromItems(Items); |
| | 0 | 205 | | base.DisposeManagedResources(); |
| | 0 | 206 | | } |
| | | 207 | | } |
| | | 208 | | |