< Summary

Information
Class: MyNet.UI.ViewModels.Dialog.ListDialogViewModel<T1, T2>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Dialog/ListDialogViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 85
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
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(...)100%210%
Validate()100%210%
Cancel()100%210%
CanValidate()100%210%
RaiseValidateCanExecuteChanged()0%620%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Dialog/ListDialogViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ListDialogViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Windows.Input;
 10using MyNet.Globalization.Culture;
 11using MyNet.UI.Commands;
 12using MyNet.UI.ViewModels.List;
 13
 14namespace MyNet.UI.ViewModels.Dialog;
 15
 16/// <summary>
 17/// Provides a base implementation for dialog view models that display a list of items and allow the user to select one 
 18/// </summary>
 19/// <typeparam name="T">The type of the items in the list.</typeparam>
 20/// <typeparam name="TList">The type of the list view model.</typeparam>
 21public class ListDialogViewModel<T, TList> : DialogViewModel<IReadOnlyCollection<T>>
 22    where T : notnull
 23    where TList : IListViewModel<T>
 24{
 25    /// <summary>
 26    /// Gets the list view model containing the items to be displayed in the dialog. This property is initialized throug
 27    /// </summary>
 28    public TList List { get; }
 29
 30    /// <summary>
 31    /// Gets the command to execute when the user validates their selection and closes the dialog. This command is initi
 32    /// </summary>
 33    public ICommand ValidateCommand { get; }
 34
 35    /// <summary>
 36    /// Gets the command to execute when the user cancels the dialog. This command is initialized through the constructo
 37    /// </summary>
 38    public ICommand CancelCommand { get; }
 39
 40    /// <summary>
 41    /// Initializes a new instance of the <see cref="ListDialogViewModel{T, TList}"/> class with the specified list view
 42    /// </summary>
 43    /// <param name="list">The list view model containing the items to be displayed in the dialog.</param>
 44    /// <param name="commandFactory">An optional command factory to create commands.</param>
 45    /// <param name="cultureService">Optional culture service used to manage culture changes.</param>
 46    /// <exception cref="ArgumentNullException">Thrown if the list parameter is null.</exception>
 47    public ListDialogViewModel(
 48        TList list,
 49        ICommandFactory? commandFactory = null,
 50        ICultureService? cultureService = null)
 051        : base(commandFactory, cultureService)
 52    {
 053        ArgumentNullException.ThrowIfNull(list);
 054        List = list;
 55
 056        ValidateCommand = Commands.Create(Validate, CanValidate);
 057        CancelCommand = Commands.Create(Cancel);
 058    }
 59
 60    /// <summary>
 61    /// Validates the user's selection and closes the dialog, returning the selected items from the list as a read-only 
 62    /// </summary>
 063    protected virtual void Validate() => Close([.. List.Items]);
 64
 65    /// <summary>
 66    /// Cancels the dialog without returning any selected items. This method is called when the Cancel command is execut
 67    /// </summary>
 68    protected virtual void Cancel()
 69    {
 070        SetCancelled();
 071        RequestClose();
 072    }
 73
 74    /// <summary>
 75    /// Determines whether the Validate command can execute. This method returns true if the Validate command can be exe
 76    /// </summary>
 77    /// <returns>True if the Validate command can execute; otherwise, false.</returns>
 078    protected virtual bool CanValidate() => true;
 79
 80    /// <summary>
 81    /// Raises <see cref="ICommand.CanExecuteChanged"/> for <see cref="ValidateCommand"/>.
 82    /// </summary>
 083    protected void RaiseValidateCanExecuteChanged() => (ValidateCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChan
 84}
 85