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