using System.Diagnostics.CodeAnalysis;
using Connected.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Connected.Services;
[ExcludeFromCodeCoverage]
public static class ServiceCollectionExtensions
{
///
/// Adds a Dialog Service as a Scoped instance.
///
/// IServiceCollection
/// Continues the IServiceCollection chain.
public static IServiceCollection AddBlazorDialog(this IServiceCollection services)
{
services.TryAddScoped();
return services;
}
///
/// Adds a Snackbar Service as a Scoped instance.
///
/// IServiceCollection
/// Defines SnackbarConfiguration for this instance.
/// Continues the IServiceCollection chain.
public static IServiceCollection AddBlazorSnackbar(this IServiceCollection services, SnackbarConfiguration configuration = null)
{
configuration ??= new SnackbarConfiguration();
services.TryAddScoped(builder =>
new SnackbarService(builder.GetService(), configuration));
return services;
}
///
/// Adds a Snackbar Service as a Scoped instance.
///
/// IServiceCollection
/// Defines SnackbarConfiguration for this instance.
/// Continues the IServiceCollection chain.
public static IServiceCollection AddBlazorSnackbar(this IServiceCollection services, Action configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
var options = new SnackbarConfiguration();
configuration(options);
return AddBlazorSnackbar(services, options);
}
///
/// Adds a ResizeListener as a Scoped instance.
///
/// IServiceCollection
/// Defines ResizeOptions for this instance
/// Continues the IServiceCollection chain.
public static IServiceCollection AddBlazorResizeListener(this IServiceCollection services, ResizeOptions options = null)
{
options ??= new ResizeOptions();
services.AddBlazorResizeListener(o =>
{
o = options;
});
return services;
}
///
/// Adds a ResizeListener as a Scoped instance.
///
/// IServiceCollection
/// Defines ResizeOptions for this instance
/// Continues the IServiceCollection chain.
public static IServiceCollection AddBlazorResizeListener(this IServiceCollection services, Action options)
{
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.TryAddScoped();
services.Configure(options);
return services;
}
///
/// Adds a IResizeObserver as a Transient instance.
///
/// IServiceCollection
/// Defines ResizeObserverOptions for this instance
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudBlazorResizeObserver(this IServiceCollection services, Action options)
{
services.TryAddTransient();
services.Configure(options);
return services;
}
///
/// Adds a IResizeObserver as a Transient instance.
///
/// IServiceCollection
/// Defines ResizeObserverOptions for this instance
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudBlazorResizeObserver(this IServiceCollection services, ResizeObserverOptions options = null)
{
options ??= new ResizeObserverOptions();
services.AddMudBlazorResizeObserver(o =>
{
o = options;
});
return services;
}
///
/// Adds a IResizeObserverFactory as a scoped dependency.
///
/// IServiceCollection
/// Defines ResizeObserverOptions for this instance
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudBlazorResizeObserverFactory(this IServiceCollection services, Action options)
{
services.TryAddScoped();
services.Configure(options);
return services;
}
///
/// Adds a IResizeObserverFactory as a scoped dependency.
///
/// IServiceCollection
/// Defines ResizeObserverOptions for this instance
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudBlazorResizeObserverFactory(this IServiceCollection services, ResizeObserverOptions options = null)
{
options ??= new ResizeObserverOptions();
services.AddMudBlazorResizeObserverFactory(o =>
{
o = options;
});
return services;
}
///
/// Adds IKeyInterceptor as a Transient instance.
///
/// IServiceCollection
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudBlazorKeyInterceptor(this IServiceCollection services)
{
services.TryAddTransient();
services.TryAddScoped();
return services;
}
///
/// Adds JsEvent as a Transient instance.
///
/// IServiceCollection
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudBlazorJsEvent(this IServiceCollection services)
{
services.TryAddTransient();
services.TryAddScoped();
return services;
}
///
/// Adds ScrollManager as a transient instance.
///
/// IServiceCollection
public static IServiceCollection AddMudBlazorScrollManager(this IServiceCollection services)
{
services.TryAddTransient();
return services;
}
///
/// Adds ScrollManager as a transient instance.
///
/// IServiceCollection
/// Defines PopoverOptions for the application/user
public static IServiceCollection AddMudPopoverService(this IServiceCollection services, Action options)
{
services.Configure(options);
services.TryAddScoped();
return services;
}
///
/// Adds ScrollManager as a transient instance.
///
/// IServiceCollection
/// Defines PopoverOptions for the application/user
public static IServiceCollection AddMudPopoverService(this IServiceCollection services, PopoverOptions options)
{
options ??= new PopoverOptions();
services.AddMudPopoverService(o =>
{
o = options;
});
return services;
}
///
/// Adds ScrollListener as a transient instance.
///
/// IServiceCollection
public static IServiceCollection AddMudBlazorScrollListener(this IServiceCollection services)
{
services.TryAddTransient();
services.TryAddScoped();
return services;
}
///
/// Adds ScrollSpy as a transient instance.
///
/// IServiceCollection
public static IServiceCollection AddMudBlazorScrollSpy(this IServiceCollection services)
{
services.TryAddTransient();
services.TryAddScoped();
return services;
}
///
/// Adds JsApi as a transient instance.
///
/// IServiceCollection
public static IServiceCollection AddMudBlazorJsApi(this IServiceCollection services)
{
services.TryAddTransient();
return services;
}
///
/// Adds IEventListener as a transient instance.
///
/// IServiceCollection
public static IServiceCollection AddMudEventManager(this IServiceCollection services)
{
services.TryAddTransient();
services.TryAddScoped();
return services;
}
///
/// Adds common services required by MudBlazor components
///
/// IServiceCollection
/// Defines options for all MudBlazor services.
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudServices(this IServiceCollection services, MudServicesConfiguration configuration = null)
{
configuration ??= new MudServicesConfiguration();
return services
.AddBlazorDialog()
.AddBlazorSnackbar(configuration.SnackbarConfiguration)
.AddBlazorResizeListener(configuration.ResizeOptions)
.AddMudBlazorResizeObserver(configuration.ResizeObserverOptions)
.AddMudBlazorResizeObserverFactory()
.AddMudBlazorKeyInterceptor()
.AddMudBlazorJsEvent()
.AddMudBlazorScrollManager()
.AddMudBlazorScrollListener()
.AddMudBlazorJsApi()
.AddMudBlazorScrollSpy()
.AddMudPopoverService(configuration.PopoverOptions)
.AddMudEventManager();
}
///
/// Adds common services required by MudBlazor components
///
/// IServiceCollection
/// Defines options for all MudBlazor services.
/// Continues the IServiceCollection chain.
public static IServiceCollection AddMudServices(this IServiceCollection services, Action configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
var options = new MudServicesConfiguration();
configuration(options);
return services
.AddBlazorDialog()
.AddBlazorSnackbar(options.SnackbarConfiguration)
.AddBlazorResizeListener(options.ResizeOptions)
.AddMudBlazorResizeObserver(options.ResizeObserverOptions)
.AddMudBlazorResizeObserverFactory(options.ResizeObserverOptions)
.AddMudBlazorKeyInterceptor()
.AddMudBlazorJsEvent()
.AddMudBlazorScrollManager()
.AddMudBlazorScrollListener()
.AddMudBlazorJsApi()
.AddMudPopoverService(options.PopoverOptions)
.AddMudBlazorScrollSpy()
.AddMudEventManager();
}
}