Skip to content

Commit

Permalink
Simplify Navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Sep 6, 2023
1 parent 7af8b66 commit 427ab76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/BlazorBindings.Maui/Navigation/Navigation.Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace BlazorBindings.Maui;

public partial class Navigation
{
private NavigationManager _navigationManager;
private Router _router;
private bool _isRouterRendered;
private TaskCompletionSource<RouteData> _waitForRouteSource;

/// <summary>
Expand All @@ -22,8 +21,11 @@ public async Task NavigateToAsync(string uri, Dictionary<string, object> paramet

// I cannot use Blazor's route discovery directly as it is internal.
// Instead, I render Router internally with callbacks to get navigated page and parameters.
_navigationManager ??= _services.GetRequiredService<NavigationManager>();
_router ??= await RenderRouter();
if (!_isRouterRendered)
{
await RenderRouter();
_isRouterRendered = true;
}

var routeTask = WaitForRoute();
_navigationManager.NavigateTo(uri);
Expand Down
10 changes: 4 additions & 6 deletions src/BlazorBindings.Maui/Navigation/Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace BlazorBindings.Maui;

public partial class Navigation : INavigation
{
protected readonly IServiceProvider _services;
private readonly MauiBlazorBindingsRenderer _renderer;
private readonly NavigationManager _navigationManager;
private Type _wrapperComponentType;

internal Navigation(MauiBlazorBindingsServiceProvider services)
{
_services = services;
_renderer = services.GetRequiredService<MauiBlazorBindingsRenderer>();
_navigationManager = services.GetRequiredService<NavigationManager>();
}

internal void SetWrapperComponentType(Type wrapperComponentType)
Expand Down Expand Up @@ -79,9 +79,7 @@ public async Task PopToRootAsync(bool animated = true)
[RequiresPreviewFeatures]
public async Task<T> BuildElement<T>(Type componentType, Dictionary<string, object> arguments) where T : Element
{
var renderer = _services.GetRequiredService<MauiBlazorBindingsRenderer>();

var (bindableObject, componentTask) = await renderer.GetElementFromRenderedComponent(componentType, arguments);
var (bindableObject, componentTask) = await _renderer.GetElementFromRenderedComponent(componentType, arguments);
var element = (Element)bindableObject;

element.ParentChanged += DisposeScopeWhenParentRemoved;
Expand All @@ -96,7 +94,7 @@ async void DisposeScopeWhenParentRemoved(object _, EventArgs __)
element.ParentChanged -= DisposeScopeWhenParentRemoved;

var component = await componentTask;
renderer.RemoveRootComponent(component);
_renderer.RemoveRootComponent(component);
}
}
}
Expand Down

0 comments on commit 427ab76

Please sign in to comment.