| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ContentDialogService.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.Linq; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.Dialogs.ContentDialogs; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Default implementation of <see cref="IContentDialogService"/>. |
| | | 17 | | /// Selects the appropriate <see cref="IDialogStrategy"/> via <c>CanHandle</c> / <c>Priority</c> |
| | | 18 | | /// and manages the full dialog lifecycle (OnOpenedAsync → show → OnClosedAsync). |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class ContentDialogService(IEnumerable<IDialogStrategy> strategies) : IContentDialogService |
| | | 21 | | { |
| | 33 | 22 | | private readonly Lock _sync = new(); |
| | 33 | 23 | | private readonly List<IDialog> _openedDialogs = []; |
| | 33 | 24 | | private readonly Dictionary<IDialog, ActiveDialog> _activeDialogs = []; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public event EventHandler<ContentDialogLifecycleEventArgs>? DialogOpened; |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public event EventHandler<ContentDialogLifecycleEventArgs>? DialogClosed; |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public IReadOnlyList<IDialog> OpenedDialogs |
| | | 34 | | { |
| | | 35 | | get |
| | 3 | 36 | | { |
| | | 37 | | lock (_sync) |
| | | 38 | | { |
| | 3 | 39 | | return _openedDialogs.ToList().AsReadOnly(); |
| | | 40 | | } |
| | 3 | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public bool HasOpenedDialogs |
| | | 46 | | { |
| | | 47 | | get |
| | 9 | 48 | | { |
| | | 49 | | lock (_sync) |
| | | 50 | | { |
| | 9 | 51 | | return _openedDialogs.Count > 0; |
| | | 52 | | } |
| | 9 | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public async Task<DialogResult<bool>> ShowAsync(IDialog dialog, DialogOptions? options = null, CancellationToken can |
| | | 58 | | { |
| | 18 | 59 | | ArgumentNullException.ThrowIfNull(dialog); |
| | 18 | 60 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 61 | | |
| | 18 | 62 | | var resolvedOptions = DialogOptions.Resolve(dialog, options); |
| | 18 | 63 | | var strategy = GetStrategy(dialog, resolvedOptions); |
| | 15 | 64 | | BeginShow(dialog, strategy); |
| | | 65 | | |
| | 15 | 66 | | await dialog.OnOpenedAsync().ConfigureAwait(false); |
| | | 67 | | |
| | | 68 | | try |
| | | 69 | | { |
| | 15 | 70 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 15 | 71 | | return await strategy.ShowAsync(dialog, resolvedOptions, cancellationToken).ConfigureAwait(false); |
| | | 72 | | } |
| | | 73 | | finally |
| | | 74 | | { |
| | 15 | 75 | | await CompleteLifecycleAsync(dialog).ConfigureAwait(false); |
| | | 76 | | } |
| | 15 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public async Task<DialogResult<TResult>> ShowAsync<TResult>(IDialog<TResult> dialog, DialogOptions? options = null, |
| | | 81 | | { |
| | 12 | 82 | | ArgumentNullException.ThrowIfNull(dialog); |
| | 12 | 83 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 84 | | |
| | 12 | 85 | | var resolvedOptions = DialogOptions.Resolve(dialog, options); |
| | 12 | 86 | | var strategy = GetStrategy(dialog, resolvedOptions); |
| | 12 | 87 | | BeginShow(dialog, strategy); |
| | | 88 | | |
| | 12 | 89 | | await dialog.OnOpenedAsync().ConfigureAwait(false); |
| | 12 | 90 | | var resultTask = dialog.GetResultAsync(); |
| | | 91 | | |
| | | 92 | | try |
| | | 93 | | { |
| | 12 | 94 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 12 | 95 | | await strategy.ShowAsync(dialog, resolvedOptions, cancellationToken).ConfigureAwait(false); |
| | | 96 | | } |
| | | 97 | | finally |
| | | 98 | | { |
| | 12 | 99 | | await CompleteLifecycleAsync(dialog).ConfigureAwait(false); |
| | | 100 | | } |
| | | 101 | | |
| | 12 | 102 | | return await resultTask.ConfigureAwait(false); |
| | 12 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <inheritdoc /> |
| | 0 | 106 | | public IDialogBuilder Create(IDialog dialog) => new DialogBuilder(this, dialog); |
| | | 107 | | |
| | | 108 | | /// <inheritdoc /> |
| | 0 | 109 | | public IDialogBuilder<TResult> Create<TResult>(IDialog<TResult> dialog) => new DialogBuilder<TResult>(this, dialog); |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public async Task CloseAsync(IDialog dialog) |
| | | 113 | | { |
| | 3 | 114 | | ArgumentNullException.ThrowIfNull(dialog); |
| | | 115 | | |
| | | 116 | | IDialogStrategy strategy; |
| | | 117 | | lock (_sync) |
| | | 118 | | { |
| | 3 | 119 | | strategy = _activeDialogs.TryGetValue(dialog, out var active) |
| | 3 | 120 | | ? active.Strategy |
| | 3 | 121 | | : GetStrategy(dialog, null); |
| | 3 | 122 | | } |
| | | 123 | | |
| | 3 | 124 | | await strategy.CloseAsync(dialog).ConfigureAwait(false); |
| | 3 | 125 | | await CompleteLifecycleAsync(dialog).ConfigureAwait(false); |
| | 3 | 126 | | } |
| | | 127 | | |
| | | 128 | | private void BeginShow(IDialog dialog, IDialogStrategy strategy) |
| | 27 | 129 | | { |
| | | 130 | | lock (_sync) |
| | | 131 | | { |
| | 27 | 132 | | _openedDialogs.Add(dialog); |
| | 27 | 133 | | _activeDialogs[dialog] = new() { Strategy = strategy }; |
| | 27 | 134 | | } |
| | | 135 | | |
| | 27 | 136 | | DialogOpened?.Invoke(this, new(dialog)); |
| | 3 | 137 | | } |
| | | 138 | | |
| | | 139 | | private async Task CompleteLifecycleAsync(IDialog dialog) |
| | 30 | 140 | | { |
| | | 141 | | bool shouldNotifyClosed; |
| | | 142 | | lock (_sync) |
| | | 143 | | { |
| | 30 | 144 | | if (!_activeDialogs.TryGetValue(dialog, out var active) || active.LifecycleCompleted) |
| | | 145 | | { |
| | 3 | 146 | | return; |
| | | 147 | | } |
| | | 148 | | |
| | 27 | 149 | | active.LifecycleCompleted = true; |
| | 27 | 150 | | _openedDialogs.Remove(dialog); |
| | 27 | 151 | | _activeDialogs.Remove(dialog); |
| | 27 | 152 | | shouldNotifyClosed = true; |
| | 27 | 153 | | } |
| | | 154 | | |
| | 27 | 155 | | if (shouldNotifyClosed) |
| | | 156 | | { |
| | 27 | 157 | | await dialog.OnClosedAsync().ConfigureAwait(false); |
| | 27 | 158 | | DialogClosed?.Invoke(this, new(dialog)); |
| | | 159 | | } |
| | 30 | 160 | | } |
| | | 161 | | |
| | | 162 | | private IDialogStrategy GetStrategy(IDialog dialog, DialogOptions? options) |
| | 30 | 163 | | => strategies |
| | 30 | 164 | | .Where(s => s.CanHandle(dialog, options)) |
| | 30 | 165 | | .OrderByDescending(s => s.Priority) |
| | 30 | 166 | | .FirstOrDefault() |
| | 30 | 167 | | ?? throw new InvalidOperationException( |
| | 30 | 168 | | $"No dialog strategy found for '{dialog.GetType().Name}'. Register an IDialogStrategy via AddDialogs() or |
| | | 169 | | |
| | | 170 | | private sealed class ActiveDialog |
| | | 171 | | { |
| | | 172 | | public required IDialogStrategy Strategy { get; init; } |
| | | 173 | | |
| | | 174 | | public bool LifecycleCompleted { get; set; } |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | |