| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ImportDialogViewModel.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.Windows.Input; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using MyNet.Observable; |
| | | 11 | | using MyNet.Observable.Behaviors; |
| | | 12 | | using MyNet.UI.Commands; |
| | | 13 | | using MyNet.UI.Notifications; |
| | | 14 | | using MyNet.UI.ViewModels.Dialog; |
| | | 15 | | |
| | | 16 | | namespace MyNet.UI.ViewModels.Import; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides a dialog workflow for importing selected items. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <typeparam name="T">The import item type.</typeparam> |
| | | 22 | | public class ImportDialogViewModel<T> : DialogViewModel<IReadOnlyCollection<T>> |
| | | 23 | | where T : ImportItemViewModel |
| | | 24 | | { |
| | | 25 | | private readonly INotificationPublisher? _notificationPublisher; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="ImportDialogViewModel{T}"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="list">The import list view model.</param> |
| | | 31 | | /// <param name="notificationPublisher">Optional notification publisher used to display validation errors.</param> |
| | | 32 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 33 | | /// <param name="validator">Optional validator used to validate this view model.</param> |
| | | 34 | | public ImportDialogViewModel( |
| | | 35 | | ImportListViewModel<T> list, |
| | | 36 | | INotificationPublisher? notificationPublisher = null, |
| | | 37 | | ICommandFactory? commandFactory = null, |
| | | 38 | | IValidator? validator = null) |
| | 12 | 39 | | : base(commandFactory) |
| | | 40 | | { |
| | 12 | 41 | | List = list; |
| | 12 | 42 | | _notificationPublisher = notificationPublisher; |
| | | 43 | | |
| | 12 | 44 | | ValidateCommand = Commands.Create(ValidateAndClose, CanValidate); |
| | 12 | 45 | | CancelCommand = Commands.Create(Cancel); |
| | | 46 | | |
| | 12 | 47 | | this.UseTracking() |
| | 12 | 48 | | .UseValidation(validator ?? new ImportDialogViewModelValidator<T>()); |
| | 12 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the import list view model. |
| | | 53 | | /// </summary> |
| | | 54 | | public ImportListViewModel<T> List { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the number of items marked for import. |
| | | 58 | | /// </summary> |
| | 27 | 59 | | public int ImportItemCount => List.ImportItems.Count; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets validation error messages from the registered <see cref="IValidationBehavior"/>. |
| | | 63 | | /// </summary> |
| | | 64 | | public IReadOnlyCollection<string> Errors => |
| | 27 | 65 | | Behaviors.TryGet<IValidationBehavior>(out var behavior) ? behavior.Errors : []; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets a value indicating whether <see cref="Errors"/> contains at least one message. |
| | | 69 | | /// </summary> |
| | 15 | 70 | | public bool HasErrors => Errors.Count > 0; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the command that validates and closes the dialog. |
| | | 74 | | /// </summary> |
| | | 75 | | public ICommand ValidateCommand { get; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the command that cancels and closes the dialog. |
| | | 79 | | /// </summary> |
| | | 80 | | public ICommand CancelCommand { get; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Determines whether validation can execute. |
| | | 84 | | /// </summary> |
| | 9 | 85 | | protected virtual bool CanValidate() => ImportItemCount > 0; |
| | | 86 | | |
| | | 87 | | private void ValidateAndClose() |
| | | 88 | | { |
| | 6 | 89 | | if (!this.TryValidateAndNotify(_notificationPublisher)) |
| | 3 | 90 | | return; |
| | | 91 | | |
| | 3 | 92 | | Close(List.ImportItems); |
| | 3 | 93 | | } |
| | | 94 | | |
| | | 95 | | private void Cancel() |
| | | 96 | | { |
| | 3 | 97 | | SetCancelled(); |
| | 3 | 98 | | RequestClose(); |
| | 3 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |