| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ExportViewModelBase.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.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using System.Windows.Input; |
| | | 11 | | using FluentValidation; |
| | | 12 | | using MyNet.Observable; |
| | | 13 | | using MyNet.Observable.Behaviors; |
| | | 14 | | using MyNet.UI.Commands; |
| | | 15 | | using MyNet.UI.Notifications; |
| | | 16 | | using MyNet.UI.ViewModels.Dialog; |
| | | 17 | | |
| | | 18 | | namespace MyNet.UI.ViewModels.Export; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Provides a reusable base implementation for export dialogs. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <typeparam name="T">The exported item type.</typeparam> |
| | | 24 | | public abstract class ExportViewModelBase<T> : DialogViewModel<bool> |
| | | 25 | | { |
| | | 26 | | private readonly INotificationPublisher? _notificationPublisher; |
| | | 27 | | private ICollection<T>? _items; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="ExportViewModelBase{T}"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="notificationPublisher">Optional notification publisher used to display validation/export errors.</p |
| | | 33 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 34 | | /// <param name="validator">Optional validator used to validate this view model.</param> |
| | | 35 | | protected ExportViewModelBase( |
| | | 36 | | INotificationPublisher? notificationPublisher = null, |
| | | 37 | | ICommandFactory? commandFactory = null, |
| | | 38 | | IValidator? validator = null) |
| | 12 | 39 | | : base(commandFactory) |
| | | 40 | | { |
| | 12 | 41 | | _notificationPublisher = notificationPublisher; |
| | | 42 | | ExportAndCloseCommand = Commands.Create(() => ExportAndCloseAsync()); |
| | 12 | 43 | | CancelCommand = Commands.Create(Cancel); |
| | | 44 | | |
| | 12 | 45 | | this.UseTracking() |
| | 12 | 46 | | .UseValidation(validator ?? new ExportViewModelValidator<T>()); |
| | 12 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets or sets a value indicating whether configuration should be saved before export. |
| | | 51 | | /// </summary> |
| | 12 | 52 | | public bool SaveConfigurationOnValidate { get; set; } = true; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the number of items loaded for export. |
| | | 56 | | /// </summary> |
| | 9 | 57 | | public int ExportItemCount { get; private set => SetProperty(ref field, value); } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets validation error messages from the registered <see cref="IValidationBehavior"/>. |
| | | 61 | | /// </summary> |
| | | 62 | | public IReadOnlyCollection<string> Errors => |
| | 27 | 63 | | Behaviors.TryGet<IValidationBehavior>(out var behavior) ? behavior.Errors : []; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets a value indicating whether <see cref="Errors"/> contains at least one message. |
| | | 67 | | /// </summary> |
| | 15 | 68 | | public bool HasErrors => Errors.Count > 0; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the command that exports and closes the dialog. |
| | | 72 | | /// </summary> |
| | | 73 | | public ICommand ExportAndCloseCommand { get; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the command that cancels the dialog. |
| | | 77 | | /// </summary> |
| | | 78 | | public ICommand CancelCommand { get; } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Loads items to export. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="items">The items to export.</param> |
| | | 84 | | public virtual void Load(ICollection<T> items) |
| | | 85 | | { |
| | 9 | 86 | | _items = items; |
| | 9 | 87 | | ExportItemCount = items.Count; |
| | 9 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Saves the current export configuration. |
| | | 92 | | /// </summary> |
| | | 93 | | protected virtual void SaveConfiguration() |
| | | 94 | | { |
| | 6 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Exports provided items. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="items">The items to export.</param> |
| | | 101 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 102 | | /// <returns><see langword="true"/> when export succeeds; otherwise <see langword="false"/>.</returns> |
| | | 103 | | protected abstract Task<bool> ExportItemsAsync(ICollection<T> items, CancellationToken cancellationToken = default); |
| | | 104 | | |
| | | 105 | | private async Task ExportAndCloseAsync(CancellationToken cancellationToken = default) |
| | | 106 | | { |
| | 9 | 107 | | if (!this.TryValidateAndNotify(_notificationPublisher)) |
| | 3 | 108 | | return; |
| | | 109 | | |
| | 6 | 110 | | if (SaveConfigurationOnValidate) |
| | 6 | 111 | | SaveConfiguration(); |
| | | 112 | | |
| | 6 | 113 | | var items = _items ?? []; |
| | 6 | 114 | | var success = await ExportItemsAsync(items, cancellationToken).ConfigureAwait(false); |
| | | 115 | | |
| | 6 | 116 | | if (success) |
| | 3 | 117 | | Close(true); |
| | 9 | 118 | | } |
| | | 119 | | |
| | | 120 | | private void Cancel() |
| | | 121 | | { |
| | 3 | 122 | | SetCancelled(); |
| | 3 | 123 | | RequestClose(); |
| | 3 | 124 | | } |
| | | 125 | | } |
| | | 126 | | |