| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DisplaySelectorViewModel.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.Collections.ObjectModel; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Windows.Input; |
| | | 12 | | using MyNet.Observable; |
| | | 13 | | using MyNet.UI.Commands; |
| | | 14 | | |
| | | 15 | | namespace MyNet.UI.ViewModels.Display; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Provides a reusable selector implementation for display modes. |
| | | 19 | | /// </summary> |
| | | 20 | | public 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> |
| | 3 | 28 | | public DisplaySelectorViewModel( |
| | 3 | 29 | | IEnumerable<IDisplayModeViewModel>? allowedModes = null, |
| | 3 | 30 | | IDisplayModeViewModel? defaultMode = null, |
| | 3 | 31 | | ICommandFactory? commandFactory = null) |
| | | 32 | | { |
| | 3 | 33 | | AllowedModes = [.. allowedModes ?? []]; |
| | 3 | 34 | | CurrentMode = defaultMode ?? AllowedModes.FirstOrDefault(); |
| | | 35 | | |
| | 3 | 36 | | var commands = commandFactory.GetOrDefault(); |
| | 3 | 37 | | SetModeCommand = commands.Create<IDisplayModeViewModel>(SetMode, CanSetMode); |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public ObservableCollection<IDisplayModeViewModel> AllowedModes { get; } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | 9 | 44 | | 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 |
| | 0 | 52 | | => CurrentMode = AllowedModes.OfType<TMode>().FirstOrDefault(); |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public void SetMode(Type type) |
| | | 56 | | { |
| | 0 | 57 | | ArgumentNullException.ThrowIfNull(type); |
| | 0 | 58 | | CurrentMode = AllowedModes.FirstOrDefault(x => x.GetType() == type); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public void SetMode(string key) |
| | | 63 | | { |
| | 0 | 64 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 0 | 65 | | CurrentMode = AllowedModes.FirstOrDefault(x => string.Equals(x.Key, key, StringComparison.Ordinal)); |
| | 0 | 66 | | } |
| | | 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 | | { |
| | 6 | 76 | | ArgumentNullException.ThrowIfNull(mode); |
| | | 77 | | |
| | 6 | 78 | | AllowedModes.Add(mode); |
| | | 79 | | |
| | 6 | 80 | | if (isDefault || CurrentMode is null) |
| | 3 | 81 | | CurrentMode = mode; |
| | | 82 | | |
| | 6 | 83 | | 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 | | { |
| | 0 | 96 | | var mode = new TMode(); |
| | 0 | 97 | | configure?.Invoke(mode); |
| | 0 | 98 | | return AddMode(mode, isDefault); |
| | | 99 | | } |
| | | 100 | | |
| | 3 | 101 | | private bool CanSetMode(IDisplayModeViewModel? mode) => mode is not null && AllowedModes.Contains(mode); |
| | | 102 | | |
| | | 103 | | private void SetMode(IDisplayModeViewModel? mode) |
| | | 104 | | { |
| | 3 | 105 | | if (mode is not null && AllowedModes.Contains(mode)) |
| | 3 | 106 | | CurrentMode = mode; |
| | 3 | 107 | | } |
| | | 108 | | } |
| | | 109 | | |