| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ImportListViewModel.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.Linq; |
| | | 10 | | using System.Windows.Input; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.Import; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides list operations for import items. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The import item type.</typeparam> |
| | | 19 | | public sealed class ImportListViewModel<T> : ViewModelBase |
| | | 20 | | where T : ImportItemViewModel |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="ImportListViewModel{T}"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | 15 | 26 | | public ImportListViewModel(ICommandFactory? commandFactory = null) |
| | | 27 | | { |
| | 15 | 28 | | var commands = commandFactory.GetOrDefault(); |
| | 15 | 29 | | ImportSelectionCommand = commands.Create(ImportSelection, CanApplyToSelection); |
| | 15 | 30 | | DoNotImportSelectionCommand = commands.Create(DoNotImportSelection, CanApplyToSelection); |
| | 15 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets items available for import. |
| | | 35 | | /// </summary> |
| | 15 | 36 | | public ObservableCollection<T> Items { get; } = []; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets items marked for import. |
| | | 40 | | /// </summary> |
| | 39 | 41 | | public IReadOnlyCollection<T> ImportItems => [.. Items.Where(x => x.Import)]; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the command that marks selected items for import. |
| | | 45 | | /// </summary> |
| | | 46 | | public ICommand ImportSelectionCommand { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the command that marks selected items as not imported. |
| | | 50 | | /// </summary> |
| | | 51 | | public ICommand DoNotImportSelectionCommand { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Replaces list items. |
| | | 55 | | /// </summary> |
| | | 56 | | public void Load(IEnumerable<T> items) |
| | | 57 | | { |
| | 15 | 58 | | Items.Clear(); |
| | | 59 | | |
| | 60 | 60 | | foreach (var item in items) |
| | 15 | 61 | | Items.Add(item); |
| | | 62 | | |
| | 15 | 63 | | RaiseSelectionCommandsCanExecuteChanged(); |
| | 15 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Marks selected items for import. |
| | | 68 | | /// </summary> |
| | 3 | 69 | | public void ImportSelection() => ApplyOnSelection(x => x.Import = true); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Marks selected items as not imported. |
| | | 73 | | /// </summary> |
| | 0 | 74 | | public void DoNotImportSelection() => ApplyOnSelection(x => x.Import = false); |
| | | 75 | | |
| | 3 | 76 | | private bool CanApplyToSelection() => Items.Any(x => x.IsSelected); |
| | | 77 | | |
| | | 78 | | private void ApplyOnSelection(System.Action<T> action) |
| | | 79 | | { |
| | 12 | 80 | | foreach (var item in Items.Where(x => x.IsSelected)) |
| | 3 | 81 | | action(item); |
| | 3 | 82 | | } |
| | | 83 | | |
| | | 84 | | private void RaiseSelectionCommandsCanExecuteChanged() |
| | | 85 | | { |
| | 15 | 86 | | (ImportSelectionCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 15 | 87 | | (DoNotImportSelectionCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 15 | 88 | | } |
| | | 89 | | } |
| | | 90 | | |