Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to provide a custom INavigationViewLocator so other OIC c… #32

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/AvaloniaInside.Shell/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Avalonia;
using AvaloniaInside.Shell.Presenters;
using Splat;
Expand All @@ -6,7 +7,7 @@ namespace AvaloniaInside.Shell;

public static class AppBuilderExtensions
{
public static AppBuilder UseShell(this AppBuilder builder) =>
public static AppBuilder UseShell(this AppBuilder builder, Func<INavigationViewLocator>? viewLocatorFactory = null) =>
builder.AfterPlatformServicesSetup(_ =>
{
if (Locator.CurrentMutable is null)
Expand All @@ -16,18 +17,37 @@ public static AppBuilder UseShell(this AppBuilder builder) =>

Locator.CurrentMutable.Register<INavigationRegistrar, NavigationRegistrar>();
Locator.CurrentMutable.Register<IPresenterProvider, PresenterProvider>();
Locator.CurrentMutable.Register<INavigationViewLocator, DefaultNavigationViewLocator>();

if (viewLocatorFactory is null)
{
Locator.CurrentMutable.Register<INavigationViewLocator, DefaultNavigationViewLocator>();
}

Locator.CurrentMutable.Register<INavigationUpdateStrategy>(() =>
new DefaultNavigationUpdateStrategy(Locator.Current.GetService<IPresenterProvider>()!));

Locator.CurrentMutable.Register<INavigator>(() =>
{
var viewLocator = viewLocatorFactory != null ? viewLocatorFactory.Invoke() : Locator.Current.GetService<INavigationViewLocator>()!;
var registrar = Locator.Current.GetService<INavigationRegistrar>()!;
return new Navigator(
registrar,
new RelativeNavigateStrategy(registrar),
Locator.Current.GetService<INavigationUpdateStrategy>()!,
Locator.Current.GetService<INavigationViewLocator>()!);
viewLocator
);
});
});

public static AppBuilder UseShell(this AppBuilder builder, Func<NavigationNode, object> viewFactory)
=> builder.UseShell(() => new DelegateNavigationViewLocator(viewFactory));

private class DelegateNavigationViewLocator(Func<NavigationNode, object> viewFactory)
: INavigationViewLocator
{
public object GetView(NavigationNode navigationItem)
{
return viewFactory(navigationItem);
}
}
}
Loading