| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WorkspaceViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using System.Windows.Input; |
| | | 12 | | using MyNet.Globalization.Culture; |
| | | 13 | | using MyNet.Observable; |
| | | 14 | | using MyNet.Observable.Behaviors.Metadata.Features.Events; |
| | | 15 | | using MyNet.UI.Commands; |
| | | 16 | | |
| | | 17 | | namespace MyNet.UI.ViewModels.Workspace; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Provides a reusable base implementation for workspace view models. |
| | | 21 | | /// </summary> |
| | | 22 | | public class WorkspaceViewModel : ViewModelBase, IWorkspaceViewModel, IEventAware<CultureChangedEvent> |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="WorkspaceViewModel"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 28 | | /// <param name="cultureService">Optional culture service used to manage culture changes.</param> |
| | 132 | 29 | | protected WorkspaceViewModel( |
| | 132 | 30 | | ICommandFactory? commandFactory = null, |
| | 132 | 31 | | ICultureService? cultureService = null) |
| | | 32 | | { |
| | 132 | 33 | | Commands = commandFactory.GetOrDefault(); |
| | | 34 | | |
| | | 35 | | RefreshCommand = Commands.Create(() => RefreshAsync(), CanRefresh); |
| | | 36 | | ResetCommand = Commands.Create(() => ResetAsync(), CanReset); |
| | | 37 | | |
| | 132 | 38 | | if (cultureService is not null) |
| | | 39 | | { |
| | 0 | 40 | | this.ReactOnCultureChanged(cultureService); |
| | | 41 | | } |
| | | 42 | | |
| | 132 | 43 | | ScheduleInitialTitle(cultureService?.CurrentCulture ?? CultureInfo.CurrentUICulture); |
| | 132 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the command factory used by this workspace (never null). |
| | | 48 | | /// </summary> |
| | | 49 | | protected ICommandFactory Commands { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the command to refresh the workspace content. |
| | | 53 | | /// </summary> |
| | | 54 | | public ICommand RefreshCommand { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the command to reset the workspace state and content. |
| | | 58 | | /// </summary> |
| | | 59 | | public ICommand ResetCommand { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets the workspace title. |
| | | 63 | | /// </summary> |
| | | 64 | | public string? Title |
| | | 65 | | { |
| | | 66 | | get; |
| | | 67 | | |
| | 27 | 68 | | set => SetProperty(ref field, value); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets or sets the workspace display mode. |
| | | 73 | | /// </summary> |
| | 15 | 74 | | public ScreenMode Mode { get; set => SetProperty(ref field, value); } = ScreenMode.Unknown; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets or sets a value indicating whether the workspace is enabled. |
| | | 78 | | /// </summary> |
| | 0 | 79 | | public bool IsEnabled { get; set => SetProperty(ref field, value); } = true; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Determines whether the workspace can be refreshed. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns><see langword="true"/> when the workspace can be refreshed; otherwise <see langword="false"/>.</returns |
| | 0 | 85 | | protected virtual bool CanRefresh() => true; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Determines whether the workspace can be reset. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <returns><see langword="true"/> when the workspace can be reset; otherwise <see langword="false"/>.</returns> |
| | 0 | 91 | | protected virtual bool CanReset() => true; |
| | | 92 | | |
| | | 93 | | /// <inheritdoc /> |
| | 15 | 94 | | Task ILoadable.LoadAsync(CancellationToken cancellationToken) => LoadAsync(cancellationToken); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Refreshes the workspace content. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 100 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | | 101 | | public Task RefreshAsync(CancellationToken cancellationToken = default) |
| | 6 | 102 | | => ExecuteSafeAsync(OnRefreshAsync, cancellationToken); |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Resets the workspace state and content. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 108 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | | 109 | | public Task ResetAsync(CancellationToken cancellationToken = default) |
| | 9 | 110 | | => ExecuteSafeAsync(OnResetAsync, cancellationToken); |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Performs the asynchronous refresh logic. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 116 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | 3 | 117 | | protected virtual Task OnRefreshAsync(CancellationToken cancellationToken = default) => OnLoadAsync(cancellationToke |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Performs the asynchronous reset logic. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 123 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | 3 | 124 | | protected virtual Task OnResetAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Creates the workspace title for the specified culture. |
| | | 128 | | /// Called from <see cref="ApplyTitle"/> after construction and on culture changes. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="culture">The culture information to use for creating the title.</param> |
| | | 131 | | /// <returns>The title to assign, or <see langword="null"/> to leave <see cref="Title"/> unchanged.</returns> |
| | 114 | 132 | | protected virtual string? CreateTitle(CultureInfo culture) => null; |
| | | 133 | | |
| | | 134 | | /// <inheritdoc/> |
| | 0 | 135 | | public virtual void OnEvent(CultureChangedEvent e) => ApplyTitle(e.Culture); |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Assigns <see cref="Title"/> from <see cref="CreateTitle"/> when the result is not <see langword="null"/>. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="culture">The culture used to build the title.</param> |
| | | 141 | | protected void ApplyTitle(CultureInfo culture) |
| | | 142 | | { |
| | 132 | 143 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 144 | | |
| | 132 | 145 | | var title = CreateTitle(culture); |
| | | 146 | | |
| | 132 | 147 | | if (title is not null) |
| | 18 | 148 | | Title = title; |
| | 132 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Schedules an initial title assignment using the current culture. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="culture">The culture to use for the initial title assignment.</param> |
| | | 155 | | private void ScheduleInitialTitle(CultureInfo culture) |
| | | 156 | | { |
| | 132 | 157 | | var sync = SynchronizationContext.Current; |
| | | 158 | | |
| | 132 | 159 | | if (sync is not null) |
| | | 160 | | { |
| | 132 | 161 | | sync.Post(_ => ApplyTitle(culture), null); |
| | 132 | 162 | | return; |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | ThreadPool.QueueUserWorkItem(_ => ApplyTitle(culture)); |
| | 0 | 166 | | } |
| | | 167 | | } |
| | | 168 | | |