< Summary

Information
Class: MyNet.UI.ViewModels.Display.DisplaySelectorViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Display/DisplaySelectorViewModel.cs
Tag: 323_28699572109
Line coverage
65%
Covered lines: 19
Uncovered lines: 10
Coverable lines: 29
Total lines: 109
Line coverage: 65.5%
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(...)50%66100%
set_CurrentMode(...)100%11100%
SetMode()100%210%
SetMode(...)100%210%
SetMode(...)100%210%
AddMode(...)100%44100%
AddMode(...)0%620%
CanSetMode(...)50%22100%
SetMode(...)50%44100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Display/DisplaySelectorViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DisplaySelectorViewModel.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.Collections.ObjectModel;
 10using System.Linq;
 11using System.Windows.Input;
 12using MyNet.Observable;
 13using MyNet.UI.Commands;
 14
 15namespace MyNet.UI.ViewModels.Display;
 16
 17/// <summary>
 18/// Provides a reusable selector implementation for display modes.
 19/// </summary>
 20public class DisplaySelectorViewModel : ObservableObject, IDisplaySelectorViewModel
 21{
 22    /// <summary>
 23    /// Initializes a new instance of the <see cref="DisplaySelectorViewModel"/> class.
 24    /// </summary>
 25    /// <param name="allowedModes">The allowed display modes.</param>
 26    /// <param name="defaultMode">The optional default mode.</param>
 27    /// <param name="commandFactory">Optional command factory used to create commands.</param>
 328    public DisplaySelectorViewModel(
 329        IEnumerable<IDisplayModeViewModel>? allowedModes = null,
 330        IDisplayModeViewModel? defaultMode = null,
 331        ICommandFactory? commandFactory = null)
 32    {
 333        AllowedModes = [.. allowedModes ?? []];
 334        CurrentMode = defaultMode ?? AllowedModes.FirstOrDefault();
 35
 336        var commands = commandFactory.GetOrDefault();
 337        SetModeCommand = commands.Create<IDisplayModeViewModel>(SetMode, CanSetMode);
 338    }
 39
 40    /// <inheritdoc />
 41    public ObservableCollection<IDisplayModeViewModel> AllowedModes { get; }
 42
 43    /// <inheritdoc />
 944    public IDisplayModeViewModel? CurrentMode { get; private set => SetProperty(ref field, value); }
 45
 46    /// <inheritdoc />
 47    public ICommand SetModeCommand { get; }
 48
 49    /// <inheritdoc />
 50    public void SetMode<TMode>()
 51        where TMode : class, IDisplayModeViewModel
 052        => CurrentMode = AllowedModes.OfType<TMode>().FirstOrDefault();
 53
 54    /// <inheritdoc />
 55    public void SetMode(Type type)
 56    {
 057        ArgumentNullException.ThrowIfNull(type);
 058        CurrentMode = AllowedModes.FirstOrDefault(x => x.GetType() == type);
 059    }
 60
 61    /// <inheritdoc />
 62    public void SetMode(string key)
 63    {
 064        ArgumentException.ThrowIfNullOrWhiteSpace(key);
 065        CurrentMode = AllowedModes.FirstOrDefault(x => string.Equals(x.Key, key, StringComparison.Ordinal));
 066    }
 67
 68    /// <summary>
 69    /// Adds a mode to <see cref="AllowedModes"/>.
 70    /// </summary>
 71    /// <param name="mode">The mode to add.</param>
 72    /// <param name="isDefault">Whether the added mode should become current mode.</param>
 73    /// <returns>The current selector instance.</returns>
 74    public DisplaySelectorViewModel AddMode(IDisplayModeViewModel mode, bool isDefault = false)
 75    {
 676        ArgumentNullException.ThrowIfNull(mode);
 77
 678        AllowedModes.Add(mode);
 79
 680        if (isDefault || CurrentMode is null)
 381            CurrentMode = mode;
 82
 683        return this;
 84    }
 85
 86    /// <summary>
 87    /// Adds a mode of type <typeparamref name="TMode"/> to <see cref="AllowedModes"/>.
 88    /// </summary>
 89    /// <typeparam name="TMode">The mode type.</typeparam>
 90    /// <param name="isDefault">Whether the added mode should become current mode.</param>
 91    /// <param name="configure">Optional mode configuration callback.</param>
 92    /// <returns>The current selector instance.</returns>
 93    public DisplaySelectorViewModel AddMode<TMode>(bool isDefault = false, Action<TMode>? configure = null)
 94        where TMode : class, IDisplayModeViewModel, new()
 95    {
 096        var mode = new TMode();
 097        configure?.Invoke(mode);
 098        return AddMode(mode, isDefault);
 99    }
 100
 3101    private bool CanSetMode(IDisplayModeViewModel? mode) => mode is not null && AllowedModes.Contains(mode);
 102
 103    private void SetMode(IDisplayModeViewModel? mode)
 104    {
 3105        if (mode is not null && AllowedModes.Contains(mode))
 3106            CurrentMode = mode;
 3107    }
 108}
 109