| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PreferencesViewModel.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.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Globalization; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using System.Windows.Input; |
| | | 15 | | using FluentValidation; |
| | | 16 | | using MyNet.Globalization.Culture; |
| | | 17 | | using MyNet.UI.Commands; |
| | | 18 | | using MyNet.UI.Dialogs; |
| | | 19 | | using MyNet.UI.Notifications; |
| | | 20 | | using MyNet.UI.Resources; |
| | | 21 | | using MyNet.UI.Services; |
| | | 22 | | using MyNet.UI.ViewModels.Crud; |
| | | 23 | | using MyNet.UI.ViewModels.Workspace; |
| | | 24 | | |
| | | 25 | | namespace MyNet.UI.ViewModels.Preferences; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Preferences dialog with tabbed pages and persistent settings save/reset/reload. |
| | | 29 | | /// </summary> |
| | | 30 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed in DisposeManagedReso |
| | | 31 | | public sealed class PreferencesViewModel : EditionViewModel, ITabWorkspaceViewModel |
| | | 32 | | { |
| | | 33 | | private readonly IPersistentPreferencesService _preferencesService; |
| | | 34 | | private readonly PreferencesTabWorkspace _tabs; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="PreferencesViewModel"/> class. |
| | | 38 | | /// </summary> |
| | | 39 | | public PreferencesViewModel( |
| | | 40 | | IPersistentPreferencesService preferencesService, |
| | | 41 | | IEnumerable<IWorkspaceViewModel> pages, |
| | | 42 | | IDialogService dialogService, |
| | | 43 | | INotificationPublisher notificationPublisher, |
| | | 44 | | ICommandFactory? commandFactory = null, |
| | | 45 | | IValidator? validator = null, |
| | | 46 | | ICultureService? cultureService = null) |
| | 15 | 47 | | : base(dialogService, notificationPublisher, commandFactory, validator, cultureService) |
| | | 48 | | { |
| | 15 | 49 | | _preferencesService = preferencesService ?? throw new ArgumentNullException(nameof(preferencesService)); |
| | 15 | 50 | | ArgumentNullException.ThrowIfNull(pages); |
| | | 51 | | |
| | 15 | 52 | | _tabs = new(commandFactory, cultureService); |
| | 15 | 53 | | _tabs.AddPages(pages); |
| | 15 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | 54 | 57 | | public ReadOnlyObservableCollection<IWorkspaceViewModel> Workspaces => _tabs.Workspaces; |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | 15 | 60 | | public IWorkspaceViewModel? SelectedWorkspace => _tabs.SelectedWorkspace; |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | 15 | 63 | | public int SelectedIndex => _tabs.SelectedIndex; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the command to navigate to a specific tab. |
| | | 67 | | /// </summary> |
| | 15 | 68 | | public ICommand GoToTabCommand => _tabs.GoToTabCommand; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the command to navigate to the next tab. |
| | | 72 | | /// </summary> |
| | 15 | 73 | | public ICommand GoToNextTabCommand => _tabs.GoToNextTabCommand; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the command to navigate to the previous tab. |
| | | 77 | | /// </summary> |
| | 15 | 78 | | public ICommand GoToPreviousTabCommand => _tabs.GoToPreviousTabCommand; |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | 0 | 81 | | public void Select(IWorkspaceViewModel workspace) => _tabs.Select(workspace); |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | 0 | 84 | | public void Select(int index) => _tabs.Select(index); |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | 15 | 87 | | protected override string? CreateTitle(CultureInfo culture) => UiResources.Preferences; |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | 3 | 90 | | protected override void SaveCore() => _preferencesService.Save(); |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | protected override async Task OnResetAsync(CancellationToken cancellationToken = default) |
| | | 94 | | { |
| | 3 | 95 | | _preferencesService.Reset(); |
| | | 96 | | |
| | 12 | 97 | | foreach (var workspace in Workspaces) |
| | 3 | 98 | | await workspace.ResetAsync(cancellationToken).ConfigureAwait(false); |
| | 3 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | | 102 | | protected override async Task OnRefreshAsync(CancellationToken cancellationToken = default) |
| | | 103 | | { |
| | 3 | 104 | | _preferencesService.Reload(); |
| | | 105 | | |
| | 12 | 106 | | foreach (var workspace in Workspaces) |
| | 3 | 107 | | await workspace.RefreshAsync(cancellationToken).ConfigureAwait(false); |
| | 3 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | protected override void DisposeManagedResources() |
| | | 112 | | { |
| | 3 | 113 | | _tabs.Dispose(); |
| | 3 | 114 | | base.DisposeManagedResources(); |
| | 3 | 115 | | } |
| | | 116 | | } |
| | | 117 | | |