< Summary

Information
Class: MyNet.UI.ViewModels.Dialog.SelectionDialogViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Dialog/SelectionDialogViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 71
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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%
Validate()100%210%
CanValidate()100%210%
HandleListPropertyChanged(...)0%4260%
DisposeManagedResources()0%620%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectionDialogViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.ComponentModel;
 8using System.Windows.Input;
 9using MyNet.Observable;
 10using MyNet.Observable.Collections.Selection;
 11using MyNet.UI.Commands;
 12using MyNet.UI.ViewModels.List.Selection;
 13
 14namespace MyNet.UI.ViewModels.Dialog;
 15
 16/// <summary>
 17/// ViewModel for a dialog allowing to select one or more items from a list. The selected items are returned when the di
 18/// </summary>
 19/// <typeparam name="T">The type of the items in the list.</typeparam>
 20public class SelectionDialogViewModel<T> : ListDialogViewModel<T, ISelectableListViewModel<T>>
 21    where T : notnull
 22{
 23    /// <summary>
 24    /// Gets the command to execute when an item is double-clicked in the list. This command will validate the selection
 25    /// </summary>
 26    public ICommand DoubleClickCommand { get; }
 27
 28    /// <summary>
 29    /// Initializes a new instance of the <see cref="SelectionDialogViewModel{T}"/> class with the specified selectable 
 30    /// </summary>
 31    /// <param name="list">The selectable list to be displayed in the dialog.</param>
 32    /// <param name="commandFactory">An optional command factory to create commands.</param>
 33    public SelectionDialogViewModel(ISelectableListViewModel<T> list, ICommandFactory? commandFactory = null)
 034        : base(list, commandFactory)
 35    {
 36        DoubleClickCommand = Commands.Create(Validate, () => List is { SelectedCount: > 0, SelectionMode: SelectionMode.
 37
 038        if (List is ObservableObject observableList)
 039            observableList.PropertyChanged += HandleListPropertyChanged;
 040    }
 41
 42    /// <summary>
 43    /// Validates the selection and closes the dialog, returning the selected items. This method is called when the Vali
 44    /// </summary>
 045    protected override void Validate() => Close([.. List.SelectedItems]);
 46
 47    /// <summary>
 48    /// Determines whether the Validate command can execute. This method returns true if there is at least one selected 
 49    /// </summary>
 50    /// <returns>True if the Validate command can execute; otherwise, false.</returns>
 051    protected override bool CanValidate() => List.SelectedCount > 0;
 52
 53    private void HandleListPropertyChanged(object? sender, PropertyChangedEventArgs e)
 54    {
 055        if (e.PropertyName != nameof(ISelectableListViewModel<>.SelectedCount) && e.PropertyName != nameof(ISelectableLi
 056            return;
 57
 058        (DoubleClickCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged();
 059        RaiseValidateCanExecuteChanged();
 060    }
 61
 62    /// <inheritdoc />
 63    protected override void DisposeManagedResources()
 64    {
 065        if (List is ObservableObject observableList)
 066            observableList.PropertyChanged -= HandleListPropertyChanged;
 67
 068        base.DisposeManagedResources();
 069    }
 70}
 71