< Summary

Information
Class: MyNet.UI.ViewModels.Import.ImportBySourcesDialogViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Import/ImportBySourcesDialogViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 37
Coverable lines: 37
Total lines: 133
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%620%
get_Sources()100%210%
set_CurrentSource(...)100%210%
set_ShowList(...)100%210%
OnOpenedAsync()0%620%
CanValidate()0%620%
LoadSourceAsync()0%620%
CanReloadSource()0%620%
ReloadSource()0%2040%
CanClearLoadedItems()100%210%
ClearLoadedItems()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Import/ImportBySourcesDialogViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ImportBySourcesDialogViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Collections.Generic;
 8using System.Linq;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using System.Windows.Input;
 12using FluentValidation;
 13using MyNet.UI.Commands;
 14using MyNet.UI.Notifications;
 15
 16namespace 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>
 22public 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)
 041        : base(list, notificationPublisher, commandFactory, validator)
 42    {
 043        _itemsProvider = new(sources);
 44
 45        LoadSourceCommand = Commands.Create<IImportSourceViewModel<T>>(async x => await LoadSourceAsync(x).ConfigureAwai
 046        ReloadSourceCommand = Commands.Create(ReloadSource, CanReloadSource);
 047        ClearLoadedItemsCommand = Commands.Create(ClearLoadedItems, CanClearLoadedItems);
 048    }
 49
 50    /// <summary>
 51    /// Gets available import sources.
 52    /// </summary>
 053    public IReadOnlyCollection<IImportSourceViewModel<T>> Sources => _itemsProvider.Sources;
 54
 55    /// <summary>
 56    /// Gets the currently loaded source.
 57    /// </summary>
 058    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>
 063    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    {
 085        await base.OnOpenedAsync().ConfigureAwait(false);
 086        await Task.WhenAll(Sources.Select(x => x.InitializeAsync(CancellationToken.None))).ConfigureAwait(false);
 087    }
 88
 89    /// <inheritdoc />
 090    protected override bool CanValidate() => ShowList && base.CanValidate();
 91
 92    private async Task LoadSourceAsync(IImportSourceViewModel<T>? source)
 93    {
 094        if (source is null)
 095            return;
 96
 097        await ExecuteSafeAsync(_ =>
 098        {
 099            _itemsProvider.LoadSource(source);
 0100            List.Load(_itemsProvider.Items);
 0101            CurrentSource = source;
 0102            ShowList = true;
 0103            return Task.CompletedTask;
 0104        }).ConfigureAwait(false);
 0105    }
 106
 0107    private bool CanReloadSource() => ShowList && CurrentSource is not null;
 108
 109    private void ReloadSource()
 110    {
 0111        if (!CanReloadSource())
 0112            return;
 113
 0114        _itemsProvider.Reload();
 115
 0116        if (CurrentSource is not null)
 117        {
 0118            _itemsProvider.LoadSource(CurrentSource);
 0119            List.Load(_itemsProvider.Items);
 120        }
 0121    }
 122
 0123    private bool CanClearLoadedItems() => ShowList;
 124
 125    private void ClearLoadedItems()
 126    {
 0127        _itemsProvider.Clear();
 0128        List.Load([]);
 0129        CurrentSource = null;
 0130        ShowList = false;
 0131    }
 132}
 133