< Summary

Information
Class: MyNet.UI.ViewModels.Display.YearDisplayModeViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.UI/ViewModels/Display/DisplayModeViewModel.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 326
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.UI/ViewModels/Display/DisplayModeViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DisplayModeViewModel.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.Globalization;
 10using System.Windows.Input;
 11using MyNet.Collections;
 12using MyNet.Observable;
 13using MyNet.Primitives;
 14using MyNet.Temporal;
 15using MyNet.UI.Commands;
 16using MyNet.UI.ViewModels.Display.Options;
 17
 18namespace MyNet.UI.ViewModels.Display;
 19
 20/// <summary>
 21/// Provides a reusable base implementation for display mode view models.
 22/// </summary>
 23public abstract class DisplayModeViewModel : ObservableObject, IDisplayModeViewModel
 24{
 25    /// <summary>Grid mode key. Prefer <see cref="DisplayModeWellKnownKeys.Grid"/> for new code.</summary>
 26    public const string GridKey = DisplayModeWellKnownKeys.Grid;
 27
 28    /// <summary>Detailed mode key. Prefer <see cref="DisplayModeWellKnownKeys.Detailed"/> for new code.</summary>
 29    public const string DetailedKey = DisplayModeWellKnownKeys.Detailed;
 30
 31    /// <summary>Chart mode key. Prefer <see cref="DisplayModeWellKnownKeys.Chart"/> for new code.</summary>
 32    public const string ChartKey = DisplayModeWellKnownKeys.Chart;
 33
 34    /// <summary>List mode key. Prefer <see cref="DisplayModeWellKnownKeys.List"/> for new code.</summary>
 35    public const string ListKey = DisplayModeWellKnownKeys.List;
 36
 37    /// <summary>Hour mode key. Prefer <see cref="DisplayModeWellKnownKeys.Hour"/> for new code.</summary>
 38    public const string HourKey = DisplayModeWellKnownKeys.Hour;
 39
 40    /// <summary>Day mode key. Prefer <see cref="DisplayModeWellKnownKeys.Day"/> for new code.</summary>
 41    public const string DayKey = DisplayModeWellKnownKeys.Day;
 42
 43    /// <summary>Week mode key. Prefer <see cref="DisplayModeWellKnownKeys.Week"/> for new code.</summary>
 44    public const string WeekKey = DisplayModeWellKnownKeys.Week;
 45
 46    /// <summary>Month mode key. Prefer <see cref="DisplayModeWellKnownKeys.Month"/> for new code.</summary>
 47    public const string MonthKey = DisplayModeWellKnownKeys.Month;
 48
 49    /// <summary>Year mode key. Prefer <see cref="DisplayModeWellKnownKeys.Year"/> for new code.</summary>
 50    public const string YearKey = DisplayModeWellKnownKeys.Year;
 51
 52    /// <summary>
 53    /// Initializes a new instance of the <see cref="DisplayModeViewModel"/> class.
 54    /// </summary>
 55    /// <param name="key">The display mode key.</param>
 56    /// <param name="commandFactory">Optional command factory used to create commands.</param>
 57    protected DisplayModeViewModel(string key, ICommandFactory? commandFactory = null)
 58    {
 59        Key = key;
 60        var commands = commandFactory.GetOrDefault();
 61        ResetCommand = commands.Create(Reset);
 62    }
 63
 64    /// <inheritdoc />
 65    public string Key { get; }
 66
 67    /// <inheritdoc />
 68    public virtual bool OverrideEmptySourceTemplate => false;
 69
 70    /// <inheritdoc />
 71    public virtual bool OverrideEmptyItemsTemplate => false;
 72
 73    /// <summary>
 74    /// Gets the command that resets this mode options.
 75    /// </summary>
 76    public ICommand ResetCommand { get; }
 77
 78    /// <inheritdoc />
 79    public virtual void Reset()
 80    {
 81    }
 82}
 83
 84/// <summary>
 85/// Represents a grid display mode.
 86/// </summary>
 87public sealed class GridDisplayModeViewModel : DisplayModeViewModel
 88{
 89    /// <summary>
 90    /// Initializes a new instance of the <see cref="GridDisplayModeViewModel"/> class.
 91    /// </summary>
 92    public GridDisplayModeViewModel()
 93        : base(GridKey)
 94    {
 95    }
 96}
 97
 98/// <summary>
 99/// Represents a detailed display mode.
 100/// </summary>
 101public sealed class DetailedDisplayModeViewModel : DisplayModeViewModel
 102{
 103    /// <summary>
 104    /// Initializes a new instance of the <see cref="DetailedDisplayModeViewModel"/> class.
 105    /// </summary>
 106    public DetailedDisplayModeViewModel()
 107        : base(DetailedKey)
 108    {
 109    }
 110}
 111
 112/// <summary>
 113/// Represents a chart display mode.
 114/// </summary>
 115public sealed class ChartDisplayModeViewModel : DisplayModeViewModel
 116{
 117    /// <summary>
 118    /// Initializes a new instance of the <see cref="ChartDisplayModeViewModel"/> class.
 119    /// </summary>
 120    public ChartDisplayModeViewModel()
 121        : base(ChartKey)
 122    {
 123    }
 124}
 125
 126/// <summary>
 127/// Represents a list display mode with column options.
 128/// </summary>
 129public sealed class ListDisplayModeViewModel : DisplayModeViewModel, IColumnOptionsDisplayModeViewModel
 130{
 131    /// <summary>
 132    /// Initializes a new instance of the <see cref="ListDisplayModeViewModel"/> class.
 133    /// </summary>
 134    /// <param name="defaultColumns">The default displayed columns.</param>
 135    /// <param name="commandFactory">Optional command factory used to create commands.</param>
 136    public ListDisplayModeViewModel(string[]? defaultColumns = null, ICommandFactory? commandFactory = null)
 137        : base(ListKey, commandFactory)
 138    {
 139        // ReSharper disable once UseCollectionExpression
 140        ColumnOptions = new ColumnOptionsViewModel(defaultColumns);
 141        var commands = commandFactory.GetOrDefault();
 142        SetDisplayedColumnsCommand = commands.Create<IEnumerable<string>>(x => SetDisplayedColumns(x ?? []));
 143    }
 144
 145    /// <inheritdoc />
 146    public override bool OverrideEmptyItemsTemplate => true;
 147
 148    /// <inheritdoc />
 149    public ColumnOptionsViewModel ColumnOptions { get; }
 150
 151    /// <inheritdoc />
 152    public ObservableRangeCollection<LabeledValue<string[]>> PresetColumns { get; } = [];
 153
 154    /// <inheritdoc />
 155    public ICommand SetDisplayedColumnsCommand { get; }
 156
 157    /// <inheritdoc />
 158    public void SetDisplayedColumns(IEnumerable<string> columns) => ColumnOptions.SetDisplayedColumns(columns);
 159
 160    /// <inheritdoc />
 161    public override void Reset() => ColumnOptions.Reset();
 162}
 163
 164/// <summary>
 165/// Provides a reusable base implementation for calendar-like display modes.
 166/// </summary>
 167public class CalendarDisplayModeViewModel : DisplayModeViewModel, IDateNavigableDisplayModeViewModel
 168{
 169    private readonly TimeUnit _unit;
 170    private readonly int _changeValue;
 171
 172    /// <summary>
 173    /// Initializes a new instance of the <see cref="CalendarDisplayModeViewModel"/> class.
 174    /// </summary>
 175    /// <param name="timeUnit">The navigation time unit.</param>
 176    /// <param name="changeValue">The navigation step value.</param>
 177    /// <param name="key">The display mode key.</param>
 178    /// <param name="commandFactory">Optional command factory used to create commands.</param>
 179    protected CalendarDisplayModeViewModel(TimeUnit timeUnit, int changeValue, string key, ICommandFactory? commandFacto
 180        : base(key, commandFactory)
 181    {
 182        _unit = timeUnit;
 183        _changeValue = changeValue;
 184
 185        DisplayDate = DateTime.Now;
 186
 187        var commands = commandFactory.GetOrDefault();
 188        MoveToPreviousDateCommand = commands.Create(MoveToPreviousDate);
 189        MoveToNextDateCommand = commands.Create(MoveToNextDate);
 190        MoveToTodayCommand = commands.Create(MoveToToday);
 191    }
 192
 193    /// <inheritdoc />
 194    public override bool OverrideEmptySourceTemplate => true;
 195
 196    /// <inheritdoc />
 197    public override bool OverrideEmptyItemsTemplate => true;
 198
 199    /// <inheritdoc />
 200    public DateTime DisplayDate { get; protected set => SetProperty(ref field, value); }
 201
 202    /// <summary>
 203    /// Gets the first day of week for calendar display.
 204    /// </summary>
 205    public virtual DayOfWeek FirstDayOfWeek => CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
 206
 207    /// <inheritdoc />
 208    public override void Reset() => MoveToToday();
 209
 210    /// <inheritdoc />
 211    public ICommand MoveToPreviousDateCommand { get; }
 212
 213    /// <inheritdoc />
 214    public ICommand MoveToNextDateCommand { get; }
 215
 216    /// <inheritdoc />
 217    public ICommand MoveToTodayCommand { get; }
 218
 219    /// <summary>
 220    /// Moves the displayed date to today.
 221    /// </summary>
 222    public virtual void MoveToToday() => DisplayDate = DateTime.Now;
 223
 224    /// <summary>
 225    /// Moves the displayed date to the next date range.
 226    /// </summary>
 227    public virtual void MoveToNextDate() => DisplayDate = GetNextDate(DisplayDate);
 228
 229    /// <summary>
 230    /// Moves the displayed date to the previous date range.
 231    /// </summary>
 232    public virtual void MoveToPreviousDate() => DisplayDate = GetPreviousDate(DisplayDate);
 233
 234    /// <summary>
 235    /// Computes the next display date.
 236    /// </summary>
 237    protected virtual DateTime GetNextDate(DateTime date) => date.Add(_changeValue, _unit);
 238
 239    /// <summary>
 240    /// Computes the previous display date.
 241    /// </summary>
 242    protected virtual DateTime GetPreviousDate(DateTime date) => date.Add(-_changeValue, _unit);
 243}
 244
 245/// <summary>
 246/// Represents an hour-based calendar display mode.
 247/// </summary>
 248/// <remarks>
 249/// Initializes a new instance of the <see cref="HourDisplayModeViewModel"/> class.
 250/// </remarks>
 251public sealed class HourDisplayModeViewModel(ICommandFactory? commandFactory = null) : CalendarDisplayModeViewModel(Time
 252
 253/// <summary>
 254/// Represents a day-based calendar display mode.
 255/// </summary>
 256public sealed class DayDisplayModeViewModel : CalendarDisplayModeViewModel
 257{
 258    /// <summary>
 259    /// Initializes a new instance of the <see cref="DayDisplayModeViewModel"/> class.
 260    /// </summary>
 261    /// <param name="displayDaysCount">The number of displayed days.</param>
 262    /// <param name="displayTimeStart">Optional displayed time start.</param>
 263    /// <param name="displayTimeEnd">Optional displayed time end.</param>
 264    /// <param name="commandFactory">Optional command factory used to create commands.</param>
 265    public DayDisplayModeViewModel(
 266        int displayDaysCount = 1,
 267        TimeSpan? displayTimeStart = null,
 268        TimeSpan? displayTimeEnd = null,
 269        ICommandFactory? commandFactory = null)
 270        : base(TimeUnit.Day, 1, DayKey, commandFactory)
 271    {
 272        DisplayDaysCount = displayDaysCount;
 273
 274        if (displayTimeStart.HasValue)
 275            DisplayTimeStart = displayTimeStart.Value;
 276
 277        if (displayTimeEnd.HasValue)
 278            DisplayTimeEnd = displayTimeEnd.Value;
 279    }
 280
 281    /// <summary>
 282    /// Gets or sets the number of displayed days.
 283    /// </summary>
 284    public int DisplayDaysCount { get; set => SetProperty(ref field, value); }
 285
 286    /// <summary>
 287    /// Gets or sets the displayed time start.
 288    /// </summary>
 289    public TimeSpan DisplayTimeStart { get; set => SetProperty(ref field, value); } = TimeSpan.Zero;
 290
 291    /// <summary>
 292    /// Gets or sets the displayed time end.
 293    /// </summary>
 294    public TimeSpan DisplayTimeEnd { get; set => SetProperty(ref field, value); } = TimeSpan.FromHours(24);
 295
 296    /// <inheritdoc />
 297    protected override DateTime GetNextDate(DateTime date) => date.Add(DisplayDaysCount, TimeUnit.Day);
 298
 299    /// <inheritdoc />
 300    protected override DateTime GetPreviousDate(DateTime date) => date.Add(-DisplayDaysCount, TimeUnit.Day);
 301}
 302
 303/// <summary>
 304/// Represents a week-based calendar display mode.
 305/// </summary>
 306/// <remarks>
 307/// Initializes a new instance of the <see cref="WeekDisplayModeViewModel"/> class.
 308/// </remarks>
 309public sealed class WeekDisplayModeViewModel(ICommandFactory? commandFactory = null) : CalendarDisplayModeViewModel(Time
 310
 311/// <summary>
 312/// Represents a month-based calendar display mode.
 313/// </summary>
 314/// <remarks>
 315/// Initializes a new instance of the <see cref="MonthDisplayModeViewModel"/> class.
 316/// </remarks>
 317public sealed class MonthDisplayModeViewModel(ICommandFactory? commandFactory = null) : CalendarDisplayModeViewModel(Tim
 318
 319/// <summary>
 320/// Represents a year-based calendar display mode.
 321/// </summary>
 322/// <remarks>
 323/// Initializes a new instance of the <see cref="YearDisplayModeViewModel"/> class.
 324/// </remarks>
 3325public sealed class YearDisplayModeViewModel(ICommandFactory? commandFactory = null) : CalendarDisplayModeViewModel(Time
 326