Add support for self scope

This commit is contained in:
TheXamlGuy
2024-05-12 12:22:04 +01:00
parent 1dc4da48bb
commit a44391f4d5
4 changed files with 33 additions and 17 deletions
+1 -2
View File
@@ -29,13 +29,12 @@ public class ComponentBuilder :
services.AddScoped<SubscriptionCollection>();
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
services.AddScoped<IDisposer, Disposer>();
services.AddTransient<ISubscriber, Subscriber>();
services.AddTransient<IPublisher, Publisher>();
services.AddTransient<IMediator, Mediator>();
services.AddScoped<IDisposer, Disposer>();
services.AddTransient<INavigationScope, NavigationScope>();
services.AddTransient<INavigationProvider, NavigationProvider>();
+5 -4
View File
@@ -24,11 +24,12 @@ public class DefaultHostBuilder :
ComponentHostCollection>();
services.AddScoped<SubscriptionCollection>();
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
services.AddTransient<ISubscriptionManager, SubscriptionManager>();
services.AddTransient<ISubscriber, Subscriber>();
services.AddTransient<IPublisher, Publisher>();
services.AddScoped<IMediator, Mediator>();
services.AddTransient<IPublisher, Publisher>();
services.AddTransient<IMediator, Mediator>();
services.AddScoped<IProxyService<IPublisher>>(provider =>
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
@@ -48,7 +49,7 @@ public class DefaultHostBuilder :
services.AddScoped<INavigationRegionCollection, NavigationRegionCollection>();
services.AddTransient<INavigationRegionProvider, NavigationRegionProvider>();
services.AddTransient<INavigationScope, NavigationScope>();
services.AddScoped<INavigationScope, NavigationScope>();
services.AddSingleton(new NamedComponent("Root"));
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
+17 -9
View File
@@ -3,20 +3,28 @@
namespace Toolkit.Foundation;
public class NavigateHandler(NamedComponent scope,
IComponentScopeProvider provider) :
IComponentScopeProvider componentScopeProvider,
IServiceProvider provider) :
INotificationHandler<Navigate>
{
public async Task Handle(Navigate args,
CancellationToken cancellationToken)
CancellationToken cancellationToken)
{
if (provider.Get(args.Scope ?? scope.Name)
is ComponentScopeDescriptor descriptor)
INavigationScope? navigationScope;
if (args.Scope == "self")
{
if (descriptor?.Services?.GetService<INavigationScope>() is INavigationScope navigationScope)
{
await navigationScope.NavigateAsync(args.Route, args.Sender,
args.Context, args.Navigated, args.Parameters, cancellationToken);
}
navigationScope = provider.GetRequiredService<INavigationScope>();
}
else
{
ComponentScopeDescriptor? descriptor = componentScopeProvider.Get(args.Scope ?? scope.Name);
navigationScope = descriptor?.Services?.GetRequiredService<INavigationScope>();
}
if (navigationScope is not null)
{
await navigationScope.NavigateAsync(args.Route, args.Sender,
args.Context, args.Navigated, args.Parameters, cancellationToken);
}
}
}
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
@@ -140,11 +141,18 @@ public partial class ObservableCollectionViewModel<TViewModel> :
return item;
}
public TViewModel Add<T>()
public TViewModel Add<T>(bool scope = false)
where T :
TViewModel
{
T? item = Factory.Create<T>();
IServiceFactory? factory = null;
if (scope)
{
IServiceScope serviceScope = Provider.CreateScope();
factory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
}
T? item = factory is not null ? factory.Create<T>() : Factory.Create<T>();
Add(item);
return item;