< Summary

Information
Class: MyNet.UI.ViewModels.Import.ImportDialogViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Import/ImportDialogViewModel.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 101
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
get_ImportItemCount()100%11100%
get_Errors()100%22100%
get_HasErrors()100%11100%
CanValidate()100%11100%
ValidateAndClose()100%22100%
Cancel()100%11100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ImportDialogViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Collections.Generic;
 8using System.Windows.Input;
 9using FluentValidation;
 10using MyNet.Observable;
 11using MyNet.Observable.Behaviors;
 12using MyNet.UI.Commands;
 13using MyNet.UI.Notifications;
 14using MyNet.UI.ViewModels.Dialog;
 15
 16namespace 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>
 22public 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)
 1239        : base(commandFactory)
 40    {
 1241        List = list;
 1242        _notificationPublisher = notificationPublisher;
 43
 1244        ValidateCommand = Commands.Create(ValidateAndClose, CanValidate);
 1245        CancelCommand = Commands.Create(Cancel);
 46
 1247        this.UseTracking()
 1248            .UseValidation(validator ?? new ImportDialogViewModelValidator<T>());
 1249    }
 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>
 2759    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 =>
 2765        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>
 1570    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>
 985    protected virtual bool CanValidate() => ImportItemCount > 0;
 86
 87    private void ValidateAndClose()
 88    {
 689        if (!this.TryValidateAndNotify(_notificationPublisher))
 390            return;
 91
 392        Close(List.ImportItems);
 393    }
 94
 95    private void Cancel()
 96    {
 397        SetCancelled();
 398        RequestClose();
 399    }
 100}
 101