| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ImportBySourcesDialogViewModel.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.Linq; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using System.Windows.Input; |
| | | 12 | | using FluentValidation; |
| | | 13 | | using MyNet.UI.Commands; |
| | | 14 | | using MyNet.UI.Notifications; |
| | | 15 | | |
| | | 16 | | namespace MyNet.UI.ViewModels.Import; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides a source-driven import dialog workflow. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <typeparam name="T">The import item type.</typeparam> |
| | | 22 | | public class ImportBySourcesDialogViewModel<T> : ImportDialogViewModel<T> |
| | | 23 | | where T : ImportItemViewModel |
| | | 24 | | { |
| | | 25 | | private readonly ImportItemsProvider<T> _itemsProvider; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="ImportBySourcesDialogViewModel{T}"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="sources">The available import sources.</param> |
| | | 31 | | /// <param name="list">The import list view model.</param> |
| | | 32 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 33 | | /// <param name="notificationPublisher">Optional notification publisher used to display validation errors.</param> |
| | | 34 | | /// <param name="validator">Optional validator used to validate this view model.</param> |
| | | 35 | | public ImportBySourcesDialogViewModel( |
| | | 36 | | IReadOnlyCollection<IImportSourceViewModel<T>> sources, |
| | | 37 | | ImportListViewModel<T> list, |
| | | 38 | | ICommandFactory? commandFactory = null, |
| | | 39 | | INotificationPublisher? notificationPublisher = null, |
| | | 40 | | IValidator? validator = null) |
| | 0 | 41 | | : base(list, notificationPublisher, commandFactory, validator) |
| | | 42 | | { |
| | 0 | 43 | | _itemsProvider = new(sources); |
| | | 44 | | |
| | | 45 | | LoadSourceCommand = Commands.Create<IImportSourceViewModel<T>>(async x => await LoadSourceAsync(x).ConfigureAwai |
| | 0 | 46 | | ReloadSourceCommand = Commands.Create(ReloadSource, CanReloadSource); |
| | 0 | 47 | | ClearLoadedItemsCommand = Commands.Create(ClearLoadedItems, CanClearLoadedItems); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets available import sources. |
| | | 52 | | /// </summary> |
| | 0 | 53 | | public IReadOnlyCollection<IImportSourceViewModel<T>> Sources => _itemsProvider.Sources; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the currently loaded source. |
| | | 57 | | /// </summary> |
| | 0 | 58 | | public IImportSourceViewModel<T>? CurrentSource { get; private set => SetProperty(ref field, value); } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets a value indicating whether source items are currently shown. |
| | | 62 | | /// </summary> |
| | 0 | 63 | | public bool ShowList { get; private set => SetProperty(ref field, value); } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the command that loads items for the selected source. |
| | | 67 | | /// </summary> |
| | | 68 | | public ICommand LoadSourceCommand { get; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the command that reloads current source data. |
| | | 72 | | /// </summary> |
| | | 73 | | public ICommand ReloadSourceCommand { get; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the command that resets the loaded list. |
| | | 77 | | /// </summary> |
| | | 78 | | public ICommand ClearLoadedItemsCommand { get; } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Initializes all sources. |
| | | 82 | | /// </summary> |
| | | 83 | | public override async Task OnOpenedAsync() |
| | | 84 | | { |
| | 0 | 85 | | await base.OnOpenedAsync().ConfigureAwait(false); |
| | 0 | 86 | | await Task.WhenAll(Sources.Select(x => x.InitializeAsync(CancellationToken.None))).ConfigureAwait(false); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | 0 | 90 | | protected override bool CanValidate() => ShowList && base.CanValidate(); |
| | | 91 | | |
| | | 92 | | private async Task LoadSourceAsync(IImportSourceViewModel<T>? source) |
| | | 93 | | { |
| | 0 | 94 | | if (source is null) |
| | 0 | 95 | | return; |
| | | 96 | | |
| | 0 | 97 | | await ExecuteSafeAsync(_ => |
| | 0 | 98 | | { |
| | 0 | 99 | | _itemsProvider.LoadSource(source); |
| | 0 | 100 | | List.Load(_itemsProvider.Items); |
| | 0 | 101 | | CurrentSource = source; |
| | 0 | 102 | | ShowList = true; |
| | 0 | 103 | | return Task.CompletedTask; |
| | 0 | 104 | | }).ConfigureAwait(false); |
| | 0 | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | private bool CanReloadSource() => ShowList && CurrentSource is not null; |
| | | 108 | | |
| | | 109 | | private void ReloadSource() |
| | | 110 | | { |
| | 0 | 111 | | if (!CanReloadSource()) |
| | 0 | 112 | | return; |
| | | 113 | | |
| | 0 | 114 | | _itemsProvider.Reload(); |
| | | 115 | | |
| | 0 | 116 | | if (CurrentSource is not null) |
| | | 117 | | { |
| | 0 | 118 | | _itemsProvider.LoadSource(CurrentSource); |
| | 0 | 119 | | List.Load(_itemsProvider.Items); |
| | | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | private bool CanClearLoadedItems() => ShowList; |
| | | 124 | | |
| | | 125 | | private void ClearLoadedItems() |
| | | 126 | | { |
| | 0 | 127 | | _itemsProvider.Clear(); |
| | 0 | 128 | | List.Load([]); |
| | 0 | 129 | | CurrentSource = null; |
| | 0 | 130 | | ShowList = false; |
| | 0 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |