< Summary

Information
Class: MyNet.UI.ViewModels.Dialog.DialogViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Dialog/DialogViewModel.cs
Tag: 323_28699572109
Line coverage
66%
Covered lines: 4
Uncovered lines: 2
Coverable lines: 6
Total lines: 117
Line coverage: 66.6%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CanCloseAsync()100%210%
OnOpenedAsync()50%22100%
OnClosedAsync()100%11100%
CanClose(...)100%210%
RequestClose(...)50%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Dialog/DialogViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DialogViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading.Tasks;
 9using System.Windows.Input;
 10using MyNet.Globalization.Culture;
 11using MyNet.UI.Commands;
 12using MyNet.UI.Dialogs;
 13using MyNet.UI.Dialogs.ContentDialogs;
 14using MyNet.UI.ViewModels.Workspace;
 15
 16namespace 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>
 22public 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)
 6037        : 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 />
 046    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>
 3051    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>
 956    public virtual Task OnClosedAsync() => Task.CompletedTask;
 57
 58    /// <summary>Determines whether the close command can execute.</summary>
 059    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>
 2163    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>
 74public abstract class DialogViewModel<TResult>(
 75    ICommandFactory? commandFactory = null,
 76    ICultureService? cultureService = null)
 77    : DialogViewModel(commandFactory, cultureService), IDialog<TResult>
 78{
 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.
 85        _tcs = new();
 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.
 93        _tcs.TrySetResult(DialogResult<TResult>.Dismissed());
 94        return base.OnClosedAsync();
 95    }
 96
 97    /// <inheritdoc />
 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)
 102        => _tcs.TrySetResult(DialogResult<TResult>.Success(value));
 103
 104    /// <summary>Marks the dialog as explicitly canceled (no result value).</summary>
 105    protected void SetCancelled()
 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    {
 113        SetResult(result);
 114        RequestClose(force);
 115    }
 116}
 117