| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DialogViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Windows.Input; |
| | | 10 | | using MyNet.Globalization.Culture; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | using MyNet.UI.Dialogs; |
| | | 13 | | using MyNet.UI.Dialogs.ContentDialogs; |
| | | 14 | | using MyNet.UI.ViewModels.Workspace; |
| | | 15 | | |
| | | 16 | | namespace MyNet.UI.ViewModels.Dialog; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides a reusable base implementation for dialog view models. |
| | | 20 | | /// Implements <see cref="IDialog"/> including the <see cref="IDialogAware"/> lifecycle callbacks. |
| | | 21 | | /// </summary> |
| | | 22 | | public class DialogViewModel : WorkspaceViewModel, IDialog |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Occurs when the dialog requests to be closed. |
| | | 26 | | /// </summary> |
| | | 27 | | public event EventHandler<CloseRequestedEventArgs>? CloseRequested; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="DialogViewModel"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 33 | | /// <param name="cultureService">Optional culture service used to manage culture changes.</param> |
| | | 34 | | protected DialogViewModel( |
| | | 35 | | ICommandFactory? commandFactory = null, |
| | | 36 | | ICultureService? cultureService = null) |
| | | 37 | | : base(commandFactory, cultureService) |
| | | 38 | | => CloseCommand = Commands.Create<bool?>(close => RequestClose(close == true), CanClose); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the command to close the dialog. The boolean parameter indicates whether to force close. |
| | | 42 | | /// </summary> |
| | | 43 | | public ICommand CloseCommand { get; } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public virtual Task<bool> CanCloseAsync() => Task.FromResult(true); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Called by the infrastructure after the dialog is opened. Override for initialization logic. |
| | | 50 | | /// </summary> |
| | | 51 | | public virtual Task OnOpenedAsync() => State is LoadState.NotLoaded ? LoadAsync() : Task.CompletedTask; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Called by the infrastructure after the dialog is closed. Override for cleanup logic. |
| | | 55 | | /// </summary> |
| | | 56 | | public virtual Task OnClosedAsync() => Task.CompletedTask; |
| | | 57 | | |
| | | 58 | | /// <summary>Determines whether the close command can execute.</summary> |
| | | 59 | | protected virtual bool CanClose(bool? force) => true; |
| | | 60 | | |
| | | 61 | | /// <summary>Raises a close request for the current dialog.</summary> |
| | | 62 | | /// <param name="force">A value indicating whether the close request should bypass optional checks.</param> |
| | | 63 | | protected virtual void RequestClose(bool force = false) => CloseRequested?.Invoke(this, new() { Force = force }); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Provides a reusable base implementation for dialog view models that expose a typed result. |
| | | 68 | | /// The internal <see cref="System.Threading.Tasks.TaskCompletionSource{TResult}"/> is reset each |
| | | 69 | | /// time <see cref="OnOpenedAsync"/> is called, so the instance may be reused. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <typeparam name="TResult">The type of the dialog result.</typeparam> |
| | | 72 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 73 | | /// <param name="cultureService">Optional culture service used to manage culture changes.</param> |
| | | 74 | | public abstract class DialogViewModel<TResult>( |
| | | 75 | | ICommandFactory? commandFactory = null, |
| | | 76 | | ICultureService? cultureService = null) |
| | 48 | 77 | | : DialogViewModel(commandFactory, cultureService), IDialog<TResult> |
| | | 78 | | { |
| | 48 | 79 | | private TaskCompletionSource<DialogResult<TResult>> _tcs = new(); |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public override Task OnOpenedAsync() |
| | | 83 | | { |
| | | 84 | | // Reset so the dialog can be shown more than once. |
| | 30 | 85 | | _tcs = new(); |
| | 30 | 86 | | return base.OnOpenedAsync(); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | | 90 | | public override Task OnClosedAsync() |
| | | 91 | | { |
| | | 92 | | // Guarantee the TCS always completes é even on forced/unexpected close. |
| | 9 | 93 | | _tcs.TrySetResult(DialogResult<TResult>.Dismissed()); |
| | 9 | 94 | | return base.OnClosedAsync(); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc /> |
| | 30 | 98 | | public Task<DialogResult<TResult>> GetResultAsync() => _tcs.Task; |
| | | 99 | | |
| | | 100 | | /// <summary>Marks the dialog as successful and stores <paramref name="value"/> as the result.</summary> |
| | | 101 | | protected void SetResult(TResult value) |
| | 15 | 102 | | => _tcs.TrySetResult(DialogResult<TResult>.Success(value)); |
| | | 103 | | |
| | | 104 | | /// <summary>Marks the dialog as explicitly canceled (no result value).</summary> |
| | | 105 | | protected void SetCancelled() |
| | 6 | 106 | | => _tcs.TrySetResult(DialogResult<TResult>.Cancelled()); |
| | | 107 | | |
| | | 108 | | /// <summary>Stores the result and requests the dialog to close.</summary> |
| | | 109 | | /// <param name="result">The result returned by the dialog.</param> |
| | | 110 | | /// <param name="force">Whether the close request should be forced.</param> |
| | | 111 | | protected virtual void Close(TResult result, bool force = false) |
| | | 112 | | { |
| | 6 | 113 | | SetResult(result); |
| | 6 | 114 | | RequestClose(force); |
| | 6 | 115 | | } |
| | | 116 | | } |
| | | 117 | | |