| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DialogServiceExtensions.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; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using MyNet.UI.Dialogs.ContentDialogs; |
| | | 11 | | using MyNet.UI.Locators; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 |
| | | 14 | | namespace MyNet.UI.Dialogs; |
| | | 15 | | #pragma warning restore IDE0130 |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Extension methods that provide DI-friendly helpers around <see cref="IDialogService"/>. |
| | | 19 | | /// They replace the need for a global static dialog manager. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class DialogServiceExtensions |
| | | 22 | | { |
| | | 23 | | extension(IDialogService dialogService) |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Resolves a dialog instance from the view-model locator and shows it. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <typeparam name="TDialog">The dialog type to resolve and show.</typeparam> |
| | | 29 | | /// <param name="viewModelLocator">The locator used to resolve the dialog.</param> |
| | | 30 | | /// <param name="options">Optional dialog options.</param> |
| | | 31 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 32 | | /// <returns>A non-typed dialog result.</returns> |
| | | 33 | | public Task<DialogResult<bool>> ShowAsync<TDialog>(IViewModelLocator viewModelLocator, |
| | | 34 | | DialogOptions? options = null, |
| | | 35 | | CancellationToken cancellationToken = default) |
| | | 36 | | where TDialog : class, IDialog |
| | 6 | 37 | | => dialogService.ShowAsync(ResolveFromLocator<TDialog>(dialogService, viewModelLocator), options, cancellati |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Resolves a typed dialog instance from the view-model locator and shows it. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <typeparam name="TDialog">The typed dialog type to resolve and show.</typeparam> |
| | | 43 | | /// <typeparam name="TResult">The typed result value returned by the dialog.</typeparam> |
| | | 44 | | /// <param name="viewModelLocator">The locator used to resolve the dialog.</param> |
| | | 45 | | /// <param name="options">Optional dialog options.</param> |
| | | 46 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 47 | | /// <returns>A strongly-typed dialog result.</returns> |
| | | 48 | | public Task<DialogResult<TResult>> ShowAsync<TDialog, TResult>(IViewModelLocator viewModelLocator, |
| | | 49 | | DialogOptions? options = null, |
| | | 50 | | CancellationToken cancellationToken = default) |
| | | 51 | | where TDialog : class, IDialog<TResult> |
| | 3 | 52 | | => dialogService.ShowAsync(ResolveFromLocator<TDialog>(dialogService, viewModelLocator), options, cancellati |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Creates a non-typed dialog builder after resolving the dialog from the view-model locator. |
| | | 56 | | /// </summary> |
| | | 57 | | public IDialogBuilder Create<TDialog>(IViewModelLocator viewModelLocator) |
| | | 58 | | where TDialog : class, IDialog |
| | 6 | 59 | | => dialogService.Create(ResolveFromLocator<TDialog>(dialogService, viewModelLocator)); |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Creates a typed dialog builder after resolving the dialog from the view-model locator. |
| | | 63 | | /// </summary> |
| | | 64 | | public IDialogBuilder<TResult> Create<TDialog, TResult>(IViewModelLocator viewModelLocator) |
| | | 65 | | where TDialog : class, IDialog<TResult> |
| | 3 | 66 | | => dialogService.Create(ResolveFromLocator<TDialog>(dialogService, viewModelLocator)); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private static TDialog ResolveFromLocator<TDialog>(IDialogService dialogService, IViewModelLocator viewModelLocator) |
| | | 70 | | where TDialog : class |
| | | 71 | | { |
| | 18 | 72 | | ArgumentNullException.ThrowIfNull(dialogService); |
| | 18 | 73 | | ArgumentNullException.ThrowIfNull(viewModelLocator); |
| | | 74 | | |
| | 15 | 75 | | var dialog = viewModelLocator.Get<TDialog>(); |
| | | 76 | | |
| | 12 | 77 | | return dialog ?? throw new InvalidOperationException($"Dialog type '{typeof(TDialog).FullName}' could not be res |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |