| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ServiceCollectionExtensions.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.Observable.Validation; |
| | | 14 | | using MyNet.UI.Extensions; |
| | | 15 | | using MyNet.UI.Resources; |
| | | 16 | | using MyNet.UI.Theming; |
| | | 17 | | |
| | | 18 | | #pragma warning disable IDE0130 |
| | | 19 | | namespace MyNet.UI; |
| | | 20 | | #pragma warning restore IDE0130 |
| | | 21 | | |
| | | 22 | | public static class ServiceCollectionExtensions |
| | | 23 | | { |
| | | 24 | | extension(IServiceCollection services) |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Registers core UI services shared across host applications, including globalization, localization, inflectio |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="supportedCultures">The cultures supported by the UI.</param> |
| | | 30 | | /// <returns>The updated service collection.</returns> |
| | 3 | 31 | | public IServiceCollection AddUi(IEnumerable<CultureInfo>? supportedCultures = null) => services.AddUi(b => b.Wit |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Registers core UI services and applies optional configuration (dialogs, locators, navigation, preferences). |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="configure">Callback to customize registrations on a <see cref="UiBuilder"/>.</param> |
| | | 37 | | /// <returns>The updated service collection.</returns> |
| | | 38 | | public IServiceCollection AddUi(Action<UiBuilder> configure) |
| | | 39 | | { |
| | 6 | 40 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 41 | | |
| | 6 | 42 | | var builder = new UiBuilder(services); |
| | 6 | 43 | | configure(builder); |
| | 6 | 44 | | builder.Apply(); |
| | | 45 | | |
| | 6 | 46 | | return services; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Contributes UI translation resources to the catalog. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <returns>The updated service collection.</returns> |
| | | 53 | | public IServiceCollection AddUiTranslations() |
| | | 54 | | { |
| | 6 | 55 | | services.AddTranslationResource(nameof(UiResources), UiResources.ResourceManager); |
| | 6 | 56 | | services.AddTranslationResource(nameof(MessageResources), MessageResources.ResourceManager); |
| | 6 | 57 | | services.AddTranslationResource(nameof(FormatResources), FormatResources.ResourceManager); |
| | | 58 | | |
| | 6 | 59 | | return services; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | extension(IServiceProvider services) |
| | | 64 | | { |
| | | 65 | | /// <summary> |
| | | 66 | | /// Configures shared UI static facades (globalization, localization, humanizer, validation). |
| | | 67 | | /// Configures <see cref="ThemeManager"/> only when <see cref="IThemeService"/> and <see cref="IThemeBaseRegistr |
| | | 68 | | /// </summary> |
| | | 69 | | /// <returns>The updated service provider.</returns> |
| | | 70 | | public IServiceProvider UseUi() |
| | | 71 | | { |
| | 0 | 72 | | services.UseGlobalization(); |
| | 0 | 73 | | services.UseLocalization(); |
| | 0 | 74 | | services.UseDisplayText(); |
| | 0 | 75 | | services.UseThemeManagerIfAvailable(); |
| | 0 | 76 | | ValidationLocalization.Configure(); |
| | | 77 | | |
| | 0 | 78 | | return services; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |