| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FileExportViewModelBase.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.IO; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using System.Windows.Input; |
| | | 13 | | using FluentValidation; |
| | | 14 | | using MyNet.UI.Commands; |
| | | 15 | | using MyNet.UI.Dialogs; |
| | | 16 | | using MyNet.UI.Notifications; |
| | | 17 | | |
| | | 18 | | namespace MyNet.UI.ViewModels.Export; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Provides a reusable base implementation for file export dialogs. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <typeparam name="T">The exported item type.</typeparam> |
| | | 24 | | public abstract class FileExportViewModelBase<T> : ExportViewModelBase<T> |
| | | 25 | | { |
| | | 26 | | private readonly IDialogService _dialogService; |
| | | 27 | | private readonly string _defaultFolder; |
| | | 28 | | private readonly Func<string> _defaultExportName; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="FileExportViewModelBase{T}"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="dialogService">Dialog service used to select a destination file.</param> |
| | | 34 | | /// <param name="fileType">The supported file type constraints.</param> |
| | | 35 | | /// <param name="defaultExportName">Function used to generate default file name (without extension).</param> |
| | | 36 | | /// <param name="defaultFolder">Optional default destination folder.</param> |
| | | 37 | | /// <param name="notificationPublisher">Optional notification publisher used to display validation/export errors.</p |
| | | 38 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 39 | | /// <param name="validator">Optional validator used to validate this view model.</param> |
| | | 40 | | protected FileExportViewModelBase( |
| | | 41 | | IDialogService dialogService, |
| | | 42 | | ExportFileType fileType, |
| | | 43 | | Func<string> defaultExportName, |
| | | 44 | | string? defaultFolder = null, |
| | | 45 | | INotificationPublisher? notificationPublisher = null, |
| | | 46 | | ICommandFactory? commandFactory = null, |
| | | 47 | | IValidator? validator = null) |
| | 0 | 48 | | : base(notificationPublisher, commandFactory, validator ?? new FileExportViewModelValidator<T>()) |
| | | 49 | | { |
| | 0 | 50 | | _dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService)); |
| | 0 | 51 | | FileType = fileType ?? throw new ArgumentNullException(nameof(fileType)); |
| | 0 | 52 | | _defaultExportName = defaultExportName ?? throw new ArgumentNullException(nameof(defaultExportName)); |
| | 0 | 53 | | _defaultFolder = string.IsNullOrWhiteSpace(defaultFolder) |
| | 0 | 54 | | ? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) |
| | 0 | 55 | | : defaultFolder; |
| | | 56 | | |
| | | 57 | | SetFilePathCommand = Commands.Create(() => SetFilePathAsync()); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets file type constraints. |
| | | 62 | | /// </summary> |
| | | 63 | | public ExportFileType FileType { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets destination file path. |
| | | 67 | | /// </summary> |
| | 0 | 68 | | public string? Destination { get; set => SetProperty(ref field, value); } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the command that lets the user select a destination path. |
| | | 72 | | /// </summary> |
| | | 73 | | public ICommand SetFilePathCommand { get; } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public override void Load(ICollection<T> items) |
| | | 77 | | { |
| | 0 | 78 | | base.Load(items); |
| | 0 | 79 | | var directory = Path.GetDirectoryName(Destination) ?? _defaultFolder; |
| | 0 | 80 | | Destination = Path.Combine(directory, Path.ChangeExtension(_defaultExportName(), FileType.DefaultExtension)); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | private async Task SetFilePathAsync(CancellationToken cancellationToken = default) |
| | | 84 | | { |
| | 0 | 85 | | var result = await _dialogService.SaveFile() |
| | 0 | 86 | | .WithFileName(Path.GetFileNameWithoutExtension(Destination) ?? string.Empty) |
| | 0 | 87 | | .WithInitialDirectory(GetInitialDirectory()) |
| | 0 | 88 | | .WithFilters(FileType.DialogFilter) |
| | 0 | 89 | | .WithDefaultExtension(FileType.DefaultExtension) |
| | 0 | 90 | | .PickAsync(cancellationToken) |
| | 0 | 91 | | .ConfigureAwait(false); |
| | | 92 | | |
| | 0 | 93 | | if (result is { IsCancelled: false, Files.Count: > 0 }) |
| | 0 | 94 | | Destination = result.Files[0]; |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | private string GetInitialDirectory() |
| | | 98 | | { |
| | 0 | 99 | | if (!string.IsNullOrWhiteSpace(Destination)) |
| | | 100 | | { |
| | 0 | 101 | | var directory = Path.GetDirectoryName(Destination); |
| | 0 | 102 | | if (!string.IsNullOrWhiteSpace(directory) && Directory.Exists(directory)) |
| | 0 | 103 | | return directory; |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | return _defaultFolder; |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |