| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="UiBuilder.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.Globalization; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | using MyNet.Globalization; |
| | | 12 | | using MyNet.Humanizer; |
| | | 13 | | using MyNet.UI.Dialogs; |
| | | 14 | | using MyNet.UI.Loading; |
| | | 15 | | using MyNet.UI.Locators; |
| | | 16 | | using MyNet.UI.Locators.Conventions; |
| | | 17 | | using MyNet.UI.Navigation; |
| | | 18 | | using MyNet.UI.Notifications; |
| | | 19 | | using MyNet.UI.Notifications.Processors; |
| | | 20 | | using MyNet.UI.Toasting; |
| | | 21 | | using MyNet.UI.Toasting.Settings; |
| | | 22 | | using MyNet.UI.ViewModels; |
| | | 23 | | |
| | | 24 | | namespace MyNet.UI.Extensions; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Configures optional UI registrations inside <c>AddUi(Action<UiBuilder>)</c>. |
| | | 28 | | /// </summary> |
| | | 29 | | public sealed class UiBuilder(IServiceCollection services) |
| | | 30 | | { |
| | | 31 | | private Action<DialogServiceBuilder>? _configureDialogs; |
| | | 32 | | private Action<ITypeResolver>? _configureViewLocators; |
| | | 33 | | private Action<IServiceCollection>? _configureNavigation; |
| | | 34 | | private Action<IList<INotificationProcessor>>? _configureNotifications; |
| | | 35 | | private Action<ToastManagerOptions>? _configureToasting; |
| | | 36 | | private bool _shellPreferences; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the underlying service collection. |
| | | 40 | | /// </summary> |
| | 6 | 41 | | public IServiceCollection Services { get; } = services ?? throw new ArgumentNullException(nameof(services)); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Sets cultures offered in shell chrome (e.g. status bar language selector). |
| | | 45 | | /// </summary> |
| | | 46 | | public UiBuilder WithSupportedCultures(IEnumerable<CultureInfo>? supportedCultures) |
| | | 47 | | { |
| | 3 | 48 | | SupportedCultures = supportedCultures; |
| | 3 | 49 | | return this; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Registers a platform dialog presenter, file dialog service, or other dialog overrides. |
| | | 54 | | /// </summary> |
| | | 55 | | public UiBuilder ConfigureDialogs(Action<DialogServiceBuilder> configure) |
| | | 56 | | { |
| | 0 | 57 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 58 | | _configureDialogs = configure; |
| | 0 | 59 | | return this; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Registers manual ViewModel ↔ View mappings on the type resolver. |
| | | 64 | | /// </summary> |
| | | 65 | | public UiBuilder ConfigureViewLocators(Action<ITypeResolver> configure) |
| | | 66 | | { |
| | 0 | 67 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 68 | | _configureViewLocators = configure; |
| | 0 | 69 | | return this; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Adds navigation guards or middleware after the core navigation stack is registered. |
| | | 74 | | /// </summary> |
| | | 75 | | public UiBuilder ConfigureNavigation(Action<IServiceCollection> configure) |
| | | 76 | | { |
| | 0 | 77 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 78 | | _configureNavigation = configure; |
| | 0 | 79 | | return this; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Customizes notification processors applied before publication. |
| | | 84 | | /// </summary> |
| | | 85 | | public UiBuilder ConfigureNotifications(Action<IList<INotificationProcessor>> configure) |
| | | 86 | | { |
| | 0 | 87 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 88 | | _configureNotifications = configure; |
| | 0 | 89 | | return this; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Customizes toast manager options. |
| | | 94 | | /// </summary> |
| | | 95 | | public UiBuilder ConfigureToasting(Action<ToastManagerOptions> configure) |
| | | 96 | | { |
| | 0 | 97 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 98 | | _configureToasting = configure; |
| | 0 | 99 | | return this; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Registers preferences page view models used by the shell preferences workspace. |
| | | 104 | | /// </summary> |
| | | 105 | | public UiBuilder AddShellPreferences() |
| | | 106 | | { |
| | 3 | 107 | | _shellPreferences = true; |
| | 3 | 108 | | return this; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | internal IEnumerable<CultureInfo>? SupportedCultures { get; private set; } |
| | | 112 | | |
| | | 113 | | internal void Apply() |
| | | 114 | | { |
| | 6 | 115 | | Services.AddGlobalization() |
| | 6 | 116 | | .AddLocalization() |
| | 6 | 117 | | .AddInflection() |
| | 6 | 118 | | .AddHumanizer() |
| | 6 | 119 | | .AddBusy() |
| | 6 | 120 | | .AddNavigation(); |
| | | 121 | | |
| | 6 | 122 | | _configureNavigation?.Invoke(Services); |
| | | 123 | | |
| | 6 | 124 | | Services.AddViewLocators(_configureViewLocators) |
| | 6 | 125 | | .AddDialogs(_configureDialogs) |
| | 6 | 126 | | .AddNotifications(_configureNotifications) |
| | 6 | 127 | | .AddToasting(_configureToasting) |
| | 6 | 128 | | .AddShell(SupportedCultures) |
| | 6 | 129 | | .AddUiTranslations(); |
| | | 130 | | |
| | 6 | 131 | | if (_shellPreferences) |
| | 3 | 132 | | Services.AddShellPreferences(); |
| | 6 | 133 | | } |
| | | 134 | | } |
| | | 135 | | |