< Summary

Information
Class: MyNet.UI.ViewModels.Crud.StringListEditionViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Crud/StringListEditionViewModel.cs
Tag: 323_28699572109
Line coverage
95%
Covered lines: 20
Uncovered lines: 1
Coverable lines: 21
Total lines: 96
Line coverage: 95.2%
Branch coverage
55%
Covered branches: 10
Total branches: 18
Branch coverage: 55.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Count()100%11100%
Load(...)50%22100%
GetValues()100%11100%
ApplyTo(...)100%22100%
CreateNewItem()100%11100%
CanAdd(...)50%44100%
CanRemove(...)50%44100%
EnsureEditableRow()50%2266.66%
OnItemsStateChanged()50%44100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Crud/StringListEditionViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="StringListEditionViewModel.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.Linq;
 10using MyNet.Observable;
 11using MyNet.UI.Commands;
 12
 13namespace MyNet.UI.ViewModels.Crud;
 14
 15/// <summary>
 16/// Provides an editable list of strings with add/remove row commands.
 17/// </summary>
 18/// <remarks>
 19/// Initializes a new instance of the <see cref="StringListEditionViewModel"/> class.
 20/// </remarks>
 21/// <param name="commandFactory">Optional command factory used to create commands.</param>
 1222public sealed class StringListEditionViewModel(ICommandFactory? commandFactory = null) : CollectionEditionViewModel<Stri
 23{
 24    /// <summary>
 25    /// Gets the number of non-empty values in <see cref="CollectionEditionViewModel{TRow}.Items"/>.
 26    /// </summary>
 327    public int Count => Items.Count(x => !string.IsNullOrWhiteSpace(x.Value));
 28
 29    /// <summary>
 30    /// Loads values into the editor.
 31    /// Ensures there is always at least one editable row.
 32    /// </summary>
 33    /// <param name="values">The values to edit.</param>
 1234    public void Load(IEnumerable<string>? values) => LoadItems(values?.Select(x => new StringListItemViewModel { Value =
 35
 36    /// <summary>
 37    /// Returns non-empty values from <see cref="CollectionEditionViewModel{TRow}.Items"/>.
 38    /// </summary>
 39    /// <returns>The current edited values.</returns>
 40    public IReadOnlyCollection<string> GetValues()
 641        => [.. Items.Select(x => x.Value).Where(x => !string.IsNullOrWhiteSpace(x)).Cast<string>()];
 42
 43    /// <summary>
 44    /// Replaces the target collection content with current edited values.
 45    /// </summary>
 46    /// <param name="target">The target collection to update.</param>
 47    public void ApplyTo(ICollection<string> target)
 48    {
 349        ArgumentNullException.ThrowIfNull(target);
 50
 351        target.Clear();
 52
 1853        foreach (var value in GetValues())
 654            target.Add(value);
 355    }
 56
 57    /// <inheritdoc />
 358    protected override StringListItemViewModel CreateNewItem() => new();
 59
 60    /// <inheritdoc />
 61    protected override bool CanAdd(StringListItemViewModel? item)
 662        => item is not null
 663           && Items.IndexOf(item) == Items.Count - 1
 664           && !string.IsNullOrWhiteSpace(item.Value);
 65
 66    /// <inheritdoc />
 67    protected override bool CanRemove(StringListItemViewModel? item)
 668        => item is not null && (Items.Count > 1 || !string.IsNullOrWhiteSpace(item.Value));
 69
 70    /// <inheritdoc />
 71    protected override void EnsureEditableRow()
 72    {
 1573        if (!Items.Any())
 074            Items.Add(new());
 1575    }
 76
 77    /// <inheritdoc />
 78    protected override void OnItemsStateChanged()
 79    {
 5180        (AddCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged();
 5181        (RemoveCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged();
 5182        NotifyPropertyChanged(nameof(Count));
 5183    }
 84}
 85
 86/// <summary>
 87/// Represents one editable string row.
 88/// </summary>
 89public sealed class StringListItemViewModel : ObservableObject
 90{
 91    /// <summary>
 92    /// Gets or sets the row value.
 93    /// </summary>
 94    public string? Value { get; set; }
 95}
 96